Почему у меня не работает появления объект в PyQt6?

Рейтинг: 0Ответов: 0Опубликовано: 25.02.2023

Я хотел сделать программу где при обнаружения лица он запускает анимацию.

Только вот у меня проблема что при обнаружения он в терминал выводить текст а вот объект PyQt6 не показывается.

Помогите пожалуйста!

from PyQt6.QtWidgets import *
from PyQt6.QtCore import *
from PyQt6.QtGui import *
import face_recognition
import numpy as np
import time
import cv2
import sys



class ThreadOpenCV(QThread):

    changePixmap = pyqtSignal(QImage)

    def __init__(self, source):

        super().__init__()

        self.source = source

        self.main = Main()

        self.running = True
    
    def run(self):

        cap = cv2.VideoCapture(self.source)


        image1 = face_recognition.load_image_file("./azam.jpg")
        face_encoding1 = face_recognition.face_encodings(image1, model='large')[0]


        image2 = face_recognition.load_image_file("./img2.jpg")
        face_encoding2 = face_recognition.face_encodings(image2, model='large')[0]

        known_face_encodings = [
            face_encoding1,
            face_encoding2
        ]

        self.known_face_names = [
            "Никита",
            "Дима"
        ]

        self.running = True
    
        while self.running:
            ret, frame = cap.read()


            face_locations = face_recognition.face_locations(frame)
            face_encodings = face_recognition.face_encodings(frame, face_locations)

            try:
                face_encoding = face_encodings[0]
                top, right, bottom, left = face_locations[0]
                self.matches = face_recognition.compare_faces(known_face_encodings, 
                           face_encoding)

                face_distances = face_recognition.face_distance(known_face_encodings, 
                           face_encoding)
                self.best_match_index = np.argmin(face_distances)

                if self.matches[self.best_match_index] == True:
    
                    cv2.rectangle(frame, (left, top), (right, bottom), (50, 205, 50), 2)
                    #time.sleep(15)
                    self.main.anim()

                elif self.matches[self.best_match_index] == False:

                    cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)


            except Exception as ex:
                print(ex)

            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

            h, w, ch = frame.shape
            bytes_per_line = ch * w   # PEP8: `lower_case_names` for variables
            
            image = QImage(frame.data, w, h, bytes_per_line, 
               QImage.Format.Format_RGB888)
            image = image.scaled(600, 300, Qt.AspectRatioMode.KeepAspectRatio)

            self.changePixmap.emit(image)




class Main(QWidget):
    def __init__(self):
        super().__init__()

        self.layout = QHBoxLayout(self)

        self.video = QLabel(self)
        self.video.resize(1000, 900)
        self.video.hide()

        self.textl = QLabel("Привет !!!", self)
        self.textl.hide()

        self.btn_start = QPushButton("Start")
        self.btn_start.clicked.connect(self.btn_clik)

        self.layout.addWidget(self.btn_start)
        self.layout.addWidget(self.video)
        self.layout.addWidget(self.textl)
    
    def setImage(self, image):
        self.video.setPixmap(QPixmap.fromImage(image))

    def btn_clik(self):
        self.thread_2 = ThreadOpenCV(0)
        self.thread_2.changePixmap.connect(self.setImage)
        self.thread_2.start()
        self.video.show()
        self.btn_start.hide()

   def anim(self):
       try:
           self.video.hide()
           self.textl.show()
           print("anim")
       except Exception as ex:
           print(ex)



if __name__ == "__main__":
    try:
        app = QApplication(sys.argv)
        main = Main()
        main.show()
        app.exec()
    except Exception as ex:
        print(ex)

Ответы

Ответов пока нет.