python freezes upon video webcam capture

asked 2020-01-19 18:02:48 -0500

Attempting to use the webcam for some object recognition. Inserted the cv2.waitKey(1) after reading a few forums. Running the following turns off the webcam and closes the window but python freezes causing a manual force quit. Please advise.

import cv2
import math
import utils
import face_detection

video_capture = cv2.VideoCapture(0)
counter = 5

while True:
    _, frame = video_capture.read()
    frame, face_box, face_coords = face_detection.detect_faces(frame)
    text = 'Image will be taken in {}..'.format(math.ceil(counter))
    if face_box is not None:
        frame = utils.write_on_frame(frame, text, face_coords[0], face_coords[1]-10)
    cv2.imshow('Video', frame)
    cv2.waitKey(1)
    counter -= 0.1
    if counter <= 0:
        cv2.imwrite('true_img.png', face_box)
        break

# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()

print("Onboarding Image Captured")
edit retag flag offensive close merge delete

Comments

Move both imshow and waitKey after if/else block.

if counter <= 0:
    ......
cv2.imshow('Video', frame)
cv2.waitKey(1)
supra56 gravatar imagesupra56 ( 2020-01-19 19:55:57 -0500 )edit