Why does the webcam page not close? cv2 python

Viewed 18

i have a simple webcam opening using cv2 module:

cap=cv2.VideoCapture(0)
while True:
    ret, img = cap.read()
    cv2.imshow('webcam',img)
    k=cv2.waitKey(10)
    if k == 27:
        break
cap.release()
cv2.destroyWindows()

this program runs for ever, although i try to close it, the only way is closing vsCode

1 Answers

You can close the webcam window by clicking on your webcam window and press esc while if you want to close it with specific alphabet then you can use the below code to destroy the window by pressing q.

import cv2
cap=cv2.VideoCapture(0)
while True:
    ret, img = cap.read()
    cv2.imshow('webcam',img)
    if cv2.waitKey(10) & 0xFF == ord('q'):
        break        
cap.release()
cv2.destroyAllWindows()
Related