基于pyqt5的人脸识别
时间: 2023-10-11 19:08:37 浏览: 98
基于PyQt5的人脸识别可以通过结合OpenCV和dlib等库来实现。下面是一个简单的示例代码:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt5.QtGui import QImage, QPixmap
import cv2
import dlib
class FaceRecognition(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Face Recognition')
self.video = cv2.VideoCapture(0)
self.detector = dlib.get_frontal_face_detector()
self.label = QLabel(self)
self.layout = QVBoxLayout(self)
self.layout.addWidget(self.label)
self.setLayout(self.layout)
def detect_faces(self, frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = self.detector(gray)
for face in faces:
x, y, w, h = face.left(), face.top(), face.width(), face.height()
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
return frame
def start_video(self):
ret, frame = self.video.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = self.detect_faces(frame)
image = QImage(frame.data, frame.shape
阅读全文