Skip to content
Advertisement

my camera doesnt turn on while Coding in opencv

I am beginner in CV. My camera crash every time. I mean camera light is ON but the there is no camera display Could someone help me? Here is the code

import cv2 as cv
import numpy as np

cap=cv.VideoCapture(0) 
# read webcam untill the end
while (cap.isOpened()):
    # capture frame by frame
    ret,frame=cap.read()
    if ret == True:
        # to display frame
        cv.imshow("Frame",frame)
    else:
        break

cv.waitKey(1)
cap.release()
cv.destroyAllWindows()

Advertisement

Answer

i think you put waitKey in wrong place, I hope below code will work !

import cv2 as cv

cap = cv.VideoCapture(0)
# read webcam untill the end
while (cap.isOpened()):
    # capture frame by frame
    ret, frame = cap.read()
    if ret == True:
        # to display frame
        cv.imshow("Frame", frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break


cap.release()
cv.destroyAllWindows()
Advertisement