Vi liễu ngận tiến nhất bộ đích tiếp cận hạng mục, ngã dụng pyqtf tiến hành hạng mục đích phong trạng, phương tiện dĩ hậu quản lý.
1, an trang 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-sip
0upgraded,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]y
Get: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
Kiểm tra thị phủ an trang thành công:
forlinx@ok3588:~$ python3
Python3.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()
Đại mã:
importsys
importos
importcv2
importnumpyasnp
fromPyQt5importQtCore, QtGui, QtWidgets
fromPyQt5.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.363
l2_similarity_threshold =1.128
defGets_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_list
classUi_MainWindow(QtWidgets.QWidget):
def__init__(self, parent=None):
super(Ui_MainWindow, self).__init__(parent)
self.CAM_NUM =74
self.label_show_camera =None
self.label_move =None
self.button_close =None
self.button_open_camera =None
self.__layout_data_show =None
self.__layout_fun_button =None
self.__layout_main =None
self.image =None
self.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 =0
self.x =0
self.count =0
defset_ui(self):
self.__layout_main = QtWidgets.QHBoxLayout()
self.__layout_fun_button = QtWidgets.QHBoxLayout()
self.__layout_data_show = QtWidgets.QVBoxLayout
self.button_open_camera = QtWidgets.QPushButton(u' đả khai tương cơ ')
self.button_close = QtWidgets.QPushButton(u' thối xuất ')
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 "Nhiếp tượng đầu")
"""
# thiết trí bối cảnh nhan sắc
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 "Thỉnh kiểm trắc tương dữ điện não thị phủ liên tiếp chính xác",
buttons=QtWidgets.QMessageBox.Ok,
defaultButton=QtWidgets.QMessageBox.Ok)
else:
self.timer_camera.start(30)
self.button_open_camera.setText(u "Quan bế tương cơ")
else:
self.timer_camera.stop()
self.cap.release()
self.label_show_camera.clear()
self.button_open_camera.setText(u' đả khai tương cơ ')
defshow_camera(self):
flag, show = self.cap.read()
ifflagisnotTrue:
print(' nhiếp tượng đầu vị đả khai ')
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 =1
elifl2_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 =1
showImage = 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 "Quan bế")
ok = QtWidgets.QPushButton()
cancel = QtWidgets.QPushButton()
msg = QtWidgets.QMessageBox(QtWidgets.QMessageBox,u "Quan bế",u "Thị phủ quan bế")
msg.addButton(ok, QtWidgets.QMessageBox.ActionRole)
msg.addButton(cancel, QtWidgets.QMessageBox.RejectRole)
ok.setText(u "Xác định")
cancel.setText(u "Thủ tiê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_())
Thật trắc hiệu quả
![49890e9f27e7ece6968c0da54beb1dd.jpg](https://file1.elecfans.com/web2/M00/82/01/wKgZomQyXGCAYT0tAAHKkAf39vA374.jpg)