cv2.waitKey为所有键返回255

jan*_*777 5 opencv python-3.x

我试图使用cv2.waitKey(0)读取键值,但它不起作用.它永远等待.我使用cv2.waitKey(1)检查它返回的内容,无论我按哪个键,它总是255.

while True:
      key = cv2.waitKey(0)
      print(key)
Run Code Online (Sandbox Code Playgroud)

无论我按哪个键,上面的代码都不起作用.

while True:
     key = cv2.waitKey(1) & 0xFF
     print(key)
     if key == ord('q'):
        break
Run Code Online (Sandbox Code Playgroud)

保持打印255,如果按'q'则不会打破.

jan*_*777 11

我找到了解决方案.看起来它需要打开一个命名窗口才能读取键值.所以我尝试了下面的工作.

cap = cv2.VideoCapture(0) 
cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
while(True):
    ret, frame = cap.read()
    cv2.imshow('frame',frame)
    key=cv2.waitKey(0) & 0xFF
    print(key)
    if key == ord('q'):
         break
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)