简体   繁体   English

如何在 Python OpenCV 中使用 cv2.waitKey(1)

[英]How to use cv2.waitKey(1) in Python OpenCV

I am doing some OpenCV python code and I have below code at the end:我正在做一些OpenCV python 代码,最后有以下代码:

if cv2.waitKey(1) & 0xFF == ord('q'):
    break

So whenever I press q , the code breaks which is working fine.所以每当我按下q ,代码就会中断,这工作正常。 But below code is not working:但下面的代码不起作用:

if cv2.waitKey(1) & 0xFF == ord('q'):
    break
if cv2.waitKey(1) & 0xFF == ord('a'):
    print('a')

In above code, only q is working but if I press a , it is not printing a .在上面的代码中,只有q正在工作,但是如果我按a ,它不会打印a Why is this not working.为什么这不起作用。 Can anyone please suggest me what is wrong here.任何人都可以请告诉我这里有什么问题。 Thanks谢谢

CODE:代码:

cam = cv2.VideoCapture(0)
while True:
    ret_val, image = cam.read()

    cv2.imshow('my webcam', image)

    if cv2.waitKey(1) & 0xFF == ord('a'):  
        print("a")
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()

waitKey returns the ASCII value of key that's pressed while ord converts a character into its ASCII value. waitKey 返回在 ord 将字符转换为其 ASCII 值时按下的键的 ASCII 值。 So something like this will work所以这样的事情会起作用

key = cv2.waitKey(1)
if key == ord('q') :
 break
elif key == ord('a'):
 print('a')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM