python - 使用 QtWidgets.QSlider 将 Waitkey 更改为 "0"无效

标签 python opencv pyqt5

这个问题在这里已经有了答案:





How to pause and play with 'p' key using cv2.waitKey on qt application

(1 个回答)


1年前关闭。




我的程序扫描给定视频中的圆圈。使用 QSlider,我可以在程序运行时更改 WaitKey 的值。每个数字都可以正常工作,除非我将 slider 值更改为 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说:

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()当且仅当 OpenCV HighGUI 窗口被激活时才有效。如果您使用 Qt GUI 作为界面,您也应该使用 Qt 功能来处理鼠标或键盘事件。

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

相关文章:

python - fatal error : ffi. h:pip2 install pyOpenSSL 上没有此类文件或目录

java - OpenCV 2.4.11 Java : Drawing lines from center of mass to edge of contour

python - 如何python opencv大小增加和减少实时摄像头的窗口

python - 如何更改 QGridLayout 小部件的大小?

python - 在PyQt5中实现洪水填充

Python re.sub() 怪异

python - Huggingface 上的 mT5 模型可以用于机器翻译吗?

python - python中逆ERF函数的命令

opencv - 使用来自实时视频流的OpenCV进行形状检测

python - 如何将 QTimeEdit 中的小时、分钟值显示到相应的 LCD 上?