0
@QtCore.pyqtSlot()
    def startVideo(self):
        global image

        run_video = True
        while run_video:
            ret, image = self.camera.read()
            color_swapped_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

            qt_image1 = QtGui.QImage(color_swapped_image.data,
                                    self.width,
                                    self.height,
                                    color_swapped_image.strides[0],
                                    QtGui.QImage.Format_RGB888)
            self.VideoSignal1.emit(qt_image1)


            if self.flag:
                img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
                img_canny = cv2.Canny(img_gray, 50, 100)

                qt_image2 = QtGui.QImage(img_canny.data,
                                         self.width,
                                         self.height,
                                         img_canny.strides[0],
                                         QtGui.QImage.Format_Grayscale8)

                self.VideoSignal2.emit(qt_image2)


            loop = QtCore.QEventLoop()
            QtCore.QTimer.singleShot(25, loop.quit) #25 ms
            loop.exec_()

How this can be work about loop?

When the loop in while: do the singleshot 25ms until loop.quit,

Then what is the loop.exec??

I can't understand loop and singleshot in this code.

  • That is an uncommon way of *waiting* for a certain amount of time before continuing the while loop. It's normally not necessary to do that especially for this kind of cases, and it doesn't also seem very optimal to create and run event loops and timers every 25ms; they probably did that to avoid using threading, or because they actually needed event loops for other aspects (inter thread signals, event handling), but without a [mre] we cannot tell you more. In substance, if the above code is in a separate thread, it probably doesn't need that and a simple `time.sleep()` would suffice. – musicamante Jan 06 '23 at 11:37

0 Answers0