Error on trying to use CV2 for capturing video from WebCam

Viewed 4425

I am trying to make an application that makes use of the Webcam to get the video. But when I run the program, the webcam doesn't start and I don't see the cv2 window open. I even tried making a simple webcam program, but it isn't working either:

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

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

I am getting the following error:

[ WARN:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (436) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
Traceback (most recent call last):
  File "test.py", line 11, in <module>
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.3.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

My webcam works fine in other application. Only cv2 is having problem. what should I do to fix the problem?

3 Answers

It seems there is no frame to convert to grayscale. How about try this.

import numpy as np
import cv2

#change to your video path
cap = cv2.VideoCapture("D:\start.mp4")

while(cap.isOpened()):
    # Capture frame-by-frame
    ret, frame = cap.read()

    if (ret!=True):
         break

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

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

The program you use is the "hello the world" of the video or the blink led or arduino.

So I experienced the same problem than you and the answer is .. bad driver for your camera.

when I bought my very expensive camera (20$) I install the driver and every thing was working fine with opencv.

I experimented recently nightmare with my WIFI and I start to uninstall drivers .

I finally solve my problems of WIFI YES !!! And then I reinstall my drivers include the video BUT My cat has scratched the original installation disk of the video , ok so I let windows install a generic driver.

Everything seems fine, sound and image, good (what else do you ask for a 20$ camera)

After some time I come back to openCV and then...same error

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

I look at all the forums test every thing I could install other version and all that "magic tricks" time consuming (but you learn a lot this is the good side of it) Totally desperate I remember that I did not install the original driver BINGO I went to the shop because this oem product did not have internet address to load the driver GRGRGRGRgrrgrgrgr and everything work perfect now Small tips with the generic driver I was often obliged to disconnect and reconnect my camera with this test https://www.onlinemictest.com/webcam-test/ (not sure if it this site is clean use it at tour own risk) So install a driver adapted to your camera and stop to doubt about opencv and your installation My 5 cents.

It looks like there could be a timing issue between the camera starting up and the code entering the while loop. I ran into the exact same issue and was stumped.

I even had a previous code that worked (in a different project). So I tried adding an extra read before entering the while loop.

ret, frame = cap.read()
white True:
    ret, frame = cap.read()
    # rest of the code

This worked. Then I wanted to see if this indeed fixes it. I removed the cap.read() before the while loop and re-ran the code. Now it continued working.

I am thinking that camera to start and be read by the program once before it can start instantly.

Related