为了很进一步的接近项目,我用pyqtf进行项目的封状,方便以后管理。
1、安装pyqt5
forlinx@ok3588:~$ sudo apt install python3-pyqt5 Readingpackagelists... Done Building dependency tree Readingstateinformation... Done The following additional packages will be installed: libqt5designer5 libqt5help5 python3-sip Suggested packages: python3-pyqt5-dbg The following NEW packages will be installed: libqt5designer5 libqt5help5 python3-pyqt5 python3-sip0upgraded,4newly installed,0to removeand3notupgraded. Need to get5043kB of archives. After this operation,22.9MB of additional disk space will be used. Do you want tocontinue? [Y/n]yGet:1http://mirrors.163.com/ubuntu-ports focal/universe arm64 libqt5designer5 arm645.12.8-0ubuntu1 [2721kB] Get:2http://mirrors.163.com/ubuntu-ports focal/universe arm64 libqt5help5 arm645.12.8-0ubuntu1 [125kB] Get:3http://mirrors.163.com/ubuntu-ports focal/universe arm64 python3-sip arm644.19.21+dfsg-1build1 [85.7kB] Get:4http://mirrors.163.com/ubuntu-ports focal/universe arm64 python3-pyqt5 arm645.14.1+dfsg-3build1 [2112kB] Fetched5043kB in1min1s (82.3kB/s) Selecting previously unselectedpackagelibqt5designer5:arm64. (Reading database ...58475filesanddirectories currently installed.) Preparing tounpack.../libqt5designer5_5.12.8-0ubuntu1_arm64.deb ... Unpacking libqt5designer5:arm64 (5.12.8-0ubuntu1) ... Selecting previously unselectedpackagelibqt5help5:arm64. Preparing tounpack.../libqt5help5_5.12.8-0ubuntu1_arm64.deb ... Unpacking libqt5help5:arm64 (5.12.8-0ubuntu1) ... Selecting previously unselectedpackagepython3-sip. Preparing tounpack.../python3-sip_4.19.21+dfsg-1build1_arm64.deb ... Unpacking python3-sip (4.19.21+dfsg-1build1) ... Selecting previously unselectedpackagepython3-pyqt5. Preparing tounpack.../python3-pyqt5_5.14.1+dfsg-3build1_arm64.deb ... Unpacking python3-pyqt5 (5.14.1+dfsg-3build1) ... Setting up libqt5designer5:arm64 (5.12.8-0ubuntu1) ... Setting up libqt5help5:arm64 (5.12.8-0ubuntu1) ... Setting up python3-sip (4.19.21+dfsg-1build1) ... Setting up python3-pyqt5 (5.14.1+dfsg-3build1) ... Processing triggersforlibc-bin (2.31-0ubuntu9) ... /sbin/ldconfig.real:/lib/aarch64-linux-gnu/libvirtualkeyboard.so.1isnota symboliclink
检查是否安装成功:
forlinx@ok3588:~$ python3Python3.8.2(default, Mar132020,10:14:16)[GCC 9.2.1 20200306] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import PyQt5 >>> quit()
代码:
importsysimportosimportcv2importnumpyasnpfromPyQt5importQtCore, QtGui, QtWidgetsfromPyQt5.QtWidgetsimport*fromPyQt5.QtCoreimport*fromPyQt5.QtGuiimportQPalette, QBrush, QPixmap face_detection_model ="../models/yunet.onnx"face_recognition_model ="../models/face_recognizer_fast.onnx"images_path ="../images"faceDetector = cv2.FaceDetectorYN.create(face_detection_model,"", input_size=(640,480))recognizer = cv2.FaceRecognizerSF.create(face_recognition_model,"")cosine_similarity_threshold =0.363l2_similarity_threshold =1.128defGets_Facial_Features(images_path):images_feature_list = [] ID_list = []forimage_nameinos.listdir(images_path): ID_list.append(image_name.split('.')[0]) image = cv2.imread(images_path +'/'+ image_name) faces1 = faceDetector.detect(image)aligned_face1 = recognizer.alignCrop(image, faces1[1][0])image_feature = recognizer.feature(aligned_face1) images_feature_list.append(image_feature)returnID_list, images_feature_listclassUi_MainWindow(QtWidgets.QWidget):def__init__(self, parent=None):super(Ui_MainWindow, self).__init__(parent) self.CAM_NUM =74self.label_show_camera =Noneself.label_move =Noneself.button_close =Noneself.button_open_camera =Noneself.__layout_data_show =Noneself.__layout_fun_button =Noneself.__layout_main =Noneself.image =Noneself.timer_camera = QtCore.QTimer()self.cap = cv2.VideoCapture(self.CAM_NUM)self.cap.set(3,640) self.cap.set(4,480) self.ID_list, self.images_feature_list = Gets_Facial_Features(images_path) self.set_ui() self.slot_init() self.__flag_work =0self.x =0self.count =0defset_ui(self):self.__layout_main = QtWidgets.QHBoxLayout()self.__layout_fun_button = QtWidgets.QHBoxLayout() self.__layout_data_show = QtWidgets.QVBoxLayoutself.button_open_camera = QtWidgets.QPushButton(u'打开相机') self.button_close = QtWidgets.QPushButton(u'退出')button_color = [self.button_open_camera, self.button_close]foriinrange(2): button_color[i].setStyleSheet("QPushButton{color:black}""QPushButton:hover{color:red}""QPushButton{background-color:rgb(78,255,255)}""QpushButton{border:2px}""QPushButton{border_radius:10px}""QPushButton{padding:2px 4px}") self.button_open_camera.setMinimumHeight(50) self.button_close.setMinimumHeight(50)self.move(500,500)self.label_show_camera = QtWidgets.QLabel() self.label_move = QtWidgets.QLabel() self.label_move.setFixedSize(100,100) self.label_show_camera.setFixedSize(641,481) self.label_show_camera.setAutoFillBackground(False) self.__layout_fun_button.addWidget(self.button_open_camera) self.__layout_fun_button.addWidget(self.button_close) self.__layout_fun_button.addWidget(self.label_move) self.__layout_main.addLayout(self.__layout_fun_button) self.__layout_main.addWidget(self.label_show_camera) self.setLayout(self.__layout_main) self.label_move.raise_()self.setWindowTitle(u"摄像头")""" # 设置背景颜色 palettel1 = QPalette() palettel1.setBrush(self.backgroundRole(), QBrush(QPixmap('background.jpg'))) self.setPalette(palettel1) """defslot_init(self):self.button_open_camera.clicked.connect(self.button_open_camera_click) self.timer_camera.timeout.connect(self.show_camera) self.button_close.clicked.connect(self.close)defbutton_open_camera_click(self):ifnotself.timer_camera.isActive(): flag = self.cap.open(self.CAM_NUM)ifnotflag: msg = QtWidgets.QMessageBox.Warning(self,u"Warning",u"请检测相与电脑是否连接正确", buttons=QtWidgets.QMessageBox.Ok, defaultButton=QtWidgets.QMessageBox.Ok)else: self.timer_camera.start(30) self.button_open_camera.setText(u"关闭相机")else: self.timer_camera.stop() self.cap.release() self.label_show_camera.clear() self.button_open_camera.setText(u'打开相机')defshow_camera(self):flag, show = self.cap.read()ifflagisnotTrue:print('摄像头未打开')else:frame_faces = faceDetector.detect(show)ifframe_faces[1]isnotNone:foridx, faceinenumerate(frame_faces[1]): coords = face[:-1].astype(np.int32) cv2.rectangle(show, (coords[0], coords[1]), (coords[0] + coords[2], coords[1] + coords[3]), (255,0,0), thickness=2) aligned_face = recognizer.alignCrop(show, frame_faces[1][idx]) frame_feature = recognizer.feature(aligned_face)forindex, image_featureinenumerate(self.images_feature_list): cosine_score = recognizer.match(frame_feature, image_feature,0) l2_score = recognizer.match(frame_feature, image_feature,1)ifcosine_score >= cosine_similarity_threshold: cv2.putText(show, self.ID_list[index], (coords[0] +5, coords[1] -5), cv2.FONT_HERSHEY_SIMPLEX,1, (0,255,0),2) i =1elifl2_score <= l2_similarity_threshold: cv2.putText(show, self.ID_list[index], (coords[0] +5, coords[1] -5), cv2.FONT_HERSHEY_SIMPLEX,1, (0,255,0),2) i =1showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0], QtGui.QImage.Format_RGB888) self.label_show_camera.setPixmap(QtGui.QPixmap.fromImage(showImage))defcloseEvent(self, event):print(u"关闭") ok = QtWidgets.QPushButton() cancel = QtWidgets.QPushButton() msg = QtWidgets.QMessageBox(QtWidgets.QMessageBox,u"关闭",u"是否关闭") msg.addButton(ok, QtWidgets.QMessageBox.ActionRole) msg.addButton(cancel, QtWidgets.QMessageBox.RejectRole) ok.setText(u"确定") cancel.setText(u"取消")ifmsg.exec_() == QtWidgets.QMessageBox.RejectRole: event.ignore()else:ifself.cap.isOpened(): self.cap.release()ifself.timer_camera.isActive(): self.timer_camera.stop() event.accept()if__name__ =='__main__': App = QApplication(sys.argv) win = Ui_MainWindow() win.show() sys.exit(App.exec_())
实测效果