我正在使用 OpenCV,我希望 OpenCV 不等待按下任何键(cv2.waitKey()
的默认行为),但要等待我定义的特定键(并执行后续操作)。
我目前针对此问题的解决方案是以下递归解决方案:
def opencv_wait():
# wait for keypress; capture it
k = cv2.waitKey(0)
if k == 27: # this should be ESC
return # e.g. end the program
elif k == some_key: # some other keys...
do_some_function() # ...and actions to do after key is pressed
else:
opencv_wait() # recursively call opencv_wait() for looping
我的问题是:这个解决方案是否是让 OpenCV 等待不同 key 的便捷方式?
是否有更快/更好的方法来实现我想做的事情?
基本上,我希望 OpenCV 无限等待(尽可能少地浪费资源),直到按下应触发后续操作的特定键。
最佳答案
如果没有必要,你可以尝试非递归方法:
def is_pressed(key)
# if statement
def opencv_wait():
key = 0
while is_pressed(key) :
key = cv2.waitKey(0)
关于python - OpenCV:WAITING不同的 key ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37499096/