- 摘录一
cv2.waitKey(1)返回当前按下的键的字符代码,如果未按下任何键,则返回-1。 & 0xFF是二进制AND操作,以确保仅保留键的单字节(ASCII)表示,对于某些操作系统, cv2.waitKey(1)将返回不是单字节的代码。 ord(‘q’)始终返回ord(‘q’)的ASCII表示形式,为113(十六进制为0x71)。因此,如果在评估cv2.waitKey(1)时用户按下q键,将确定以下内容:
cv2.waitKey(1) & 0xFF == cv2.ord('q')
0xXX71 & 0xFF == 0x71
0x71 == 0x71
True
- 摘录二
不使用 ord() 和 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()