Advertisement
painnick

PyQt Face Detection 2

Oct 28th, 2024 (edited)
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 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, uic
  7. from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QThread
  8. from PyQt5.QtGui import QPixmap
  9. from PyQt5.QtWidgets import QApplication, QLabel, QVBoxLayout, QPushButton, QDialog
  10.  
  11. find_faces = False
  12.  
  13.  
  14. class VideoThread(QThread):
  15.   change_pixmap_signal = pyqtSignal(np.ndarray)
  16.  
  17.   def run(self):
  18.  
  19.     global find_faces
  20.  
  21.     mp_face_detection = mp.solutions.face_detection
  22.     mp_drawing = mp.solutions.drawing_utils
  23.  
  24.     # capture from web cam
  25.     cap = cv2.VideoCapture(0)
  26.     with mp_face_detection.FaceDetection(model_selection=0, min_detection_confidence=0.5) as face_detection:
  27.       while cap.isOpened():
  28.         success, image = cap.read()
  29.         if not success:
  30.           print("Ignoring empty camera frame.")
  31.           # If loading a video, use 'break' instead of 'continue'.
  32.           continue
  33.  
  34.         if find_faces:
  35.           # To improve performance, optionally mark the image as not writeable to
  36.           # pass by reference.
  37.           image.flags.writeable = False
  38.           image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  39.           results = face_detection.process(image)
  40.  
  41.           # Draw the face detection annotations on the image.
  42.           image.flags.writeable = True
  43.           image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
  44.           if results.detections:
  45.             for detection in results.detections:
  46.               mp_drawing.draw_detection(image, detection)
  47.  
  48.         image = cv2.flip(image, 1)
  49.         self.change_pixmap_signal.emit(image)
  50.  
  51.  
  52. form_class = uic.loadUiType("face_detection.ui")[0]
  53.  
  54.  
  55. class App(QDialog, form_class):
  56.   def __init__(self):
  57.     super().__init__()
  58.     self.setupUi(self)
  59.  
  60.     self.find_button.clicked.connect(self.find_button_clicked)
  61.  
  62.     # create the video capture thread
  63.     self.thread = VideoThread()
  64.     # connect its signal to the update_image slot
  65.     self.thread.change_pixmap_signal.connect(self.update_image)
  66.     # start the thread
  67.     self.thread.start()
  68.  
  69.   @pyqtSlot(np.ndarray)
  70.   def update_image(self, cv_img):
  71.     """Updates the image_label with a new opencv image"""
  72.     qt_img = self.convert_cv_qt(cv_img)
  73.     self.image_label.setPixmap(qt_img)
  74.  
  75.   def convert_cv_qt(self, cv_img):
  76.     """Convert from an opencv image to QPixmap"""
  77.     rgb_image = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB)
  78.     h, w, ch = rgb_image.shape
  79.     bytes_per_line = ch * w
  80.     convert_to_Qt_format = QtGui.QImage(rgb_image.data, w, h, bytes_per_line, QtGui.QImage.Format_RGB888)
  81.     p = convert_to_Qt_format.scaled(800, 600, Qt.KeepAspectRatio)
  82.     return QPixmap.fromImage(p)
  83.  
  84.   def find_button_clicked(self):
  85.     global find_faces
  86.     find_faces = not find_faces
  87.  
  88.  
  89. if __name__ == "__main__":
  90.   app = QApplication(sys.argv)
  91.   a = App()
  92.   a.show()
  93.   sys.exit(app.exec_())
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement