Pyqt crashes when trying to show opencv videostreamPyQt showing video stream from opencvSegmentation fault and crashing when trying to import opencvPyQt crashing when trying to round variable in labelskeletonization in videostream with opencv and pythonRunning into issues with PyOpenGL and PyQt5 on OSXPyQt showing video stream from opencvThe screen recording time is inconsistent with the actual use time in PyQt with opencvGet videoStream from RTMP to opencvopencv imutils resize videostreamHow to show image to PYQT with opencvDynamically add rows and columns in QTable in pyQT5

Why are Marine Le Pen's possible connections with Steve Bannon something worth investigating?

What's is the easiest way to purchase a stock and hold it

Could a chemically propelled craft travel directly between Earth and Mars spaceports?

How to make labels automatically scale in size to fit between 2 coordinates in tikz?

Why does snapping your fingers activate the Infinity Gauntlet?

Can 2 light bulbs of 120V in series be used on 230V AC?

Can the word crowd refer to just 10 people?

How does the probability of events change if an event does not occur

Is it wise to pay off mortgage with 401k?

Why does string strummed with finger sound different from the one strummed with pick?

Bash Read: Reading comma separated list, last element is missed

Why does Taylor’s series “work”?

Gambler's Fallacy Dice

Precedent for disabled Kings

Very serious stuff - Salesforce bug enabled "Modify All"

What were the "pills" that were added to solid waste in Apollo 7?

Find the 3D region containing the origin bounded by given planes

Why is python script running in background consuming 100 % CPU?

On a piano, are the effects of holding notes and the sustain pedal the same for a single chord?

How do I unravel apparent recursion in an edef statement?

Why would Thor need to strike a building with lightning to attack enemies?

Is my company merging branches wrong?

How to safely discharge oneself

Isn't Kirchhoff's junction law a violation of conservation of charge?



Pyqt crashes when trying to show opencv videostream


PyQt showing video stream from opencvSegmentation fault and crashing when trying to import opencvPyQt crashing when trying to round variable in labelskeletonization in videostream with opencv and pythonRunning into issues with PyOpenGL and PyQt5 on OSXPyQt showing video stream from opencvThe screen recording time is inconsistent with the actual use time in PyQt with opencvGet videoStream from RTMP to opencvopencv imutils resize videostreamHow to show image to PYQT with opencvDynamically add rows and columns in QTable in pyQT5






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I tried code from this answer and it crashes with error Process finished with exit code -1073740771 (0xC000041D) after some time (2-10 sec.) and sometimes with 0xC0000005. It crashes immediately if I try to drag the window.
However when I put time.sleep(0.1) in run it works fine. If I use sleeps shorter than 0.1 it crashes again.



from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QLabel,QMessageBox
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import QThread, pyqtSignal, pyqtSlot, Qt
import cv2
import sys
import time

class CamThread(QThread):
changemap = pyqtSignal('QImage')

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

while True:
ret, img_rgb = cap.read()
if ret:
self.rgb = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2RGB)
self.convert = QImage(self.rgb.data, self.rgb.shape[1], self.rgb.shape[0], QImage.Format_RGB888)
self.p = self.convert.scaled(640, 480, Qt.KeepAspectRatio)
self.changemap.emit(self.p)
#time.sleep(0.1)


class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'webcam'

self.initUI()

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

def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(100, 100, 640, 480)
self.resize(640, 480)
self.label = QLabel(self)
self.label.resize(640, 480)
thr = CamThread(self)
thr.changemap.connect(self.setImage)
thr.start()

app = QApplication(sys.argv)
win = App()
#win.setAttribute(Qt.WA_DeleteOnClose, True)
win.show()
app.exit(app.exec_())


I thought that the problem is somewhere in signals/slots but haven't been able to find anything relevant.



  • Windows 10


  • Python - 3.7


  • Pyqt - 5.12


  • OpenCV - 3.4.5.20










share|improve this question
























  • change self.rgb to rgb, self.convert to convert and self.p to p

    – eyllanesc
    Mar 22 at 19:44











  • Tried. Changes nothing

    – Mur4al
    Mar 22 at 19:47











  • execute it in the CMD, there you should surely give you more information

    – eyllanesc
    Mar 22 at 19:49











  • Still crushes. Shows nothing in CMD not even previous error.

    – Mur4al
    Mar 22 at 19:58












  • mmm, why do you want an image beyond 100ms ?, a camera provides a 30FPS image that is 30 ms, what is the FPS of your camera?

    – eyllanesc
    Mar 22 at 19:59

















0















I tried code from this answer and it crashes with error Process finished with exit code -1073740771 (0xC000041D) after some time (2-10 sec.) and sometimes with 0xC0000005. It crashes immediately if I try to drag the window.
However when I put time.sleep(0.1) in run it works fine. If I use sleeps shorter than 0.1 it crashes again.



from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QLabel,QMessageBox
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import QThread, pyqtSignal, pyqtSlot, Qt
import cv2
import sys
import time

class CamThread(QThread):
changemap = pyqtSignal('QImage')

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

while True:
ret, img_rgb = cap.read()
if ret:
self.rgb = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2RGB)
self.convert = QImage(self.rgb.data, self.rgb.shape[1], self.rgb.shape[0], QImage.Format_RGB888)
self.p = self.convert.scaled(640, 480, Qt.KeepAspectRatio)
self.changemap.emit(self.p)
#time.sleep(0.1)


class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'webcam'

self.initUI()

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

def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(100, 100, 640, 480)
self.resize(640, 480)
self.label = QLabel(self)
self.label.resize(640, 480)
thr = CamThread(self)
thr.changemap.connect(self.setImage)
thr.start()

app = QApplication(sys.argv)
win = App()
#win.setAttribute(Qt.WA_DeleteOnClose, True)
win.show()
app.exit(app.exec_())


I thought that the problem is somewhere in signals/slots but haven't been able to find anything relevant.



  • Windows 10


  • Python - 3.7


  • Pyqt - 5.12


  • OpenCV - 3.4.5.20










share|improve this question
























  • change self.rgb to rgb, self.convert to convert and self.p to p

    – eyllanesc
    Mar 22 at 19:44











  • Tried. Changes nothing

    – Mur4al
    Mar 22 at 19:47











  • execute it in the CMD, there you should surely give you more information

    – eyllanesc
    Mar 22 at 19:49











  • Still crushes. Shows nothing in CMD not even previous error.

    – Mur4al
    Mar 22 at 19:58












  • mmm, why do you want an image beyond 100ms ?, a camera provides a 30FPS image that is 30 ms, what is the FPS of your camera?

    – eyllanesc
    Mar 22 at 19:59













0












0








0








I tried code from this answer and it crashes with error Process finished with exit code -1073740771 (0xC000041D) after some time (2-10 sec.) and sometimes with 0xC0000005. It crashes immediately if I try to drag the window.
However when I put time.sleep(0.1) in run it works fine. If I use sleeps shorter than 0.1 it crashes again.



from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QLabel,QMessageBox
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import QThread, pyqtSignal, pyqtSlot, Qt
import cv2
import sys
import time

class CamThread(QThread):
changemap = pyqtSignal('QImage')

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

while True:
ret, img_rgb = cap.read()
if ret:
self.rgb = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2RGB)
self.convert = QImage(self.rgb.data, self.rgb.shape[1], self.rgb.shape[0], QImage.Format_RGB888)
self.p = self.convert.scaled(640, 480, Qt.KeepAspectRatio)
self.changemap.emit(self.p)
#time.sleep(0.1)


class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'webcam'

self.initUI()

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

def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(100, 100, 640, 480)
self.resize(640, 480)
self.label = QLabel(self)
self.label.resize(640, 480)
thr = CamThread(self)
thr.changemap.connect(self.setImage)
thr.start()

app = QApplication(sys.argv)
win = App()
#win.setAttribute(Qt.WA_DeleteOnClose, True)
win.show()
app.exit(app.exec_())


I thought that the problem is somewhere in signals/slots but haven't been able to find anything relevant.



  • Windows 10


  • Python - 3.7


  • Pyqt - 5.12


  • OpenCV - 3.4.5.20










share|improve this question
















I tried code from this answer and it crashes with error Process finished with exit code -1073740771 (0xC000041D) after some time (2-10 sec.) and sometimes with 0xC0000005. It crashes immediately if I try to drag the window.
However when I put time.sleep(0.1) in run it works fine. If I use sleeps shorter than 0.1 it crashes again.



from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QLabel,QMessageBox
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import QThread, pyqtSignal, pyqtSlot, Qt
import cv2
import sys
import time

class CamThread(QThread):
changemap = pyqtSignal('QImage')

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

while True:
ret, img_rgb = cap.read()
if ret:
self.rgb = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2RGB)
self.convert = QImage(self.rgb.data, self.rgb.shape[1], self.rgb.shape[0], QImage.Format_RGB888)
self.p = self.convert.scaled(640, 480, Qt.KeepAspectRatio)
self.changemap.emit(self.p)
#time.sleep(0.1)


class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'webcam'

self.initUI()

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

def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(100, 100, 640, 480)
self.resize(640, 480)
self.label = QLabel(self)
self.label.resize(640, 480)
thr = CamThread(self)
thr.changemap.connect(self.setImage)
thr.start()

app = QApplication(sys.argv)
win = App()
#win.setAttribute(Qt.WA_DeleteOnClose, True)
win.show()
app.exit(app.exec_())


I thought that the problem is somewhere in signals/slots but haven't been able to find anything relevant.



  • Windows 10


  • Python - 3.7


  • Pyqt - 5.12


  • OpenCV - 3.4.5.20







python python-3.x opencv pyqt5 qthread






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 19:57







Mur4al

















asked Mar 22 at 19:33









Mur4alMur4al

164




164












  • change self.rgb to rgb, self.convert to convert and self.p to p

    – eyllanesc
    Mar 22 at 19:44











  • Tried. Changes nothing

    – Mur4al
    Mar 22 at 19:47











  • execute it in the CMD, there you should surely give you more information

    – eyllanesc
    Mar 22 at 19:49











  • Still crushes. Shows nothing in CMD not even previous error.

    – Mur4al
    Mar 22 at 19:58












  • mmm, why do you want an image beyond 100ms ?, a camera provides a 30FPS image that is 30 ms, what is the FPS of your camera?

    – eyllanesc
    Mar 22 at 19:59

















  • change self.rgb to rgb, self.convert to convert and self.p to p

    – eyllanesc
    Mar 22 at 19:44











  • Tried. Changes nothing

    – Mur4al
    Mar 22 at 19:47











  • execute it in the CMD, there you should surely give you more information

    – eyllanesc
    Mar 22 at 19:49











  • Still crushes. Shows nothing in CMD not even previous error.

    – Mur4al
    Mar 22 at 19:58












  • mmm, why do you want an image beyond 100ms ?, a camera provides a 30FPS image that is 30 ms, what is the FPS of your camera?

    – eyllanesc
    Mar 22 at 19:59
















change self.rgb to rgb, self.convert to convert and self.p to p

– eyllanesc
Mar 22 at 19:44





change self.rgb to rgb, self.convert to convert and self.p to p

– eyllanesc
Mar 22 at 19:44













Tried. Changes nothing

– Mur4al
Mar 22 at 19:47





Tried. Changes nothing

– Mur4al
Mar 22 at 19:47













execute it in the CMD, there you should surely give you more information

– eyllanesc
Mar 22 at 19:49





execute it in the CMD, there you should surely give you more information

– eyllanesc
Mar 22 at 19:49













Still crushes. Shows nothing in CMD not even previous error.

– Mur4al
Mar 22 at 19:58






Still crushes. Shows nothing in CMD not even previous error.

– Mur4al
Mar 22 at 19:58














mmm, why do you want an image beyond 100ms ?, a camera provides a 30FPS image that is 30 ms, what is the FPS of your camera?

– eyllanesc
Mar 22 at 19:59





mmm, why do you want an image beyond 100ms ?, a camera provides a 30FPS image that is 30 ms, what is the FPS of your camera?

– eyllanesc
Mar 22 at 19:59












1 Answer
1






active

oldest

votes


















0














Fixed it using QMutex and QWaitCondition to prevent update call while main thread is already updating. Apparently, issue was in that. eyllanesc, I'm new here as you see, should I make an answer in original thread?



from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QLabel, QMessageBox
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import QThread, pyqtSignal, pyqtSlot, Qt, QMutex, QWaitCondition
import cv2
import sys
import time


class CamThread(QThread):
changemap = pyqtSignal('QImage')

def __init__(self, mutex, condition):
super().__init__()
self.mutex = mutex
self.condition = condition

def run(self):
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
while True:
try:
ret, img_rgb = cap.read()
if ret:
rgb = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2RGB)

#any other image processing here

convert = QImage(rgb.data, rgb.shape[1], rgb.shape[0], QImage.Format_RGB888)
p = convert.scaled(640, 480, Qt.KeepAspectRatio)
self.changemap.emit(p)
self.condition.wait(self.mutex)

except:
print('error')


class App(QWidget):
time = 0

def __init__(self):
super().__init__()
self.title = 'webcam'
self.mutex = QMutex()
self.condition = QWaitCondition()
self.initUI()

@pyqtSlot('QImage')
def setImage(self, image):
self.mutex.lock()
try:
self.label.setPixmap(QPixmap.fromImage(image))
finally:
self.mutex.unlock()
self.condition.wakeAll()

def initUI(self):
self.mutex.lock()
self.setWindowTitle(self.title)
self.setGeometry(100, 100, 640, 480)
self.resize(640, 480)
self.label = QLabel(self)
self.label.resize(640, 480)
self.thr = CamThread(mutex = self.mutex,condition=self.condition)
self.thr.changemap.connect(self.setImage)
self.thr.start()


app = QApplication(sys.argv)
win = App()
win.show()
app.exit(app.exec_())


N.B. You still need to properly stop thread and close camera connection in this example.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55306673%2fpyqt-crashes-when-trying-to-show-opencv-videostream%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Fixed it using QMutex and QWaitCondition to prevent update call while main thread is already updating. Apparently, issue was in that. eyllanesc, I'm new here as you see, should I make an answer in original thread?



    from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QLabel, QMessageBox
    from PyQt5.QtGui import QImage, QPixmap
    from PyQt5.QtCore import QThread, pyqtSignal, pyqtSlot, Qt, QMutex, QWaitCondition
    import cv2
    import sys
    import time


    class CamThread(QThread):
    changemap = pyqtSignal('QImage')

    def __init__(self, mutex, condition):
    super().__init__()
    self.mutex = mutex
    self.condition = condition

    def run(self):
    cap = cv2.VideoCapture(0)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
    while True:
    try:
    ret, img_rgb = cap.read()
    if ret:
    rgb = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2RGB)

    #any other image processing here

    convert = QImage(rgb.data, rgb.shape[1], rgb.shape[0], QImage.Format_RGB888)
    p = convert.scaled(640, 480, Qt.KeepAspectRatio)
    self.changemap.emit(p)
    self.condition.wait(self.mutex)

    except:
    print('error')


    class App(QWidget):
    time = 0

    def __init__(self):
    super().__init__()
    self.title = 'webcam'
    self.mutex = QMutex()
    self.condition = QWaitCondition()
    self.initUI()

    @pyqtSlot('QImage')
    def setImage(self, image):
    self.mutex.lock()
    try:
    self.label.setPixmap(QPixmap.fromImage(image))
    finally:
    self.mutex.unlock()
    self.condition.wakeAll()

    def initUI(self):
    self.mutex.lock()
    self.setWindowTitle(self.title)
    self.setGeometry(100, 100, 640, 480)
    self.resize(640, 480)
    self.label = QLabel(self)
    self.label.resize(640, 480)
    self.thr = CamThread(mutex = self.mutex,condition=self.condition)
    self.thr.changemap.connect(self.setImage)
    self.thr.start()


    app = QApplication(sys.argv)
    win = App()
    win.show()
    app.exit(app.exec_())


    N.B. You still need to properly stop thread and close camera connection in this example.






    share|improve this answer





























      0














      Fixed it using QMutex and QWaitCondition to prevent update call while main thread is already updating. Apparently, issue was in that. eyllanesc, I'm new here as you see, should I make an answer in original thread?



      from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QLabel, QMessageBox
      from PyQt5.QtGui import QImage, QPixmap
      from PyQt5.QtCore import QThread, pyqtSignal, pyqtSlot, Qt, QMutex, QWaitCondition
      import cv2
      import sys
      import time


      class CamThread(QThread):
      changemap = pyqtSignal('QImage')

      def __init__(self, mutex, condition):
      super().__init__()
      self.mutex = mutex
      self.condition = condition

      def run(self):
      cap = cv2.VideoCapture(0)
      cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
      cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
      while True:
      try:
      ret, img_rgb = cap.read()
      if ret:
      rgb = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2RGB)

      #any other image processing here

      convert = QImage(rgb.data, rgb.shape[1], rgb.shape[0], QImage.Format_RGB888)
      p = convert.scaled(640, 480, Qt.KeepAspectRatio)
      self.changemap.emit(p)
      self.condition.wait(self.mutex)

      except:
      print('error')


      class App(QWidget):
      time = 0

      def __init__(self):
      super().__init__()
      self.title = 'webcam'
      self.mutex = QMutex()
      self.condition = QWaitCondition()
      self.initUI()

      @pyqtSlot('QImage')
      def setImage(self, image):
      self.mutex.lock()
      try:
      self.label.setPixmap(QPixmap.fromImage(image))
      finally:
      self.mutex.unlock()
      self.condition.wakeAll()

      def initUI(self):
      self.mutex.lock()
      self.setWindowTitle(self.title)
      self.setGeometry(100, 100, 640, 480)
      self.resize(640, 480)
      self.label = QLabel(self)
      self.label.resize(640, 480)
      self.thr = CamThread(mutex = self.mutex,condition=self.condition)
      self.thr.changemap.connect(self.setImage)
      self.thr.start()


      app = QApplication(sys.argv)
      win = App()
      win.show()
      app.exit(app.exec_())


      N.B. You still need to properly stop thread and close camera connection in this example.






      share|improve this answer



























        0












        0








        0







        Fixed it using QMutex and QWaitCondition to prevent update call while main thread is already updating. Apparently, issue was in that. eyllanesc, I'm new here as you see, should I make an answer in original thread?



        from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QLabel, QMessageBox
        from PyQt5.QtGui import QImage, QPixmap
        from PyQt5.QtCore import QThread, pyqtSignal, pyqtSlot, Qt, QMutex, QWaitCondition
        import cv2
        import sys
        import time


        class CamThread(QThread):
        changemap = pyqtSignal('QImage')

        def __init__(self, mutex, condition):
        super().__init__()
        self.mutex = mutex
        self.condition = condition

        def run(self):
        cap = cv2.VideoCapture(0)
        cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
        cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
        while True:
        try:
        ret, img_rgb = cap.read()
        if ret:
        rgb = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2RGB)

        #any other image processing here

        convert = QImage(rgb.data, rgb.shape[1], rgb.shape[0], QImage.Format_RGB888)
        p = convert.scaled(640, 480, Qt.KeepAspectRatio)
        self.changemap.emit(p)
        self.condition.wait(self.mutex)

        except:
        print('error')


        class App(QWidget):
        time = 0

        def __init__(self):
        super().__init__()
        self.title = 'webcam'
        self.mutex = QMutex()
        self.condition = QWaitCondition()
        self.initUI()

        @pyqtSlot('QImage')
        def setImage(self, image):
        self.mutex.lock()
        try:
        self.label.setPixmap(QPixmap.fromImage(image))
        finally:
        self.mutex.unlock()
        self.condition.wakeAll()

        def initUI(self):
        self.mutex.lock()
        self.setWindowTitle(self.title)
        self.setGeometry(100, 100, 640, 480)
        self.resize(640, 480)
        self.label = QLabel(self)
        self.label.resize(640, 480)
        self.thr = CamThread(mutex = self.mutex,condition=self.condition)
        self.thr.changemap.connect(self.setImage)
        self.thr.start()


        app = QApplication(sys.argv)
        win = App()
        win.show()
        app.exit(app.exec_())


        N.B. You still need to properly stop thread and close camera connection in this example.






        share|improve this answer















        Fixed it using QMutex and QWaitCondition to prevent update call while main thread is already updating. Apparently, issue was in that. eyllanesc, I'm new here as you see, should I make an answer in original thread?



        from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QLabel, QMessageBox
        from PyQt5.QtGui import QImage, QPixmap
        from PyQt5.QtCore import QThread, pyqtSignal, pyqtSlot, Qt, QMutex, QWaitCondition
        import cv2
        import sys
        import time


        class CamThread(QThread):
        changemap = pyqtSignal('QImage')

        def __init__(self, mutex, condition):
        super().__init__()
        self.mutex = mutex
        self.condition = condition

        def run(self):
        cap = cv2.VideoCapture(0)
        cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
        cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
        while True:
        try:
        ret, img_rgb = cap.read()
        if ret:
        rgb = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2RGB)

        #any other image processing here

        convert = QImage(rgb.data, rgb.shape[1], rgb.shape[0], QImage.Format_RGB888)
        p = convert.scaled(640, 480, Qt.KeepAspectRatio)
        self.changemap.emit(p)
        self.condition.wait(self.mutex)

        except:
        print('error')


        class App(QWidget):
        time = 0

        def __init__(self):
        super().__init__()
        self.title = 'webcam'
        self.mutex = QMutex()
        self.condition = QWaitCondition()
        self.initUI()

        @pyqtSlot('QImage')
        def setImage(self, image):
        self.mutex.lock()
        try:
        self.label.setPixmap(QPixmap.fromImage(image))
        finally:
        self.mutex.unlock()
        self.condition.wakeAll()

        def initUI(self):
        self.mutex.lock()
        self.setWindowTitle(self.title)
        self.setGeometry(100, 100, 640, 480)
        self.resize(640, 480)
        self.label = QLabel(self)
        self.label.resize(640, 480)
        self.thr = CamThread(mutex = self.mutex,condition=self.condition)
        self.thr.changemap.connect(self.setImage)
        self.thr.start()


        app = QApplication(sys.argv)
        win = App()
        win.show()
        app.exit(app.exec_())


        N.B. You still need to properly stop thread and close camera connection in this example.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 23 at 19:21

























        answered Mar 23 at 18:41









        Mur4alMur4al

        164




        164





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55306673%2fpyqt-crashes-when-trying-to-show-opencv-videostream%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript