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?

1
  • DIfferent OS will have different return values for waitKey, but the 2 LSB will be the same. Commented Apr 7, 2016 at 8:41

1 Answer 1

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'.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.