简体   繁体   中英

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.

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")

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM