I just finished some OpenCV code and cv2.waitKey(1) & 0xff == ord('q') was one of the pieces that I toyed with multiple times.
First:
cv2.waitKey([delay])
The function waitKey waits for a key event infinitely and the delay is in milliseconds. waitKey(0) means forever.
Second:
The ord() method returns an integer representing a Unicode code point for the given Unicode character. In your code you want the user to select the letter 'q' which is translated to the Unicode value of '113.'
Third:
0xFF is a hexadecimal constant which is 11111111 in binary. It is used to
mask off the last 8bits of the sequence and the ord() of any keyboard character will not be greater than 255.
Here is the code that I'm using, which does not use ord() or & 0xff.
def display_facial_prediction_results(image):
# Display image with bounding rectangles
# and title in a window. The window
# automatically fits to the image size.
cv2.imshow('Facial Prediction', image)
while (True):
# Displays the window infinitely
key = cv2.waitKey(0)
# Shuts down the display window and terminates
# the Python process when a specific key is
# pressed on the window.
# 27 is the esc key
# 113 is the letter 'q'
if key == 27 or key == 113:
break
cv2.destroyAllWindows()