This question already has an answer here:
How to pause and play with 'p' key using cv2.waitKey on qt application

(1个答案)


5个月前关闭。




我的程序扫描给定视频中的圆圈。使用QSlider,我可以在程序运行时更改WaitKey的值。除了将滑块值更改为0之外,每个数字都可以正常工作。视频不等待任何击键(据我所知,WaitKey(0)表示等待任何击键)。该程序的行为就像该值仍为1。该参数称为“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()
    [...]

最佳答案

正如documentation所说:



且仅当OpenCV HighGUI窗口被激活时,waitKey()才有效。如果将Qt GUI用作界面,则还应将Qt功能用于鼠标或键盘事件。

关于python - 使用QtWidgets.QSlider将Waitkey更改为 “0”无效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62359023/

10-12 21:46