Unable to take pictures with multiple cameras

Hi, I’m really new to OpenCV, Python, and programming in general. I have two USB cameras plugged into a Raspberry Pi 3.0 B+, and I’m trying to get images from both. I’ve been looking around at tutorials, but it doesn’t seem to work for me.

Below is the code that I am running. The goal is to receive ten images: five from one camera, and five from the other. Ideally, the cameras alternate taking pictures, rather than Camera 1 taking five followed by Camera 2 taking five.

from cv2 import *
print("Defining cameras")
cam1 = VideoCapture(0)
print("Camera 1 defined")
cam2 = VideoCapture(1)
print("Camera 2 defined")

# This loop reads frames from each camera, then saves an image for each. Loops 5 times
for x in range(5):

    result1, image1 = cam1.read()
    result2, image2 = cam2.read()

    if result1:

        title1 = str(x) + "_1.png"
        imwrite(title1, image1)
    
    else:
        print("No image 1 detected.")
        
    if result2:

        title2 = str(x) + "_2.png"
        imwrite(title2, image2)
    
    else:
        print("No image 2 detected.")

cam1.release()
cam2.release()
destroyAllWindows()
print("end")

Running this code results in five images, labeled “0_1.png”, “1_1.png”, “2_1.png”, “3_1.png”, and “4_1.png”. This part is working as intended.

However, there are no pictures for the second camera. Below is the result in the shell:

Defining cameras
Camera 1 defined
Camera 2 defined
VIDEOIO ERROR: V4L2: Could not obtain specifics of capture window.
VIDEOIO ERROR: V4L: can’t open camera by index 1
/dev/video1 does not support memory mapping
No image 2 detected.
No image 2 detected.
No image 2 detected.
No image 2 detected.
No image 2 detected.
end

After looking into this, others have solved their issue by starting from VideoCapture index -1, but using indices -1 and 0 in my code results in a different issue where the code will define the first camera (whether it is -1 or 0), and then stop before it defines the second camera. I don’t think my code uses numpy, since there’s no difference whether or not I import it.

Am I missing something, and/or is there a specific function I should be using instead of the current setup?

Thanks

Hi,

Test camera 0 and 1 alone.
Check cam1 and cam2 with isOpened()
try to start with camera 1 and 0. Is 1 Ok and 0 out?
Problem with raspberry pi is power supply when you plug two camera. You should check this.

from cv2 import *
import numpy
print("Defining cameras")
cam1 = VideoCapture(0)
print("Camera 1 defined")
cam2 = VideoCapture(1)
print("Camera 2 defined")

print(cam1.isOpened())
print(cam2.isOpened())

This is the code I ran. 0 is OK and 1 is out. Is this a power supply issue?

Thanks

No
In your first program it was

from cv2 import *
print("Defining cameras")
cam1 = VideoCapture(0)
print("Camera 1(0) defined")
cam2 = VideoCapture(1)
print("Camera 2(1) defined")

0 was 0K and not 1

Now you have to try

from cv2 import *
print("Defining cameras")
cam1 = VideoCapture(1)
print("Camera 1 (1)defined")
cam2 = VideoCapture(0)
print("Camera 2(0) defined")

1 must be OK and 0 should be out (of course if 1 and 0 are similar)

When I run:

from cv2 import *
print("Defining cameras")
cam1 = VideoCapture(0)
print("Camera 1 (0) defined")
cam2 = VideoCapture(1)
print("Camera 2 (1) defined")

print(cam1.isOpened())
print(cam2.isOpened())

I get:
Defining cameras
Camera 1 (0) defined
VIDEOIO ERROR: V4L2: Could not obtain specifics of capture window.
VIDEOIO ERROR: V4L: can’t open camera by index 1
/dev/video1 does not support memory mapping
Camera 2 (1) defined
True
False

When I run:

from cv2 import *
print("Defining cameras")
cam1 = VideoCapture(1)
print("Camera 1 (1) defined")
cam2 = VideoCapture(0)
print("Camera 2 (0) defined")

print(cam1.isOpened())
print(cam2.isOpened())

I get:
Defining cameras
VIDEOIO ERROR: V4L2: Could not obtain specifics of capture window.
VIDEOIO ERROR: V4L: can’t open camera by index 1
/dev/video1 does not support memory mapping
Camera 1 (1) defined
Camera 2 (0) defined
False
True

Solved. Looks like there was an issue with my USB ports on the Raspberry Pi- not power or an issue with code.