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

What does this cv2.waitKey(20) & 0xFF mean?

Striezel
  • 3,693
  • 7
  • 23
  • 37

1 Answers1

1

The waitKey() function waits the specified amount of milliseconds and then returns the code of the key that was pressed, or -1 if no key was pressed.

To understand the expression better, let's add some parenthesis:

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

The & is a bitwise and operator which is used here for bit masking to get only the lowest eight bits (because 0xFF is equal to 1111 1111 in binary). waitKey() returns an int which usually is a 32 bit or a 64 bit integer, depending on the architecture. So any "excess" bits are "eliminated" by the bitwise and. The ord() function supposedly(!) returns the ordinal value of it's parameter, i. e. the ASCII code of 'q' in that example.

In other words: It waits for 20 milliseconds for key presses and checks whether the pressed key is Q.

Striezel
  • 3,693
  • 7
  • 23
  • 37