37创客科创中心

 找回密码
 立即注册
查看: 827|回复: 0

250114QM

[复制链接]

194

主题

324

帖子

2399

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2399
发表于 2025-1-14 17:54:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x




  1. #  -*- coding: UTF-8 -*-

  2. # MindPlus
  3. # Python
  4. import sys
  5. sys.path.append("/root/mindplus/.lib/thirdExtension/nick-pyttsx3-thirdex")
  6. sys.path.append("/root/mindplus/.lib/thirdExtension/liliang-pyflaskweb-thirdex")
  7. sys.path.append("/root/mindplus/.lib/thirdExtension/liliang-dfr0231_pinpong_nfc-thirdex")
  8. sys.path.append("/root/mindplus/.lib/thirdExtension/37ck-face_recognition-thirdex")
  9. sys.path.append("/root/mindplus/.lib/thirdExtension/nick-face_recognition-thirdex")
  10. import os
  11. import siot
  12. import pyttsx3

  13. import requests
  14. import pyautogui
  15. from unihiker import GUI
  16. import datetime
  17. import os
  18. from pinpong.board import Board
  19. from pinpong.board import Servo
  20. from pinpong.board import Board,Pin
  21. from pinpong.extension.unihiker import *
  22. import time
  23. import cv2
  24. import numpy as np
  25. from PIL import ImageFont, ImageDraw, Image
  26. from pinpong.libs.dfrobot_pn532 import PN532_I2C
  27. import train_model
  28. import cv2
  29. import numpy as np



  30. def nfc_get_uid_to_str(info):
  31.     if info == None:
  32.         return 'None'
  33.     uidstr=""
  34.     for i in range(info.length):
  35.         uidstr=uidstr+hex(info.uid[i]).replace('0x' , '')
  36.     return uidstr

  37. # 自定义函数
  38. def FUN_BT(_1, _2, _3, _4, X, Y, W, H, BT1, BT2, BT3, BT4):
  39.     if bool(_1):
  40.         u_gui.add_button(text=_1,x=X,y=Y,w=W,h=H,onclick=BT1)
  41.     if bool(_2):
  42.         u_gui.add_button(text=_2,x=((W * 1) + X),y=Y,w=W,h=H,onclick=BT2)
  43.     if bool(_3):
  44.         u_gui.add_button(text=_3,x=((W * 2) + X),y=Y,w=W,h=H,onclick=BT3)
  45.     if bool(_4):
  46.         u_gui.add_button(text=_4,x=((W * 3) + X),y=Y,w=W,h=H,onclick=BT4)
  47. def FUN_DiBu():
  48.     global 学生信息
  49.     global 老师信息
  50.     global 家长信息
  51.     global 卡号
  52.     global 学生
  53.     ZiHao = 10
  54.     学生信息=u_gui.draw_text(text="姓名:曾念欣",x=0,y=200,font_size=ZiHao, color="#FF0000")
  55.     老师信息=u_gui.draw_text(text="老师:高一2班放学",x=0,y=220,font_size=ZiHao, color="#0000FF")
  56.     家长信息=u_gui.draw_text(text="家长:曾念欣爸爸到1号车位",x=0,y=240,font_size=ZiHao, color="#FF0000")
  57.     学生=u_gui.draw_text(text="学生:曾念欣赶往1号车位",x=0,y=260,font_size=ZiHao, color="#0000FF")
  58.     实时=u_gui.draw_text(text="实时:实时动态",x=0,y=280,font_size=ZiHao, color="#FF0000")
  59.     卡号=u_gui.draw_text(text="卡号:20200808",x=0,y=300,font_size=ZiHao, color="#000099")
  60. def UI():
  61.     FUN_TouBu()
  62.     FUN_DiBu()
  63.     FUN_CaoZuo()
  64. def fun_DongTai(msg):
  65.     作者.config(text=msg)
  66. def DuQuXinXi(CanShu):
  67.     global res
  68.     global MingZi
  69.     global JiaChang
  70.     global LaoShi
  71.     global XueSheng
  72.     global ID
  73.     res = (GETurl((str("http://iot.37ck.cn:85/cxjs/") + str(CanShu)))[0])
  74.     print(res)
  75.     ID = (res["编号"])
  76.     MingZi = (res["名字"])
  77.     JiaChang = (res["家长"])
  78.     LaoShi = (res["老师"])
  79.     XueSheng = (res["学生"])
  80.     学生信息.config(text=(res["名字"]))
  81.     老师信息.config(text=(res["老师"]))
  82.     家长信息.config(text=(res["家长"]))
  83.     学生.config(text=(res["学生"]))
  84.     卡号.config(text=(str((res["卡号"])) + str((str(":卡号--人脸:") + str((res["人脸"]))))))
  85.     return res
  86. def XianShi(_1, _2, _3, _4, _5, _6):
  87.     if bool(_1):
  88.         学生信息.config(text=_1)
  89.     if bool(_2):
  90.         老师信息.config(text=_2)
  91.     if bool(_3):
  92.         家长信息.config(text=_3)
  93.     if bool(_4):
  94.         学生.config(text=_4)
  95.     if bool(_5):
  96.         实时.config(text=_5)
  97.     if bool(_6):
  98.         卡号.config(text=_6)
  99. def DuKaNFC():
  100.     nfc = PN532_I2C()
  101.     while not nfc.begin():
  102.         pass
  103.     KaHao = ''
  104.     if nfc.scan():
  105.         info = nfc.get_information()
  106.         if info !=None:
  107.             print(nfc_get_uid_to_str(info))
  108.             KaHao = nfc_get_uid_to_str(info)
  109.         else:
  110.             print("空卡")
  111.             KaHao = '空卡'
  112.     卡号.config(text=(str("卡号:") + str(KaHao)))
  113.     return KaHao
  114. def fun_BeiJing(TuPian):
  115.     bj.config(image=TuPian)
  116. def FUN_TouBu():
  117.     global 作者
  118.     global bj
  119.     bj=u_gui.draw_image(image="bj.png",x=0,y=0)
  120.     项目名字=u_gui.draw_text(text=G_ZuoPin,x=40,y=0,font_size=16, color="#CC0000")
  121.     作者=u_gui.draw_text(text="请选择操作",x=5,y=30,font_size=11, color="#0000FF")
  122.     u_gui.draw_round_rect(x=2,y=75,w=236,h=120,r=5,width=3,color="#FF0000")
  123. def FUN_CaoZuo():
  124.     FUN_BT('', '', '语音', '背景', 3, 35, 58, 30, bt_语音, bt_清空, bt_语音, bt_背景)
  125.     FUN_BT('录入', '建模', '识别', '读卡', 3, 80, 58, 35, bt_人脸录入, bt_建模, bt_人脸识别, bt_读卡)
  126.     FUN_BT('老师', '学生', '家长', '清空', 3, 115, 58, 35, bt_老师, bt_学生, bt_老师, bt_清空)
  127.     FUN_BT('读取', '领取', '接送区', '等待区', 3, 150, 58, 35, bt_31, bt_32, bt_33, bt_34)
  128. def QingKong():
  129.     global CiShu
  130.     CiShu = 0
  131.     for index in range(10):
  132.         CiShu = (CiShu + 1)
  133.         GETurl((str("http://iot.37ck.cn:85/mjs/") + str((str(CiShu) + str((str("/老师/") + str("空")))))))
  134.         GETurl((str("http://iot.37ck.cn:85/mjs/") + str((str(CiShu) + str((str("/学生/") + str("空")))))))
  135.         GETurl((str("http://iot.37ck.cn:85/mjs/") + str((str(CiShu) + str((str("/家长/") + str("空")))))))
  136.         GETurl((str("http://iot.37ck.cn:85/mjs/") + str((str(CiShu) + str((str("/备注/") + str("空")))))))
  137. def FaSong(msg, topic):
  138.     siot.publish(topic=topic, data=msg)
  139. def AnQuanJieSong(XinXi):
  140.     GETurl((str("http://iot.37ck.cn:85/mjs/") + str((str(XinXi) + str((str("/家长/") + str("空")))))))
  141.     GETurl((str("http://iot.37ck.cn:85/mjs/") + str((str(XinXi) + str((str("/老师/") + str("空")))))))
  142.     GETurl((str("http://iot.37ck.cn:85/mjs/") + str((str(XinXi) + str((str("/学生/") + str("空")))))))
  143.     GETurl((str("http://iot.37ck.cn:85/mjs/") + str((str(XinXi) + str((str("/备注/") + str("空")))))))
  144. def JieSong(XueSheng):
  145.     global msg
  146.     DuQuXinXi(XueSheng)
  147.     if (LaoShi == '空'):
  148.         GETurl((str("http://iot.37ck.cn:85/mjs/") + str((str(ID) + str((str("/老师/") + str("高一2班放学")))))))
  149.     else:
  150.         if (JiaChang == '空'):
  151.             msg = '到安全接送区'
  152.             GETurl((str("http://iot.37ck.cn:85/mjs/") + str((str(ID) + str((str("/学生/") + str("1/老师/高一2班放学")))))))
  153.         else:
  154.             msg = '前往接送车位'
  155.             GETurl((str("http://iot.37ck.cn:85/mjs/") + str((str(ID) + str((str("/学生/") + str((str(MingZi) + str(msg)))))))))
  156.     return (str(MingZi) + str(msg))
  157. def YuYinHeCheng(NaRong, YinPin):
  158.     engine = pyttsx3.init()
  159.     engine.setProperty('volume',1)
  160.     engine.setProperty('rate',200)
  161.     if (YinPin == ''):
  162.         engine.say(NaRong)
  163.         engine.runAndWait()
  164.     else:
  165.         engine.save_to_file(NaRong, YinPin+'.mp3')
  166.         engine.runAndWait()
  167. def YuYin(msg):
  168.     p_gravitysynthesis.speak(msg)
  169. def httpPost():
  170.     dic = requests.get("http://iot.37ck.cn:85/getlog/677").json()
  171.     dic["ID"]='677'
  172.     dic["name"]='QZH'
  173.     dic["S"]='QZH'
  174.     dic["T"]='QZH'
  175.     dic["J"]='QZH'
  176.     print(dic)
  177.     GETurl((str("http://iot.37ck.cn:85/aedb/677/") + str((str((str(DuKaNFC()) + str("/"))) + str("banana")))))
  178.     return (dic["name"])
  179. def LMQTT():
  180.     pass
  181. def RenLianShiBie():
  182.     def numberMap(x, in_min, in_max, out_min, out_max):
  183.         return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min

  184.     def drawChinese(text,x,y,size,r, g, b, a,img):
  185.         font = ImageFont.truetype("HYQiHei_50S.ttf", size)
  186.         img_pil = Image.fromarray(img)
  187.         draw = ImageDraw.Draw(img_pil)
  188.         draw.text((x,y), text, font=font, fill=(b, g, r, a))
  189.         frame = np.array(img_pil)
  190.         return frame

  191.     cap = cv2.VideoCapture(0)
  192.     cap.set(cv2.CAP_PROP_FRAME_WIDTH, 240)
  193.     cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 320)
  194.     cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)
  195.     cv2.namedWindow('cvwindow',cv2.WND_PROP_FULLSCREEN)
  196.     cv2.setWindowProperty('cvwindow', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
  197.     pic_counta = 0
  198.     pic_countb = 0
  199.     font = cv2.FONT_HERSHEY_SIMPLEX
  200.     while not cap.isOpened():
  201.         continue
  202.     detector=cv2.CascadeClassifier(cv2.data.haarcascades+ 'haarcascade_frontalface_default.xml')
  203.     recognizer = cv2.face.LBPHFaceRecognizer_create()
  204.     recognizer.read("/root/face_recognition/model.yml")
  205.     global img_id, confidence
  206.     img_id, confidence = 0,0
  207.     while True:

  208.         cvimg_success, img_src = cap.read()
  209.         cvimg_h, cvimg_w, cvimg_c = img_src.shape
  210.         cvimg_w1 = cvimg_h*240//320
  211.         cvimg_x1 = (cvimg_w-cvimg_w1)//2
  212.         img_src = img_src[:, cvimg_x1:cvimg_x1+cvimg_w1]
  213.         img_src = cv2.resize(img_src, (240, 320))
  214.         gray = cv2.cvtColor(img_src, cv2.COLOR_BGR2GRAY)
  215.         faces = detector.detectMultiScale(gray, 1.3, 5)
  216.         cv2.imshow('cvwindow', img_src)

  217.         if len(faces)>0:
  218.             img_id, confidence = 0,0
  219.             for (x, y, cvimg_w, cvimg_h) in faces:
  220.                 cv2.rectangle(img_src, (x - 50, y - 50), (x + cvimg_w + 50, y + cvimg_h + 50), (0, 255, 0), 2)
  221.                 img_array = gray[y:y + cvimg_h, x:x + cvimg_w]
  222.                 img_id, confidence = recognizer.predict(img_array)
  223.                 confidence   = (numberMap(confidence, 150, 0,0,100))
  224.                 if confidence ==100:
  225.                     confidence = 0

  226.             if (confidence > 40):
  227.                 MingZi = ((GETurl((str("http://iot.37ck.cn:85/cxjs/") + str(img_id)))[0])["名字"])
  228.                 DingYueZhuTi(reshttp, JieSong(MingZi))
  229.                 img_src = drawChinese(text=str((str(MingZi) + str(JiaChang))),x=10, y=20,size=25,r= 50,g=200,b=0,a=0,img=img_src)
  230.                 cv2.imshow('cvwindow', img_src)
  231.                 print(img_id)
  232.             else:
  233.                 img_src = drawChinese(text=str("检测中"),x=10, y=60,size=25,r= 200,g=0,b=0,a=0,img=img_src)
  234.                 cv2.imshow('cvwindow', img_src)
  235.         else:
  236.             pass
  237.         cv2.imshow('cvwindow', img_src)
  238.         cv2.waitKey(5)
  239. def DingYueZhuTi(TZhuTi, TXiaoXi):
  240.     siot.getsubscribe(topic=TZhuTi)
  241.     siot.publish(topic=TZhuTi, data=TXiaoXi)
  242. def GETurl(TWangZhi):
  243.     try:
  244.         gethttp = requests.get(TWangZhi)
  245.         gethttp.raise_for_status()
  246.         return gethttp.json()
  247.     except:
  248.         return '没有数据'
  249. def FenJieMingZi(NaRong):
  250.     return (NaRong[0:(NaRong.find(":"))])
  251. def LuRuRenLian(ID, ShuLiang):
  252.     def numberMap(x, in_min, in_max, out_min, out_max):
  253.         return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min

  254.     def drawChinese(text,x,y,size,r, g, b, a,img):
  255.         font = ImageFont.truetype("HYQiHei_50S.ttf", size)
  256.         img_pil = Image.fromarray(img)
  257.         draw = ImageDraw.Draw(img_pil)
  258.         draw.text((x,y), text, font=font, fill=(b, g, r, a))
  259.         frame = np.array(img_pil)
  260.         return frame

  261.     cap = cv2.VideoCapture(0)
  262.     cap.set(cv2.CAP_PROP_FRAME_WIDTH, 240)
  263.     cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 320)
  264.     cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)
  265.     cv2.namedWindow('cvwindow',cv2.WND_PROP_FULLSCREEN)
  266.     cv2.setWindowProperty('cvwindow', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
  267.     pic_counta = 0
  268.     pic_countb = 0
  269.     font = cv2.FONT_HERSHEY_SIMPLEX
  270.     while not cap.isOpened():
  271.         continue
  272.     detector=cv2.CascadeClassifier(cv2.data.haarcascades+ 'haarcascade_frontalface_default.xml')
  273.     recognizer = cv2.face.LBPHFaceRecognizer_create()


  274.     while not (cv2.waitKey(10)==ord('a')):
  275.         time.sleep(0.1)

  276.     while not (pic_counta >=ShuLiang):
  277.         cvimg_success, img_src = cap.read()
  278.         cvimg_h, cvimg_w, cvimg_c = img_src.shape
  279.         cvimg_w1 = cvimg_h*240//320
  280.         cvimg_x1 = (cvimg_w-cvimg_w1)//2
  281.         img_src = img_src[:, cvimg_x1:cvimg_x1+cvimg_w1]
  282.         img_src = cv2.resize(img_src, (240, 320))
  283.         gray = cv2.cvtColor(img_src, cv2.COLOR_BGR2GRAY)
  284.         faces = detector.detectMultiScale(gray, 1.3, 5)
  285.         cv2.imshow('cvwindow', img_src)
  286.         img_dir_path ="/root/face_recognition/picture/1/"
  287.         img_name_path=str(datetime.datetime.now().strftime('%Y%m%d_%H%M%S_%f'))+".jpg"
  288.         img_save_path=img_dir_path+img_name_path
  289.         img_dir_path ="/root/face_recognition/picture//ID/"
  290.         img_name_path=str(datetime.datetime.now().strftime('%Y%m%d_%H%M%S_%f'))+".jpg"
  291.         img_save_path=img_dir_path+img_name_path
  292.         try:
  293.             if not os.path.exists(img_dir_path):
  294.                 print("The folder does not exist,created automatically")
  295.                 os.system("mkdir -p /root/face_recognition/picture//ID/")
  296.         except IOError:
  297.             print("IOError,created automatically")
  298.             break
  299.         for (x, y, w, h) in faces:
  300.             cv2.rectangle(img_src, (x, y), (x + w, y + h), (0, 255, 0), 2)
  301.             if pic_counta <= ShuLiang:
  302.                 cv2.putText(img_src, 'shooting', (10, 50), font, 0.6, (0, 255, 0), 2)
  303.                 cv2.imwrite(img_save_path,img_src)
  304.                 pic_counta=pic_counta+1
  305.                 print("save picture path:",img_save_path)
  306.                 print("save ID-picture count:"+str(pic_counta))
  307.                 cv2.putText(img_src, 'ID', (x, y+h), font, 0.6, (0, 0, 255), 2)
  308.                 time.sleep(0.05)
  309.             else:
  310.                 cv2.putText(img_src, 'Done,Please quit', (10, 50), font, 0.6, (0, 255, 0), 2)
  311.                 time.sleep(2)

  312.         cv2.imshow('cvwindow', img_src)
  313.         cv2.waitKey(5)
  314.     cap.release()
  315.     cv2.destroyAllWindows()


  316. def DuoJi(BianHao, JiaoDu):
  317.     if (BianHao == 8):
  318.         servo1 = Servo(Pin((Pin.P8)))
  319.         servo1.write_angle(JiaoDu)
  320.     elif (BianHao == 9):
  321.         servo2 = Servo(Pin((Pin.P9)))
  322.         servo2.write_angle(JiaoDu)
  323. def JianMo():
  324.     print("开始训练模型")
  325.     detector=cv2.CascadeClassifier(cv2.data.haarcascades+ 'haarcascade_frontalface_default.xml')
  326.     recognizer = cv2.face.LBPHFaceRecognizer_create()
  327.     faces, Ids = train_model.get_images_and_labels('/root/face_recognition/picture/')
  328.     recognizer.train(faces, np.array(Ids))
  329.     img_dir_path ="/root/face_recognition/model.yml"
  330.     img_dir_path = os.path.abspath(os.path.dirname(img_dir_path))
  331.     print(img_dir_path)
  332.     try:
  333.         if not os.path.exists(img_dir_path):
  334.             print("The folder does not exist,created automatically")
  335.             os.mkdir(img_dir_path)
  336.     except IOError:
  337.         print("IOError,created automatically")
  338.     recognizer.save("/root/face_recognition/model.yml")
  339.     print("训练完成")
  340.     return '训练完成'
  341. def ChuWuGui(msg):
  342.     if (msg.find("8")!=-1):
  343.         if (msg.find("开门")!=-1):
  344.             DuoJi(8, 0)
  345.         elif (msg.find("关门")!=-1):
  346.             DuoJi(8, 80)
  347.     elif (msg.find("9")!=-1):
  348.         if (msg.find("开门")!=-1):
  349.             DuoJi(9, 0)
  350.         elif (msg.find("关门")!=-1):
  351.             DuoJi(9, 80)
  352. def GET(TWangZhi, TJian):
  353.     try:
  354.         gethttp = requests.get(TWangZhi)
  355.         gethttp.raise_for_status()
  356.         return gethttp.json().get(TJian)
  357.     except:
  358.         return '没有数据'

  359. # 事件回调函数
  360. def on_buttona_click_callback():
  361.     DuQuXinXi(DuKaNFC())
  362. def bt_人脸录入():
  363.     LuRuRenLian(1, 10)
  364. def bt_清空():
  365.     QingKong()
  366.     YuYinHeCheng('清空数据完成', '')
  367. def bt_31():
  368.     FaSong('有留言', reshttp)
  369. def bt_建模():
  370.     JianMo()
  371. def bt_读卡():
  372.     DuQuXinXi(DuKaNFC())
  373. def bt_32():
  374.     FaSong('有寄存物品', reshttp)
  375. def bt_33():
  376.     FaSong('赶往接送车位', reshttp)
  377. def bt_人脸识别():
  378.     RenLianShiBie()
  379. def bt_语音():
  380.     YuYinHeCheng('测试语音', '')
  381. def bt_34():
  382.     FaSong('等待区等待家长', reshttp)
  383. def bt_学生():
  384.     DuQuXinXi(DuKaNFC())
  385.     DingYueZhuTi(reshttp, JieSong(ID))
  386. def bt_老师():
  387.     DingYueZhuTi(reshttp, '高一2班放学')
  388. def bt_背景():
  389.     global G_BeiJing
  390.     if (G_BeiJing == 0):
  391.         fun_BeiJing('bj.png')
  392.         G_BeiJing = 1
  393.     else:
  394.         fun_BeiJing('bj1.png')
  395.         G_BeiJing = 0
  396. def on_buttonb_click_callback():
  397.     siot.publish_save(topic=reshttp, data=httpPost())
  398. def on_message_callback(client, userdata, msg):
  399.     print((str(msg.topic) + str(msg.payload.decode())))
  400.     作者.config(text=msg.payload.decode())
  401.     YuYinHeCheng(msg.payload.decode(), '')
  402.     if (msg.payload.decode().find("接送完成")!=-1):
  403.         DuQuXinXi(FenJieMingZi(msg.payload.decode()))
  404.         print(res)
  405.         AnQuanJieSong(ID)
  406.     if (msg.payload.decode().find("放学")!=-1):
  407.         global CiShu
  408.         CiShu = 0
  409.         for index in range(10):
  410.             CiShu = (CiShu + 1)
  411.             GETurl((str("http://iot.37ck.cn:85/mjs/") + str((str(CiShu) + str((str("/老师/") + str("高一2班放学")))))))
  412.     if (msg.payload.decode().find("号车位")!=-1):
  413.         GETurl((str("http://iot.37ck.cn:85/mjs/") + str((str(ID) + str((str("/家长/") + str(msg.payload.decode())))))))


  414. u_gui=GUI()
  415. Board().begin()
  416. siot.init(client_id="8549116620837305",server="iot.37ck.cn",port=1883,user="37ck",password="20200808")
  417. u_gui.on_a_click(on_buttona_click_callback)
  418. u_gui.on_b_click(on_buttonb_click_callback)
  419. siot.connect()
  420. siot.loop()
  421. siot.set_callback(on_message_callback)
  422. global reshttp
  423. G_BeiJing = 0
  424. G_ZuoPin = '安全蜂速:接送'
  425. ID = '安全蜂速:接送'
  426. reshttp = '37ck/莘村中学'
  427. UI()
  428. print(reshttp)
  429. LMQTT()
  430. DingYueZhuTi(reshttp, ID)
  431. while True:
  432.     pass
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|37创客科创中心

GMT+8, 2025-12-10 05:14 , Processed in 0.164259 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表