Monday, 28 March 2022

I am trying to display video stream from opencv to pyqt5 interface but my code is not working

I am trying to display video stream from opencv to pyqt5 interface. I adapted the code from the answer to one question - https://stackoverflow.com/questions/44404349/pyqt-showing-video-stream-from-opencv#:~:text=The%20problem%20is,label.%20For%20example %3A. But when I run my code in pycharm, I get this error:

Process finished with exit code -1073740791 (0xC0000409)

Here is my code:

class Gests_Recognition(QtCore.QObject):

    def __init__(self):
        super(Gests_Recognition, self).__init__()
        self.running = True

    change_pixmap = pyqtSignal(QImage)

    gest_map = {
        0: "up",
        1: "down",
        2: "right",
        3: "left",
        4: "forward",
        5: "back"
    }

    columns = ['x11', 'x21', 'x12', 'x22', 'x13', 'x23', 'x14', 'x24', 'x15', 'x25',
               'x16', 'x26', 'x17', 'x27', 'x18', 'x28', 'x19', 'x29', 'x110', 'x210', 'x111',
               'x211', 'x112', 'x212', 'x113', 'x213', '114', '214', '115', 'x215', 'x116',
               'x216', 'x117', 'x217', 'x118', 'x218', 'x119', 'x219', 'x120', 'x220', 'x121',
               'x221']

    def mapper(self, val):
        return Gests_Recognition.gest_map[val]

    def run(self):
        model = load_model("gestures_model.h5", compile=False)
        drawingModule = mediapipe.solutions.drawing_utils
        handsModule = mediapipe.solutions.hands

        capture = cv2.VideoCapture(0)

        with handsModule.Hands(static_image_mode=False, min_detection_confidence=0.7, min_tracking_confidence=0.7,
                               max_num_hands=1) as hands:
            while self.running:
                # frame == 480 640
                ret, frame = capture.read()
                if ret:
                    processed_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                    height_frame, width_frame, channels_frame = frame.shape
                    bytes_line = channels_frame * width_frame
                    qt_frame = QImage(processed_frame.data, width_frame, height_frame, bytes_line, QImage.Format_RGB888)
                    qt_frame = qt_frame.scaled(640, 480, QtCore.Qt.KeepAspectRatio)

                    self.change_pixmap.emit(qt_frame)

                    results = hands.process(processed_frame)

                    cv2.imshow('Hands recognizer', frame)
                    k = cv2.waitKey(1)

                    # do something else.

            cv2.destroyAllWindows()
            capture.release()

class Ui_MainWindow(object):

    @pyqtSlot(QImage)
    def set_image(self, image):
        self.label.setPixmap(QPixmap.fromImage(image))

    def setupUi(self, MainWindow):
        self.cam_btn = QtWidgets.QPushButton(self.centralwidget)
        self.cam_btn.setGeometry(QtCore.QRect(280, 60, 160, 150))
        self.cam_btn.setObjectName("cam_btn")
        self.cam_btn.setCheckable(True)
        self.cam_btn.clicked.connect(self.gest_recognition)
        self.cam_btn.setStyleSheet("QPushButton{background-color: #aae053;\n"
                                        "border-radius: 60%;\nbackground-image: url('images/hand.png');\n"
                                        "background-repeat: no-repeat;\nbackground-position: center;}\n"
                                        "QPushButton:hover{background-color: #81eb3b;}")

        self.label = QLabel(self.centralwidget)
        self.label.move(40, 240)
        self.label.resize(640, 480)
        self.label.setStyleSheet("border: 5px solid black;")

    def gest_recognition(self):
        if self.cam_btn.isChecked():
            self.cam_btn.setStyleSheet("QPushButton{background-color: red;\n"
                                       "border-radius: 60%;\nbackground-image: url('images/pause.png');\n"
                                       "background-repeat: no-repeat;\nbackground-position: center;}\n")
            self.thread_gests = QtCore.QThread()
            self.g_recog = Gests_Recognition()

            self.g_recog.moveToThread(self.thread_gests)
            self.g_recog.change_pixmap.connect(self.set_image)
            self.thread_gests.start()
        else:
            self.cam_btn.setStyleSheet("QPushButton{background-color: #aae053;\n"
                                       "border-radius: 60%;\nbackground-image: url('images/hand.png');\n"
                                       "background-repeat: no-repeat;\nbackground-position: center;}\n"
                                       "QPushButton:hover{background-color: #81eb3b;}")
            self.g_recog.running = False
            self.thread_gests.terminate()


from I am trying to display video stream from opencv to pyqt5 interface but my code is not working

No comments:

Post a Comment