5

So I am attempting to capture from two cameras in openCV (python & windows 7). I capture from one camera just fine, youll also notice I am doing some funky stuff to the image but that doesn't matter. This is the code to attempt to use two

import cv
import time
cv.NamedWindow("camera", 1)
cv.NamedWindow("camera2", 1)
capture = cv.CaptureFromCAM(0)
capture2 = cv.CaptureFromCAM(1)
while True:
    img = cv.GetMat(cv.QueryFrame(capture))
    img2 = cv.GetMat(cv.QueryFrame(capture2))
    dst_image = cv.CloneMat(img)
    dst_image2 = cv.CloneMat(img2)
    cv.ConvertScale(img, dst_image, 255, -59745.0)
    cv.ConvertScale(img2, dst_image2, 255, -59745.0)
    cv.ShowImage("camera", dst_image)
    cv.ShowImage("camera2", dst_image2)
    if cv.WaitKey(10) == 27:
        cv.DestroyWindow("camera")
        cv.DestroyWindow("camera2")
        break

Rather simple. However it won't work. Upon trying to create the matrix from the second camera (second line of code in the loop), I am told that the capture is null. The cameras I am using are logitech and are the same model.

Side note: I also couldnt find the command to count cameras connected in python, so if someone could refer me to that I'd much appreciate it. --Ashley

EDIT: It might also be useful to know that windows often prompts me to choose which camera I would like to use. I can't seem to avoid this behavior. Additionally I downloaded some security like software that successful runs both cameras at once. It is not open source or anything like that. So clearly, this is possible.

user1118684
  • 254
  • 1
  • 3
  • 9

3 Answers3

4

I was having the same problem with two lifecam studio webcams. After a little reading, I think that problem related to overloading the bandwidth on the USB-bus. Both cameras began working if I 1.) lowered the resolution (320 x 240 each) or 2.) lowered the frame rate (~99 msec @ 800 x 600). Attached is the code that got I working:

import cv

cv.NamedWindow("Camera 1")
cv.NamedWindow("Camera 2")
video1 = cv.CaptureFromCAM(0)
cv.SetCaptureProperty(video1, cv.CV_CAP_PROP_FRAME_WIDTH, 800)
cv.SetCaptureProperty(video1, cv.CV_CAP_PROP_FRAME_HEIGHT, 600)

video2 = cv.CaptureFromCAM(1)
cv.SetCaptureProperty(video2, cv.CV_CAP_PROP_FRAME_WIDTH, 800)
cv.SetCaptureProperty(video2, cv.CV_CAP_PROP_FRAME_HEIGHT, 600)

loop = True
while(loop == True):
    frame1 = cv.QueryFrame(video1)
    frame2 = cv.QueryFrame(video2)
    cv.ShowImage("Camera 1", frame1)
    cv.ShowImage("Camera 2", frame2)
    char = cv.WaitKey(99)
    if (char == 27):
        loop = False

cv.DestroyWindow("Camera 1")
cv.DestroyWindow("Camera 2")
eminentCodfish
  • 263
  • 1
  • 4
  • 13
2

here is a small code:

import VideoCapture
cam0 = VideoCapture.Device(0)
cam1 = VideoCapture.Device(1)
im0 = cam0.getImage()
im1 = cam1.getImage()

im0 and im1 are PIL images. You can now use scipy to convert it into arrays as follows:

import scipy as sp
imarray0 = asarray(im0)
imarray1 = asarray(im1)

imarray0 and imarray1 are numpy 2D arrays, which you can furthere use with openCV functions.

Vishwanath
  • 835
  • 1
  • 7
  • 19
  • 1
    So this helped me more easily diagnose the problem. Turns out that its the drivers for logitech that wont let the two cameras talk to the computer at once. It gets the first camera fine. The second camera attempts to connect, the light blinks, but never succeeds. Thanks again, I think I will, for now, try two different cameras and then see if I can go with another brand and do two of the same. – user1118684 Dec 30 '11 at 17:33
  • 1
    Awesome that you have been able to crack the trouble. Also tell me if you figure out how to enumerate all the possible webcams connected to the computer – Vishwanath Dec 31 '11 at 05:15
0

In case you are using windows for coding, why dont you try VideoCapture module. It is very easy to use and gives a PIL image as output. You can later change it to a 2D array.

Vishwanath
  • 835
  • 1
  • 7
  • 19
  • I have many reasons, particularly down the road, that I need I would prefer using openCV for. But thanks. – user1118684 Dec 28 '11 at 18:29
  • Okay getting desperate now. Set up VideoCapture and saved an image from one camera, I am a bit confused as to how to do 2, though. Any examples you can point me to? – user1118684 Dec 28 '11 at 20:38