[SOLVED]Assertion Error in Video Capturing ? [closed]

asked 2020-03-15 04:46:27 -0600

updated 2020-03-17 04:07:55 -0600

supra56 gravatar image

I've just started with openCV and trying to capture frames from webcam. But when I run the code the script catures the first frame and then Execption occurs.

import cv2, time

a = 1

while True:
    a = a + 1
    video = cv2.VideoCapture(0, cv2.CAP_DSHOW)
    check, frame = video.read()
    print(frame)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #This is the Erroneous line 
    cv2.imshow("Capture", gray)
    key = cv2.waitKey(1)

    if key == ord("q"):
        break
print(a)
video.release()
cv2.destroyAllWindows()

cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by supra56
close date 2020-03-17 04:06:05.592283

Comments

1

video = cv2.VideoCapture(0, cv2.CAP_DSHOW) -- this has to go before the loop.

also, the check value is there for a reason ....

try, and come again

berak gravatar imageberak ( 2020-03-15 05:11:42 -0600 )edit
1

Code :

import cv2, time
a = 1

video = cv2.VideoCapture(0, cv2.CAP_DSHOW)

while True:
    a = a + 1

    check, frame = video.read()
    print(frame)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #This is the Erroneous line 
    cv2.imshow("Capture", gray)
    key = cv2.waitKey(1)

    if key == ord("q"):
        break
print(a)
video.release()
cv2.destroyAllWindows()

when cv2.VideoCapture(0, cv2.CAP_DSHOW) inside the loop it will call camera everytime to capture image. so it should be declared outside the loop.

Imran B gravatar imageImran B ( 2020-03-16 02:36:06 -0600 )edit

Is your code working or not?

supra56 gravatar imagesupra56 ( 2020-03-16 09:46:37 -0600 )edit
1

Thanks. It's working Now !!

assassin gravatar imageassassin ( 2020-03-16 10:29:38 -0600 )edit