Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Python has stopped working error



  • Hi,
    i have been trying to design a simple GUI for an image processing application. I am sure that there is no problem with OpenCV functions (i tested them without using Qt). But when i put together my image processing codes with Qt and run the GUI, Pyhton is being not responsible and stops working.

    Here is my full code:

    import cv2
    import numpy as np
    from PyQt5.QtCore import QThread, QTimer
    from PyQt5.QtWidgets import QLabel, QWidget, QPushButton, QVBoxLayout, QApplication, QHBoxLayout, QMessageBox,  QMainWindow
    from PyQt5.QtGui import QPixmap, QImage
    
    
    class Camera:
    
        def __init__(self, cam_num):
    
            self.cap = cv2.VideoCapture(cam_num)
            self.cam_num = cam_num
    
        def open(self, width=640, height=480, fps=30):
    
            self.cap.set(3, width)   # set width #propID =3 yani 3.property si width. 3.property i 480 yap
            self.cap.set(4, height)  # set height
            return self.cap.isOpened()
    
        def find_contours(self):
    
            rval, frame = self.cap.read()
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            roi = frame
            hsv = cv2.cvtColor(roi, cv2.COLOR_RGB2HSV)
            lower_green = np.array([0, 0, 90])
            upper_green = np.array([255, 255, 255])
            mask = cv2.inRange(hsv, lower_green, upper_green)
            mask_inv = cv2.bitwise_not(mask)
            bg = cv2.bitwise_and(roi, roi, mask=mask)
            fg = cv2.bitwise_and(roi, roi, mask=mask_inv)
    
            gray = cv2.cvtColor(fg,cv2.COLOR_HSV2RGB)
            gray = cv2.cvtColor(gray, cv2.COLOR_RGB2GRAY)
            return gray
    
        def close_camera(self):
            self.cap.release()
    
    
    class UI_Window(QWidget):
    
        def __init__(self, cam_num):
            super().__init__()
            self.cam_num = cam_num
            print('UI')
            self.cam_num.open()
    
            # Create a timer.
            self.timer = QTimer()
            self.timer.timeout.connect(self.nextFrameSlot)
            self.timer.start(1000 / 24)
    
            # Create a layout.
            layout = QVBoxLayout()
    
            button2_layout = QHBoxLayout()
    
            btn2 = QPushButton("Acquire Frame")
            btn2.clicked.connect(self.take_photo)
            button2_layout.addWidget(btn2)
            layout.addLayout(button2_layout)
    
            # Add a label
            self.label = QLabel()
            self.label.setFixedSize(640, 640)
    
            layout.addWidget(self.label)
    
            # Set the layout
            self.setLayout(layout)
            self.setWindowTitle("First GUI with QT")
    
        def nextFrameSlot(self):
    
            frame = self.cam_num.find_contours()
            if frame is not None:
                image = QImage(frame, frame.shape[1], frame.shape[0], QImage.Format_RGB888)
                self.pixmap = QPixmap.fromImage(image)
    
        def take_photo(self):
            self.label.setPixmap(self.pixmap)
    
    
    if __name__ == '__main__':
    
        camera = Camera(0)
        app = QApplication([])
        start_window = UI_Window(camera)
        start_window.show()
        app.exit(app.exec_())
        camera.close_camera()
    

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    @hernancrespo89 said in Python has stopped working error:

    gray = cv2.cvtColor(fg,cv2.COLOR_HSV2RGB)
    gray = cv2.cvtColor(gray, cv2.COLOR_RGB2GRAY)
    return gray

    You're returning a gray image. and not an RGB888 information to get.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    @hernancrespo89 said in Python has stopped working error:

    gray = cv2.cvtColor(fg,cv2.COLOR_HSV2RGB)
    gray = cv2.cvtColor(gray, cv2.COLOR_RGB2GRAY)
    return gray

    You're returning a gray image. and not an RGB888 information to get.



  • Thanks, it solved my problem


Log in to reply