0

My program scans for circles in a given video. With a QSlider I can change the value of my WaitKey while the program is running. Every number works fine except when I change the slider value to 0. The video doesn't wait for any keystroke (as far as I know WaitKey(0) means to wait for any keystroke). The program acts like the value is still at 1. The parameter is called "globals.Speed".

# class that does the interface stuff
class MyWindow(QMainWindow):

    def __init__(self):
        super(MyWindow, self).__init__()
        self.resize(1300, 800)
        self.setWindowTitle("MainWindow")
        self.initUI()

    def initUI(self):
        [...]
        # QSlider to change WaitKey value
        self.horizontalSliderSpeed = QtWidgets.QSlider(self)
        self.horizontalSliderSpeed.setGeometry(QtCore.QRect(20, 300, 160, 22))
        self.horizontalSliderSpeed.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSliderSpeed.setMinimum(0)
        self.horizontalSliderSpeed.setMaximum(2000)
        self.horizontalSliderSpeed.setValue(globals.Speed)
        self.horizontalSliderSpeed.valueChanged.connect(lambda: self.changedValue(4))
        self.horizontalSliderSpeedLabel = QtWidgets.QLabel(self)
        self.horizontalSliderSpeedLabel.setFont(font)
        self.horizontalSliderSpeedLabel.setText(f"Speed: {globals.Speed}")
        self.horizontalSliderSpeedLabel.move(200, 300)
        [...]

    def changedValue(self, a):
        [...]
        if a == 4:
            globals.Speed = self.horizontalSliderSpeed.value()
            self.horizontalSliderSpeedLabel.setText(f"Speed: {globals.Speed}")
   [...]

# class that processes the video
class Child_Clocked(QThread):

    def run(self):
        cap = cv2.VideoCapture(globals.VideoFile)

        while globals.While_Run:
            try:
                cv2.waitKey(globals.Speed)
                ret, frame = cap.read()
    [...]
AtillaA
  • 91
  • 10

1 Answers1

2

As the documentation says:

Note: This function is the only method in HighGUI that can fetch and handle events, so it needs to be called periodically for normal event processing unless HighGUI is used within an environment that takes care of event processing.

Note: The function only works if there is at least one HighGUI window created and the window is active. If there are several HighGUI windows, any of them can be active.

waitKey() works if and only if an OpenCV HighGUI window is activated. If you are using Qt GUI for the interface also you should use Qt features for mouse or keyboard events.

Yunus Temurlenk
  • 4,085
  • 4
  • 18
  • 39
  • Is HighGUI the interface which pops up when the cv2.VideoCapture() starts? I display the video-frames inside a QLabel, which means no HighGUI is created in my case? Sorry my programing skills/knowledge are not that good yet, I'm not sure what needs to be changed inside my code for WaitKey(0) to work now. – AtillaA Jun 13 '20 at 11:44
  • 1
    Yes, as you said you are using qlabel which not belong to opencv ui. So waitKey() never ll work. You should use qt features for that purpose or you should use imshow() to get achieve it – Yunus Temurlenk Jun 13 '20 at 11:46
  • 2
    HighGUI is the library module from opencv. You'll need at least one cv::imshow or cv::namedWindow before your waitKey to create a window and don't close it. – Micka Jun 13 '20 at 11:48