OpenCV Assertion Failed error: (-215) scn == 3 || scn == 4 in function cv::cvtColor works ALTERNATE times
At least I do not find any major problem in your code, i.e. "should work". The problem seems to be in the camera driver. Cameras are different, and camera drivers are different (a.k.a. buggy).
Unfortunately, debugging the camera driver is not a very easy mission. The odd behaviour is most likely bound to the specific camera, operating system, OpenCV, and camera driver version. It is not very likely the driver can be fixed. Just try to keep everything up to date.
However, as your camera can capture every second image, there are things to do.
First, verify that the problem really is in the camera driver by replacing:
cam = create_capture(video_src, fallback='synth:bg=../cpp/lena.jpg:noise=0.05')
by
cam = create_capture('synth:bg=../cpp/lena.jpg:noise=0.05')
As is probably evident form the code, this forces the camera to be simulated. Function create_capture
is only a wrapper around VideoCapture
to provide this functionality. If your code runs fine with this, the problem is in the video driver.
After verifying that, you could try to run the following code:
import cv2
cam = cv2.VideoCapture(0)
cam.open(0)
results = [ cam.read()[0] for i in range(100) ]
print results
This should create a list of 100 Trues, and the process should take a few seconds, as the camera should capture 100 consecutive images.
In your code you do not seem to use the first value in the return tuple of cam.read
(ret
in your code). It is True
if the image is really captured. Also, cam.read
should block until there is an image available, no need to add any delays.
Most probably you will get a list [True, False, True, False, ...]
because the video driver does something odd. There is no beautiful way to fix this, but there is an ugly one. Replace your capture line by:
# max. 10 retries
for i in range (10):
ret, img = cam.read()
if ret:
break
else:
# capture failed even after 10 tries
raise MyExceptiom("Video driver does not like me.")
Of course, the driver may be so broken that you have to release and reopen it once in a while. Even uglier but doable, as well.
Summary: "Most probably it cannot be cured, it does not kill you, and there are medicines to alleviate the symptoms."