Advertisement
painnick

PyQt Face Detection

Oct 28th, 2024 (edited)
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.41 KB | None | 0 0
  1. import sys
  2.  
  3. import cv2
  4. import mediapipe as mp
  5. import numpy as np
  6. from PyQt5 import QtGui
  7. from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QThread
  8. from PyQt5.QtGui import QPixmap
  9. from PyQt5.QtWidgets import QWidget, QApplication, QLabel, QVBoxLayout, QPushButton
  10.  
  11. find_faces = False
  12.  
  13. class VideoThread(QThread):
  14.   change_pixmap_signal = pyqtSignal(np.ndarray)
  15.  
  16.   def run(self):
  17.  
  18.     global find_faces
  19.  
  20.     mp_face_detection = mp.solutions.face_detection
  21.     mp_drawing = mp.solutions.drawing_utils
  22.  
  23.     # capture from web cam
  24.     cap = cv2.VideoCapture(0)
  25.     with mp_face_detection.FaceDetection(model_selection=0, min_detection_confidence=0.5) as face_detection:
  26.       while cap.isOpened():
  27.         success, image = cap.read()
  28.         if not success:
  29.           print("Ignoring empty camera frame.")
  30.           # If loading a video, use 'break' instead of 'continue'.
  31.           continue
  32.  
  33.         if find_faces:
  34.           # To improve performance, optionally mark the image as not writeable to
  35.           # pass by reference.
  36.           image.flags.writeable = False
  37.           image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  38.           results = face_detection.process(image)
  39.  
  40.           # Draw the face detection annotations on the image.
  41.           image.flags.writeable = True
  42.           image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
  43.           if results.detections:
  44.             for detection in results.detections:
  45.               mp_drawing.draw_detection(image, detection)
  46.  
  47.         image = cv2.flip(image, 1)
  48.         self.change_pixmap_signal.emit(image)
  49.  
  50.  
  51. class App(QWidget):
  52.   def __init__(self):
  53.     super().__init__()
  54.     self.setWindowTitle("Qt live label demo")
  55.     self.disply_width = 640
  56.     self.display_height = 480
  57.     # create the label that holds the image
  58.     self.image_label = QLabel(self)
  59.     self.image_label.resize(self.disply_width, self.display_height)
  60.     # create a text label
  61.     self.textLabel = QLabel('Webcam')
  62.     # create find button
  63.     self.find_button = QPushButton("Find")
  64.     self.find_button.clicked.connect(self.find_button_clicked)
  65.  
  66.     # create a vertical box layout and add the two labels
  67.     vbox = QVBoxLayout()
  68.     vbox.addWidget(self.image_label)
  69.     vbox.addWidget(self.textLabel)
  70.     vbox.addWidget(self.find_button)
  71.     # set the vbox layout as the widgets layout
  72.     self.setLayout(vbox)
  73.  
  74.     # create the video capture thread
  75.     self.thread = VideoThread()
  76.     # connect its signal to the update_image slot
  77.     self.thread.change_pixmap_signal.connect(self.update_image)
  78.     # start the thread
  79.     self.thread.start()
  80.  
  81.   @pyqtSlot(np.ndarray)
  82.   def update_image(self, cv_img):
  83.     """Updates the image_label with a new opencv image"""
  84.     qt_img = self.convert_cv_qt(cv_img)
  85.     self.image_label.setPixmap(qt_img)
  86.  
  87.   def convert_cv_qt(self, cv_img):
  88.     """Convert from an opencv image to QPixmap"""
  89.     rgb_image = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB)
  90.     h, w, ch = rgb_image.shape
  91.     bytes_per_line = ch * w
  92.     convert_to_Qt_format = QtGui.QImage(rgb_image.data, w, h, bytes_per_line, QtGui.QImage.Format_RGB888)
  93.     p = convert_to_Qt_format.scaled(self.disply_width, self.display_height, Qt.KeepAspectRatio)
  94.     return QPixmap.fromImage(p)
  95.  
  96.   def find_button_clicked(self):
  97.     global find_faces
  98.     find_faces = not find_faces
  99.  
  100.  
  101. if __name__ == "__main__":
  102.   app = QApplication(sys.argv)
  103.   a = App()
  104.   a.show()
  105.   sys.exit(app.exec_())
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement