Picamera2 and Multi Camera Adapter

I have a RaspberryPi 4B running Full Bullseye 64-bit OS and an Arducam Multi Camera Adapter v2.2 with 2 OV5647 cameras attached in ports A and C.

I am using dtoverlay=camera-mux-4port,cam0-ov5647,cam1-ov5647 in the config and libcamera-hello -t 0 --camera 0 and libcamera-hello -t 0 --camera 1 both successfully run in the terminal and produce a preview feed from each of the respective cameras, so I know the cameras are properly connected and functional.

print(Picamera2.global_camera_info()) produces
[{'Model': 'ov5647', 'Location': 2, 'Rotation': 0, 'Id': '/base/soc/i2c0mux/i2c@1/pca@70/i2c@0/ov5647@36'}, {'Model': 'ov5647', 'Location': 2, 'Rotation': 0, 'Id': '/base/soc/i2c0mux/i2c@1/pca@70/i2c@1/ov5647@36'}]

The issue comes in using Picamera2 to switch between these cameras. Calling stop() on one camera and then calling start() on the other results in a device busy error. If I call close() then I can successfully start the other camera, but close() completely deallocates the camera object and closing and re-creating camera objects is computationally expensive and slow.

What is the proper way to use picamera2 to switch between cameras on the adapter board?
The multicamera example on Arducam’s github is not helpful as i2cset also returns a device is busy error. I also understand that it is encouraged to use Picamera2 which is built on libcamera instead of trying to libcamera-ify cv2 by manually controlling the gpio & i2c.

Below is sample code that shows the problem.

from picamera2 import Picamera2
import time

cam0 = Picamera2(0)
cam1 = Picamera2(1)

#code succeeds if this is cam0.close()



ERROR V4L2 v4l2_videodevice.cpp:1047 /dev/video0[26:cap]: Unable to set format: Device or resource busy

Traceback (most recent call last):
  File "/home/me/tests/test.py", line 15, in <module>
  File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 1049, in start
  File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 1004, in configure
  File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 943, in configure_
    if self.camera.configure(libcamera_config):
RuntimeError: Failed to configure camera: Device or resource busy

We will check this problem and write a demo code for the use case next week. Please stay tuned to this thread.

1 Like