Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing From 2 Cameras (OpenCV, Python)

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.

like image 618
user1118684 Avatar asked Dec 28 '11 04:12

user1118684


People also ask

How do I record multiple camera streams with OpenCV?

To capture multiple streams with OpenCV, I recommend using threading which can improve performance by alleviating the heavy I/O operations to a separate thread. Since accessing the webcam/IP/RTSP stream using cv2. VideoCapture().

How do I view multiple cameras at once?

The devices called video multiplexers and digital video recorders (DVRs) are used to integrate visuals from multiple video cameras on a single screen. A multiplexer is a small device that takes input video signals from one or more cameras and provides one or more video streams as the output.

What does cv2 VideoCapture do?

cv2. VideoCapture – Creates a video capture object, which would help stream or display the video.


2 Answers

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")
like image 86
eminentCodfish Avatar answered Sep 20 '22 22:09

eminentCodfish


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.

like image 32
Vishwanath Avatar answered Sep 20 '22 22:09

Vishwanath