本文将会把OpenCV-Python中人脸检测算法模型推理时相关的参数全部界面化,实现用户从界面选择文件,完成人脸检测操作。
这过程中主要借助了已经介绍的这些组件元素。设计好的界面如下图:
选择一张图像或者视频文件之后,点击【运行】按钮,执行如下:
人脸检测界面程序
程序实现从界面传参到算法执行更新界面显示的整个流程,其中人脸检测相关OpenCV代码实现可以参考这里:
https://www.bilibili.com/video/BV1hM4y1M7vQ/界面构建的UI类代码如下:
1from PyQt5 import QtWidgets, QtCore, QtGui 2import sys 3import cv2 as cv 4 5 6class FaceDetectDemoPanel(QtWidgets.QWidget): 7 def __init__(self, parent=None): 8 super().__init__(parent) 9 # 文本标签 10 self.weight_file_path = QtWidgets.QLineEdit() 11 self.weight_file_path.setMinimumWidth(150) 12 self.weight_file_path.setEnabled(False) 13 self.weight_file_path.setText("D:/projects/face_detector/opencv_face_detector_uint8.pb") 14 self.config_file_path = QtWidgets.QLineEdit() 15 self.config_file_path.setMinimumWidth(150) 16 self.config_file_path.setEnabled(False) 17 self.config_file_path.setText("D:/projects/face_detector/opencv_face_detector.pbtxt") 18 self.weight_select_btn = QtWidgets.QPushButton("浏览...") 19 self.config_file_btn = QtWidgets.QPushButton("浏览...") 20 hbox_layout1 = QtWidgets.QHBoxLayout() 21 hbox_layout1.addWidget(QtWidgets.QLabel("权重:")) 22 hbox_layout1.addWidget(self.weight_file_path) 23 hbox_layout1.addWidget(self.weight_select_btn) 24 hbox_layout1.addWidget(QtWidgets.QLabel("配置:")) 25 hbox_layout1.addWidget(self.config_file_path) 26 hbox_layout1.addWidget(self.config_file_btn) 27 28 panel1 = QtWidgets.QGroupBox("模型") 29 panel1.setLayout(hbox_layout1) 30 31 self.spinbox1 = QtWidgets.QDoubleSpinBox() 32 self.spinbox1.setRange(0.0, 1.0) 33 self.spinbox1.setSingleStep(0.01) 34 self.spinbox1.setValue(0.25) 35 self.fps_chkbox = QtWidgets.QCheckBox("显示FPS") 36 self.score_chkbox = QtWidgets.QCheckBox("显示置信") 37 self.fps_chkbox.setChecked(True) 38 self.score_chkbox.setChecked(True) 39 40 hbox_layout2 = QtWidgets.QHBoxLayout() 41 hbox_layout2.addWidget(self.fps_chkbox) 42 hbox_layout2.addWidget(self.score_chkbox) 43 hbox_layout2.addWidget(QtWidgets.QLabel("置信阈值:")) 44 hbox_layout2.addWidget(self.spinbox1) 45 46 panel2 = QtWidgets.QGroupBox("参数与显示") 47 panel2.setLayout(hbox_layout2) 48 49 self.rbtn1 = QtWidgets.QRadioButton("图像") 50 self.rbtn2 = QtWidgets.QRadioButton("视频") 51 self.rbtn3 = QtWidgets.QRadioButton("WebCam") 52 self.rbtn1.setChecked(True) 53 self.image_file_path = QtWidgets.QLineEdit() 54 self.image_file_path.setEnabled(False) 55 self.browser_btn = QtWidgets.QPushButton("选择...") 56 57 hbox_layout3 = QtWidgets.QHBoxLayout() 58 hbox_layout3.addWidget(self.rbtn1) 59 hbox_layout3.addWidget(self.rbtn2) 60 hbox_layout3.addWidget(self.rbtn3) 61 hbox_layout3.addWidget(QtWidgets.QLabel("文件路径:")) 62 hbox_layout3.addWidget(self.image_file_path) 63 hbox_layout3.addWidget(self.browser_btn) 64 65 panel3 = QtWidgets.QGroupBox("数据源") 66 panel3.setLayout(hbox_layout3) 67 68 panel4 = QtWidgets.QWidget() 69 vbox_layout4 = QtWidgets.QVBoxLayout() 70 vbox_layout4.addWidget(panel1) 71 vbox_layout4.addWidget(panel2) 72 vbox_layout4.addWidget(panel3) 73 panel4.setLayout(vbox_layout4) 74 75 # 输入文本框 76 self.label = QtWidgets.QLabel() 77 pixmap = QtGui.QPixmap("images/16.jpg") 78 pix = pixmap.scaled(QtCore.QSize(620, 500), QtCore.Qt.KeepAspectRatio) 79 self.label.setPixmap(pix) 80 self.label.setAlignment(QtCore.Qt.AlignCenter) 81 self.label.setStyleSheet("background-color:black; color: green") 82 83 self.run_btn = QtWidgets.QPushButton("运行") 84 self.run_btn.setMaximumWidth(100) 85 86 # 添加到布局管理器中 87 vbox_layout = QtWidgets.QVBoxLayout() 88 vbox_layout.addWidget(panel4) 89 vbox_layout.addWidget(self.run_btn) 90 vbox_layout.addWidget(self.label) 91 vbox_layout.addStretch(1) 92 93 # 面板容器 94 self.setLayout(vbox_layout) 95 96 # setup listener 97 self.weight_select_btn.clicked.connect(self.on_weight_select) 98 self.config_file_btn.clicked.connect(self.on_config_select) 99 self.browser_btn.clicked.connect(self.on_image_select) 100 101 # setup listener 102 self.rbtn1.toggled.connect(self.on_select_changed) 103 self.rbtn2.toggled.connect(self.on_select_changed) 104 self.rbtn3.toggled.connect(self.on_select_changed) 105 106 self.run_btn.clicked.connect(self.on_face_detect)
总结
本文主要是演示了PyQT5的常见组件综合运用与OpenCV人脸检测算法与PyQT5界面库的结合开发,实现简单的人脸检测界面程序。
审核编辑:刘清
全部0条评论
快来发表一下你的评论吧 !