0

I am a still beginner in programming, pordon me if this question is too trivial. Let say I have this code:

camera = cv2.VideoCapture('path_to_video_file')
while True:
    #reading frames of video
    ret, frame = camera.read()
    cv2.imshow("Video", frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

So, from my understanding, at the instance when the frame is shown (in the "Video" window), if at the same time, the q key is pressed, the loop would break. But I don't really understand how the if cv2.waitKey(1) & 0xFF == ord('q') line works.

I do know this is an AND bitwise operation in which output is 1 only if both of the two inputs is also 1. But that is all abut it. So, I really want to know what is exactly happening.

And also, what is actually the 0xFF == ord('q') means?

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • DIfferent OS will have different return values for `waitKey`, but the 2 LSB will be the same. – Miki Apr 07 '16 at 08:41

1 Answers1

1

Python operator precedence gives us:

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

In binary, this is:

(cv2.waitKey(1) & 0b11111111) == ord('q')

So, what this does is select the low 8 bits of the result cv2.waitKey and test if that's equal to ord('q'), which is the ASCII value for 'q'.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415