Capture from 2 cameras (OpenCV, Python)

So, I'm trying to capture from two cameras in openCV (python and windows 7). I shoot with one camera just fine, you will also notice that I make some funky for the image, but it does not matter. This is a code that tries 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, this will not work. When trying to create a matrix from the second camera (the second line of code in the loop), I was told that the capture is zero. The cameras I use are logitech and are the same model.

Side note: I also could not find the command to count the cameras connected to the python, so if someone could refer to me, I would really appreciate it. --Ashley

EDIT: It may also be useful to know that windows often prompt me to choose which camera I would like to use. I cannot avoid this behavior. In addition, I downloaded some security, such as software that successfully starts both cameras at the same time. This is not an open source or anything like that. So clearly, it is possible.

+4
source share
3 answers

I had the same issue with two webcams with the lifecam website. After a little reading, I think the problem is with bandwidth overload on the USB bus. Both cameras started working if I 1.) lower the resolution (320 x 240 each) or 2.) lower the frame rate (~ 99 ms at 800 x 600). Attached code I received:

 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") 
+3
source

here is a little code:

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

im0 and im1 are PIL images. Now you can use scipy to convert it to arrays as follows:

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

imarray0 and imarray1 are multi-dimensional 2D arrays that you can use with openCV functions.

+2
source

If you use windows for coding, why don't you try the VideoCapture module. It is very easy to use and gives a PIL image as an output. You can change it to a 2D array later.

0
source

Source: https://habr.com/ru/post/1388237/


All Articles