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