I'm experimenting with trying to capture an image from multiple webcams simultaneously (or near-simultaneously). I've been playing with OpenCV and with VideoCapture and programming in python. But have some confusion and hoped someone could help explain things...
To Start, I tried VideoCapture (Markus Gritsch's work found here: http://videocapture.sourceforge.net/). This is a very easy to use add-in for python. If I just wanted to capture a simple image. It works just fine... for the most part.
My setup is 2 cheapie webcams in a USB hub on a single USB port and my laptop's built-in webcam.
I have read all about using the same model webcam on the same USB bus and how it may not work, etc. but decided to give it a go. (I also have some photobooth program that I must have installed eons ago called "Cyberlink YouCam" For some reason, this program "looks" like a camera to both OpenCV and VideoCapture.
VideoCapture connects to and captures from each camera like this:
Cam0 = Device(devnum=0)
Cam0.saveSnapshot("filename0.jpg")
del Cam0
Cam1 = Device(devnum=1)
Cam1.saveSnapshot("filename1.jpg")
del Cam1
With VideoCapture and the setup I described, I can independently capture from all the cameras on my system (4 total, including the YouCam... 0 - 3)
The problem is that this program does not seem to be able to connect to more than one camera at the same time... if I don't close the previous camera instance, it simply freezes and I have to disconnect and reconnect the first webcam from USB to regain access to it.
This won't work:
cam0 = Device(devnum=0)
cam1 = Device(devnum=1)
cam0.saveSnapshot("filename0.jpg")
cam1.saveSnapshot("filename1.jpg")
del cam0
del cam1
Cam0 will open, but that is the end of it. Frozen.
Another thing with VideoCapture is that on the cheapo webcams, there is a major delay (almost a second) until the picture comes alive... in order to do a successful capture, I had to do something like this:
Cam1 = Device(devnum=1)
Cam1.saveSnapshot("filename1.jpg") #gets the camera going and saves black image
time.sleep(.75) #delay
Cam1.saveSnapshot("filename1.jpg") #captures the image second time around
Effectively saving the image twice...
I wouldn't have minded if the images from each camera were a few milliseconds apart taken in squence
#open connection to cam, take image
#close connection to cam
#connect to next cam, take next image
#close connection to cam
#etc.
But the delay was way too much... I thought the delay was the cheap webcams, but I saw a different result with Open CV so it must be VideoCapture's fault.
For one thing, I COULD open more than one camera at the same time with OpenCV... but only of different types. (One of the cheapo cams and my built-in cam... and / or the Cyberlink program) OpenCV seemed to see the 2 cheapo cams on the same USB hub as one (it only turned on one of the cams.)
capture0 = CaptureFromCAM(0) #this was Cyberlink Program
capture1 = CaptureFromCAM(1) #this was cheapo cam
capture2 = CaptureFromCAM(2) #this was built-in cam
#CaptureFromCAM(3) resulted in error... did not find a 4th "camera"
frame0 = QueryFrame(capure0)
frame1 = QueryFrame(capure1)
frame2 = QueryFrame(capure2)
cv.SaveImage("filename0.jpg",frame0)
cv.SaveImage("filename1.jpg",frame1)
cv.SaveImage("filename2.jpg",frame2)
Whereas VideoCapture could see the 2 similar cameras independently, OpenCV could not.
Anyone know why that would be? How are the two packages interacting with the computer differently that one can determine different cams on the same USB and the other cannot?
Secondly, OpenCV opened my cheap cams instantaneously... no .75 second delays there.
Again, I am curious how the two packages (videoCapture vs OpenCV) communicating with the cameras differently from each other?
What I am ultimately interested in doing is being able to capture from 2 or 3 cameras at the same time (or close to it, if there were a few milliseconds delay, that is fine). Looks like OpenCV would be the package of choice, however, I am trying to understand better how the software is interacting with the cameras.
Thank you all for the insight!
J
I can tell you some information about OpenCV, but not about VideoCapture. I do, however, use VideoCapture for getting nice camera names.
I have used OpenCV in python to successfully talk to two cameras at the same time. In some cases, the two cameras were different, other times they were the same but scientific level so perhaps the drivers were better. I can't say that having two identical cameras isn't causing you a problem.
However, I can say that you are using the old and possibly unmaintained camera API. Use the new cv2
namespace/API also called VideoCapture. See OpenCV VideoCapture class python bindings beginning with cv2
. If you have a new version of OpenCV in your python distribution, it is accessed via import cv2
. The old bindingss are in cv2.cv
(also note that as of OpenCV 2.4.6, many of the important constants for VideoCapture::get(...)
and ::set(...)
are only found in cv2.cv
, this is supposed to be fixed in the next version)