[Python] PyQt5 live image in the main window

Discussão em 'Python' iniciado por Stack, Fevereiro 14, 2025.

  1. Stack

    Stack Membro Participativo

    I'm making an application to control a camera. I have the main window where the live image of what the camera is capturing will be shown. However, I have the start and end button in another window that is built in QWidget. In the QWidget window the image appears, I tried to make a logic to send it to the Main View but so far without effect. Does anyone know what my problem might be? Thank you all!

    Camera Class

    class CameraControl(QWidget):
    frame_ready = pyqtSignal(QImage)
    def __init__(self, update_callback=None):
    super().__init__()
    self.update_callback = update_callback
    self.init_ui()
    def init_ui(self):
    ------Code--------
    def start_camera(self):
    ------Code--------
    def stop_camera(self):
    ------Code--------
    def update_frame(self):
    image, status = self.stream.wait()
    if status == cvb.WaitStatus.Ok:
    frame = cvb.as_array(image, copy=True)
    # Normalize if 16-bit
    if frame.dtype == np.uint16:
    frame = cv2.normalize(frame, None, 0, 255,
    cv2.NORM_MINMAX).astype(np.uint8)
    # Resize for display
    frame = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5,
    interpolation=cv2.INTER_AREA)
    # Convert to QImage format
    h, w = frame.shape
    qt_image = QImage(frame.data, w, h, w, QImage.Format_Grayscale8)
    if self.update_callback:
    print("[DEBUG] frame_ready signal emitted!")
    self.frame_ready.emit(qt_image)


    Class Main Window

    class MainWindow(QMainWindow):
    def __init__(self):
    super().__init__()
    self.camera_control = CameraControl(self.update_video_feed)
    self.camera_control.frame_ready.connect(self.update_video_feed)
    self.initUI()
    def initUI(self):
    # Video Feed Labe
    self.video_label = QLabel(self)
    self.video_label.setAlignment(QtCore.Qt.AlignCenter)
    self.video_label.setFixedSize(640, 480) # Default size
    self.layout.addWidget(self.video_label, 1, 1, 2, 3)
    ------Code--------
    def update_video_feed(self, image):
    if image.isNull():
    print("[ERROR] Received an invalid image!")
    return
    print("[DEBUG] Displaying frame in main window")
    self.video_label.setPixmap(QtGui.QPixmap.fromImage(image))
    self.video_label.repaint()


    The print print("[DEBUG] Displaying frame in main window") works, it's sending it to the main window, it's just not displaying it.

    Continue reading...

Compartilhe esta Página