-1073740791 (0xC0000409) Error when setting Pixmap on QLabel

0

As sadly as Python/PyQt does not give give specific error messages about things like these, I am only getting this error code when running my code.

Process finished with exit code -1073740791 (0xC0000409)

I am using PyQt and OpenCV to build an app that displays a webcam feed.

import sys
import cv2
from PyQt5.QtCore import QTimer
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.uic import loadUi
from qtpy import QtGui

class MainUI(QDialog):
    read=0;
    def __init__(self):
        super(MainUI,self).__init__()
        loadUi('MainUI.ui',self)
        self.image=None
        self.btnPlay.clicked.connect(self.start_webcam)
        self.btnStop.clicked.connect(self.stop_webcam)

    def start_webcam(self):
        self.capture=cv2.VideoCapture(0)
        self.capture.set(cv2.CAP_PROP_FRAME_HEIGHT,480)
        self.capture.set(cv2.CAP_PROP_FRAME_WIDTH,640)

        self.timer=QTimer(self)
        self.timer.timeout.connect(self.update_frame())
        self.timer.start(5)

    def stop_webcam(self):
        self.timer.stop()

    def update_frame(self):
        #Take image from video feed
        ret,self.image = self.capture.read()
        self.image=cv2.flip(self.image,1)
        self.displayImage(self.image,1)

    def displayImage(self,img,window=1):
        qformat=QImage.Format_Indexed8
        if(len(img.shape)==3):  #[0]rows,[1]=cols,[2]=channels
            if img.shape[2]==4 :
                qformat=QImage.Format_RGBA8888
            else:
                qformat=QImage.Format_RGB888

        outImage=QImage(img,img.shape[1],img.shape[0],img.strides[0],qformat)
        #BGR>>RGB
        outImage = outImage.rgbSwapped()

        if window==1:
            #Problem occurs here V
            self.imgLabel.setPixmap(QPixmap.fromImage(outImage))
            self.imgLabel.setScaledContents(True)

if __name__=='__main__':
    app=QApplication(sys.argv)
    window = MainUI()
    window.setWindowTitle('PYQt Webcam feed')
    window.show()
    sys.exit(app.exec_())

I've tried using a different approach to this, and I've done my research but I really don't understand what I'm doing wrong. :(

I am studying this code from:

OpenCV Python GUI Development Tutorial 10: Display WebCam Video Feed on QLabel

https://www.youtube.com/watch?v=MUpC6z32bCA&feature=youtu.be&t=3m30s

Any help would be much appreciated. Thank you!

python
pyqt5
opencv3.0
qlabel
qpixmap
asked on Stack Overflow Aug 14, 2018 by Daene Bancud • edited Aug 14, 2018 by Daene Bancud

1 Answer

0

Your code has several errors:

  • On the line: self.timer.timeout.connect(self.update_frame()) you are passing the evaluated function, but you must pass the name of the function without the ().

  • Assuming that the above is corrected you will have another problem, each time you press btnPlay you are calling start_webcam, and in that method you are creating a new QTimer so if you press n times you could not stop it by pressing btnStop since the reference to the Previous timers have been lost.

  • The creation of the QTimer must be at the beginning, and only connect the clicked signals of the buttons to the start and stop method of the QTimer

  • On the other hand depending on the hardware the reading every 5ms does not necessarily generate an image, opencv to indicate that the reading has been correct return ret, if it is true the reading has been a success and you can just process it.

  • image is not necessary to attribute the class since you only use it in a function.


import sys
import cv2
from PyQt5 import QtCore, QtGui, QtWidgets, uic

class MainUI(QtWidgets.QDialog):
    def __init__(self):
        super(MainUI, self).__init__()
        uic.loadUi('MainUI.ui', self)

        self.capture = cv2.VideoCapture(0)
        self.capture.set(cv2.CAP_PROP_FRAME_HEIGHT,480)
        self.capture.set(cv2.CAP_PROP_FRAME_WIDTH,640)

        timer = QtCore.QTimer(self)
        timer.timeout.connect(self.update_frame)
        timer.setInterval(5)

        self.btnPlay.clicked.connect(timer.start)
        self.btnStop.clicked.connect(timer.stop)

    def update_frame(self):
        #Take image from video feed
        ret, image = self.capture.read()
        if ret:
            image=cv2.flip(image, 1)
            self.displayImage(image, 1)

    def displayImage(self, img, window=1):
        qformat = QtGui.QImage.Format_Indexed8
        if len(img.shape) == 3:  #[0]rows,[1]=cols,[2]=channels
            if img.shape[2] == 4 :
                qformat = QtGui.QImage.Format_RGBA8888
            else:
                qformat = QtGui.QImage.Format_RGB888

        outImage = QtGui.QImage(img,img.shape[1],img.shape[0],img.strides[0],qformat)
        #BGR>>RGB
        outImage = outImage.rgbSwapped()

        if window == 1:
            self.imgLabel.setPixmap(QtGui.QPixmap.fromImage(outImage))
            self.imgLabel.setScaledContents(True)

if __name__=='__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = MainUI()
    window.setWindowTitle('PYQt Webcam feed')
    window.show()
    sys.exit(app.exec_())

As sadly as Python/PyQt does not give give specific error messages about things like these, I am only getting this error code when running my code.

That problem is not Python or PyQt, the problem is your IDE, many IDEs do not have the ability to show all the errors so I recommend you run it from the CMD or terminal and there you will find the complete error message.

answered on Stack Overflow Aug 14, 2018 by eyllanesc • edited Aug 14, 2018 by eyllanesc

User contributions licensed under CC BY-SA 3.0