最近做的一個東西,分享一下。
過程中,還是總結了一些經驗,以及一些坑,最後還是自己慢慢填起來了。
先放UI,參考鏈接
原界面:
改進後:
把原來得開始按鈕改爲True,然後setText修改True之後得按鈕文本。
增加了打開檢測功能,調用人眼檢測的py文件。
有個坑:因爲人眼檢測需要調用攝像頭,點擊原有的開始按鈕也會調用攝像頭。所以需要先release原有的,再進行調用,不然兩個調用攝像頭的都起不來。
所以改進如下:調用完以後,還需要重啓原有調用的攝像頭,不然原有程序未響應,攝像頭不會有反應。
def realtime_detection(self):
self.MsgTE.setPlainText("detecting...")
tag = self.ShowBt.text()
if tag == '打開檢測':
self.camera.release()
#調用眨眼檢測py文件:
os.system("python eye_detecting.py")
self.camera = cv2.VideoCapture(0)
本地測試,配合原有的錄像,達到"離線"檢測的目的。
def Onclick(self):
msgBox = QMessageBox()
msgBox.setWindowTitle('message')
msgBox.setText('你是否想打開示例視頻')
msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
buttonY = msgBox.button(QMessageBox.Yes)
buttonY.setText('是')
buttonN = msgBox.button(QMessageBox.No)
buttonN.setText('選擇其他文件')
msgBox.exec_()
if msgBox.clickedButton() == buttonY:
os.system(cmd)
self.MsgTE.setPlainText("video is over...")
else:
# dir_path = QFileDialog.getExistingDirectory(self, "choose directory", "D:/Record")
# print(directory1)
self.MsgTE.setPlainText("choose File.avi...")
#可以自行百度下這個方法。
fd=QFileDialog.getOpenFileName(self,'選擇一個avi文件','D:/Record','ALL(*.*);;Images(*.png *.jpg);;avi文件(*.avi)','avi文件(*.avi)')
fd=str(fd)
import re
#匹配文件名
match_obj = re.match('\(\'(.*)\',', fd)
if match_obj:
res = match_obj.group(1)
os.system('python detect_blinks.py --shape-predictor shape_predictor_68_face_landmarks.dat --video %s' % res)
又由於原有的退出,退出有點異常,會卡頓,所以調整了一下,使退出沒有那麼僵硬。
def ExitApp(self, QCloseEvent):
#1 中文
msgBox = QMessageBox()
msgBox.setWindowTitle('message')
msgBox.setText('返回上一級點擊右上角<b>X</b>')
msgBox.setStandardButtons(QMessageBox.No | QMessageBox.Yes)
buttonY = msgBox.button(QMessageBox.Yes)
buttonY.setText('直接退出')
buttonN = msgBox.button(QMessageBox.No)
buttonN.setText('取消')
msgBox.exec_()
if msgBox.clickedButton() == buttonY:
self.Timer.Stop()
self.camera.release()
self.MsgTE.setPlainText('Exiting the application..')
self.close()
# QCloseEvent.accept()
else:
pass
除此之外,新增了個選擇界面。
這裏放界面,簡單實時傳入參數即可。
這裏參考鏈接:刷新UI界面
改進下:當點擊中止時,然後slotAdd讀取到中文字符,則break跳出循環。
def slotAdd(self):
self.LStopBt.setEnabled(True)
while True:
#test1進行測試
#原有的是接受串口數據
f=os.popen("python test1.py")
d=f.read()
str_n = d
self.listFile.addItem(str_n)
self.LMsgTE.append(str_n)
QApplication.processEvents()
time.sleep(0.1)
if self.LMsgTE.toPlainText():
str=self.LMsgTE.toPlainText().replace("\n","")
match_obj=re.match(".*?([\u4e00-\u9fa5]+).*",str)
if match_obj:
break
#中止
def breakloop(self):
self.LMsgTE.append("中止")
得到如下效果:
以上就是一些總結。