Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Python has stopped working error
QtWS25 Last Chance

Python has stopped working error

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 627 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hernancrespo89
    wrote on last edited by hernancrespo89
    #1

    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()
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • H Offline
        H Offline
        hernancrespo89
        wrote on last edited by
        #3

        Thanks, it solved my problem

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved