这个问题在这里已经有了答案:
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/