The video window freezes or won't close

  • Python
  • OpenCV

Why it happens

  • cv2.waitKey(1) isn't in the loop.
  • The terminal, not the video window, has the keyboard.

How to fix it

  1. Put if cv2.waitKey(1) & 0xFF == ord('q'): break in the loop.
  2. Click on the video window, then press q.
  3. End with cap.release() and cv2.destroyAllWindows().
  4. Stuck? Press Ctrl+C in the terminal.

It worked if: q closes the window.

From the official documentation. From OpenCV's documentation of waitKey. Sources: OpenCV: highgui.

Didn't help? Search another error or ask us.