Multiplexer example and changes to close function
I found when initialising Picamera2 multiple times in a loop, with different camera numbers we would get RuntimeError: Configuration failed.
One trick I found to resolve this was to delete the Picamera2 object and call gc.collect(). This isn't ideal so I found which attributes of the Picamera2 object seem to need to be removed, by running vars(picamera2) and determining which properties seemed to need to be deleted.
Having to use .close() is a bit confusing though I think. I'm wondering if there may be other solutions.
Also by setting a number of Picamera2 properties to None, the object can't easily be reused.
This sounds interesting. The case you're talking about is when you create a new Picamera2 object, overwriting a previous one, right?
Yeah, so without my changes to the close function, and without then calling .close() The following example would fail with 'RuntimeError: Configuration failed'
#!/usr/bin/python3
import picamera2
def takephoto(cam):
picam2 = picamera2.Picamera2(camera_num=cam)
capture_config = picam2.create_still_configuration()
picam2.start()
picam2.switch_mode_and_capture_file(capture_config, f"cam{cam}.jpg")
picam2.stop()
picam2.close()
for cam in range(4):
takephoto(cam)
I have to say, the changes look good to me, and telling folks they should call close doesn't seem too bad. Would deleting the Picamera2 object explicitly also achieve the same thing (I notice that we have a __del__ method that calls close)? It wouldn't be the first time people have found the need explicitly to delete Python variables that you might have thought would get cleared up automatically...
As a side note, this might be a good place to use the with Picamera2() as picam2: idiom, which I think would call close automatically for you.
That's a good question re. calling del, I'll try that to see if that functions now, without also having to call gc.collect().
Also good point re. using 'with', will switch to using that.
I should add I also get some error messages from libcamera, with my example, despite the 4 images from the 4 different cameras capturing to a file ok -
I mentioned the libcamera error here - https://forums.raspberrypi.com/viewtopic.php?t=339339 , 6by9 said "so I'm wondering if it's largely just noise in the logging"
[16:56:01.095161033] [11351] INFO Camera camera_manager.cpp:293 libcamera v0.0.0+3866-0c55e522
[16:56:01.147896220] [11353] INFO RPI raspberrypi.cpp:1374 Registered camera /base/soc/i2c0mux/i2c@1/pca@70/i2c@0/ov5647@36 to Unicam device /dev/media3 and ISP device /dev/media0
[16:56:01.159155410] [11353] INFO RPI raspberrypi.cpp:1374 Registered camera /base/soc/i2c0mux/i2c@1/pca@70/i2c@1/ov5647@36 to Unicam device /dev/media3 and ISP device /dev/media0
[16:56:01.171238814] [11353] INFO RPI raspberrypi.cpp:1374 Registered camera /base/soc/i2c0mux/i2c@1/pca@70/i2c@2/ov5647@36 to Unicam device /dev/media3 and ISP device /dev/media0
[16:56:01.182519819] [11353] INFO RPI raspberrypi.cpp:1374 Registered camera /base/soc/i2c0mux/i2c@1/pca@70/i2c@3/ov5647@36 to Unicam device /dev/media3 and ISP device /dev/media0
[16:56:01.186400615] [11351] INFO Camera camera.cpp:1035 configuring streams: (0) 640x480-XBGR8888
[16:56:01.186792260] [11353] INFO RPI raspberrypi.cpp:761 Sensor: /base/soc/i2c0mux/i2c@1/pca@70/i2c@0/ov5647@36 - Selected sensor format: 640x480-SGBRG10_1X10 - Selected unicam format: 640x480-pGAA
[16:56:01.572121415] [11362] INFO Camera camera.cpp:1035 configuring streams: (0) 2592x1944-BGR888
[16:56:01.572891001] [11353] INFO RPI raspberrypi.cpp:761 Sensor: /base/soc/i2c0mux/i2c@1/pca@70/i2c@0/ov5647@36 - Selected sensor format: 2592x1944-SGBRG10_1X10 - Selected unicam format: 2592x1944-pGAA
[16:56:02.217833239] [11362] INFO Camera camera.cpp:1035 configuring streams: (0) 640x480-XBGR8888
[16:56:02.220169383] [11353] INFO RPI raspberrypi.cpp:761 Sensor: /base/soc/i2c0mux/i2c@1/pca@70/i2c@0/ov5647@36 - Selected sensor format: 640x480-SGBRG10_1X10 - Selected unicam format: 640x480-pGAA
[16:56:02.639791565] [11362] INFO Camera camera_manager.cpp:293 libcamera v0.0.0+3866-0c55e522
[16:56:02.673383822] [11366] INFO RPI raspberrypi.cpp:1374 Registered camera /base/soc/i2c0mux/i2c@1/pca@70/i2c@0/ov5647@36 to Unicam device /dev/media3 and ISP device /dev/media0
[16:56:02.684330033] [11366] INFO RPI raspberrypi.cpp:1374 Registered camera /base/soc/i2c0mux/i2c@1/pca@70/i2c@1/ov5647@36 to Unicam device /dev/media3 and ISP device /dev/media0
[16:56:02.695247467] [11366] INFO RPI raspberrypi.cpp:1374 Registered camera /base/soc/i2c0mux/i2c@1/pca@70/i2c@2/ov5647@36 to Unicam device /dev/media3 and ISP device /dev/media0
[16:56:02.712178214] [11366] INFO RPI raspberrypi.cpp:1374 Registered camera /base/soc/i2c0mux/i2c@1/pca@70/i2c@3/ov5647@36 to Unicam device /dev/media3 and ISP device /dev/media0
[16:56:02.717658292] [11362] INFO Camera camera.cpp:1035 configuring streams: (0) 640x480-XBGR8888
[16:56:02.718236694] [11366] INFO RPI raspberrypi.cpp:761 Sensor: /base/soc/i2c0mux/i2c@1/pca@70/i2c@1/ov5647@36 - Selected sensor format: 640x480-SGBRG10_1X10 - Selected unicam format: 640x480-pGAA
[16:56:03.144917605] [11375] INFO Camera camera.cpp:1035 configuring streams: (0) 2592x1944-BGR888
[16:56:03.146164945] [11366] INFO RPI raspberrypi.cpp:761 Sensor: /base/soc/i2c0mux/i2c@1/pca@70/i2c@1/ov5647@36 - Selected sensor format: 2592x1944-SGBRG10_1X10 - Selected unicam format: 2592x1944-pGAA
[16:56:03.742975302] [11375] INFO Camera camera.cpp:1035 configuring streams: (0) 640x480-XBGR8888
[16:56:03.745225373] [11366] INFO RPI raspberrypi.cpp:761 Sensor: /base/soc/i2c0mux/i2c@1/pca@70/i2c@1/ov5647@36 - Selected sensor format: 640x480-SGBRG10_1X10 - Selected unicam format: 640x480-pGAA
[16:56:04.156556613] [11366] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video0[14:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:04.156628520] [11366] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video13[15:out]: Unable to request 0 buffers: Device or resource busy
[16:56:04.156673779] [11366] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video14[16:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:04.156716519] [11366] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video15[17:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:04.156757945] [11366] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video16[18:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:04.156841851] [11366] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video16[18:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:04.156901888] [11366] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video15[17:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:04.156957776] [11366] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video14[16:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:04.157013664] [11366] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video13[15:out]: Unable to request 0 buffers: Device or resource busy
[16:56:04.157087553] [11366] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video0[14:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:04.160455891] [11375] INFO Camera camera_manager.cpp:293 libcamera v0.0.0+3866-0c55e522
[16:56:04.194105276] [11379] INFO RPI raspberrypi.cpp:1374 Registered camera /base/soc/i2c0mux/i2c@1/pca@70/i2c@0/ov5647@36 to Unicam device /dev/media3 and ISP device /dev/media0
[16:56:04.205067154] [11379] INFO RPI raspberrypi.cpp:1374 Registered camera /base/soc/i2c0mux/i2c@1/pca@70/i2c@1/ov5647@36 to Unicam device /dev/media3 and ISP device /dev/media0
[16:56:04.216080661] [11379] INFO RPI raspberrypi.cpp:1374 Registered camera /base/soc/i2c0mux/i2c@1/pca@70/i2c@2/ov5647@36 to Unicam device /dev/media3 and ISP device /dev/media0
[16:56:04.227046261] [11379] INFO RPI raspberrypi.cpp:1374 Registered camera /base/soc/i2c0mux/i2c@1/pca@70/i2c@3/ov5647@36 to Unicam device /dev/media3 and ISP device /dev/media0
[16:56:04.230680931] [11375] INFO Camera camera.cpp:1035 configuring streams: (0) 640x480-XBGR8888
[16:56:04.231641458] [11379] INFO RPI raspberrypi.cpp:761 Sensor: /base/soc/i2c0mux/i2c@1/pca@70/i2c@2/ov5647@36 - Selected sensor format: 640x480-SGBRG10_1X10 - Selected unicam format: 640x480-pGAA
[16:56:04.615722641] [11388] INFO Camera camera.cpp:1035 configuring streams: (0) 2592x1944-BGR888
[16:56:04.616655817] [11379] INFO RPI raspberrypi.cpp:761 Sensor: /base/soc/i2c0mux/i2c@1/pca@70/i2c@2/ov5647@36 - Selected sensor format: 2592x1944-SGBRG10_1X10 - Selected unicam format: 2592x1944-pGAA
[16:56:05.248360378] [11388] INFO Camera camera.cpp:1035 configuring streams: (0) 640x480-XBGR8888
[16:56:05.250775467] [11379] INFO RPI raspberrypi.cpp:761 Sensor: /base/soc/i2c0mux/i2c@1/pca@70/i2c@2/ov5647@36 - Selected sensor format: 640x480-SGBRG10_1X10 - Selected unicam format: 640x480-pGAA
[16:56:05.657228065] [11379] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video0[14:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:05.657334027] [11379] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video13[15:out]: Unable to request 0 buffers: Device or resource busy
[16:56:05.657408619] [11379] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video14[16:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:05.657478341] [11379] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video15[17:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:05.657547136] [11379] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video16[18:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:05.657694931] [11379] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video16[18:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:05.657783264] [11379] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video15[17:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:05.657871411] [11379] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video14[16:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:05.657955225] [11379] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video13[15:out]: Unable to request 0 buffers: Device or resource busy
[16:56:05.658062335] [11379] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video0[14:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:05.658364666] [11379] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video0[22:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:05.658440054] [11379] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video13[23:out]: Unable to request 0 buffers: Device or resource busy
[16:56:05.658514646] [11379] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video14[24:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:05.658554849] [11379] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video15[25:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:05.658594367] [11379] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video16[26:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:05.658661385] [11379] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video16[26:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:05.658715570] [11379] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video15[25:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:05.658768977] [11379] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video14[24:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:05.658822569] [11379] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video13[23:out]: Unable to request 0 buffers: Device or resource busy
[16:56:05.658894198] [11379] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video0[22:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:05.662267221] [11388] INFO Camera camera_manager.cpp:293 libcamera v0.0.0+3866-0c55e522
[16:56:05.695693035] [11393] INFO RPI raspberrypi.cpp:1374 Registered camera /base/soc/i2c0mux/i2c@1/pca@70/i2c@0/ov5647@36 to Unicam device /dev/media3 and ISP device /dev/media0
[16:56:05.706551302] [11393] INFO RPI raspberrypi.cpp:1374 Registered camera /base/soc/i2c0mux/i2c@1/pca@70/i2c@1/ov5647@36 to Unicam device /dev/media3 and ISP device /dev/media0
[16:56:05.717484958] [11393] INFO RPI raspberrypi.cpp:1374 Registered camera /base/soc/i2c0mux/i2c@1/pca@70/i2c@2/ov5647@36 to Unicam device /dev/media3 and ISP device /dev/media0
[16:56:05.728370559] [11393] INFO RPI raspberrypi.cpp:1374 Registered camera /base/soc/i2c0mux/i2c@1/pca@70/i2c@3/ov5647@36 to Unicam device /dev/media3 and ISP device /dev/media0
[16:56:05.731986525] [11388] INFO Camera camera.cpp:1035 configuring streams: (0) 640x480-XBGR8888
[16:56:05.732365521] [11393] INFO RPI raspberrypi.cpp:761 Sensor: /base/soc/i2c0mux/i2c@1/pca@70/i2c@3/ov5647@36 - Selected sensor format: 640x480-SGBRG10_1X10 - Selected unicam format: 640x480-pGAA
[16:56:06.162525803] [11402] INFO Camera camera.cpp:1035 configuring streams: (0) 2592x1944-BGR888
[16:56:06.163983715] [11393] INFO RPI raspberrypi.cpp:761 Sensor: /base/soc/i2c0mux/i2c@1/pca@70/i2c@3/ov5647@36 - Selected sensor format: 2592x1944-SGBRG10_1X10 - Selected unicam format: 2592x1944-pGAA
[16:56:06.783882959] [11402] INFO Camera camera.cpp:1035 configuring streams: (0) 640x480-XBGR8888
[16:56:06.786130660] [11393] INFO RPI raspberrypi.cpp:761 Sensor: /base/soc/i2c0mux/i2c@1/pca@70/i2c@3/ov5647@36 - Selected sensor format: 640x480-SGBRG10_1X10 - Selected unicam format: 640x480-pGAA
[16:56:07.198193759] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video0[14:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:07.198335036] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video13[15:out]: Unable to request 0 buffers: Device or resource busy
[16:56:07.198442535] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video14[16:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:07.198544571] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video15[17:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:07.198645162] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video16[18:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:07.198832383] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video16[18:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:07.198973437] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video15[17:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:07.199109713] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video14[16:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:07.199243860] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video13[15:out]: Unable to request 0 buffers: Device or resource busy
[16:56:07.199414877] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video0[14:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:07.200073815] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video0[22:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:07.200192203] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video13[23:out]: Unable to request 0 buffers: Device or resource busy
[16:56:07.200292239] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video14[24:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:07.200389016] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video15[25:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:07.200484849] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video16[26:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:07.200650051] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video16[26:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:07.200788012] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video15[25:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:07.200921826] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video14[24:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:07.201052103] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video13[23:out]: Unable to request 0 buffers: Device or resource busy
[16:56:07.201219545] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video0[22:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:07.201917094] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video0[30:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:07.202027427] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video13[31:out]: Unable to request 0 buffers: Device or resource busy
[16:56:07.202126018] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video14[32:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:07.202222147] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video15[33:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:07.202317646] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video16[34:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:07.202475237] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video16[34:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:07.202609736] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video15[33:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:07.202743068] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video14[32:cap]: Unable to request 0 buffers: Device or resource busy
[16:56:07.202875326] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video13[31:out]: Unable to request 0 buffers: Device or resource busy
[16:56:07.203038769] [11393] ERROR V4L2 v4l2_videodevice.cpp:1234 /dev/video0[30:cap]: Unable to request 0 buffers: Device or resource busy
Huh, that's pretty weird. Requesting zero buffers is what you do when you shut stuff down. I wonder if there's some weird thing where close doesn't tidy up synchronously, so we can actually create the new Picamera2 (which now holds all those devices) before the old instance tries to clear up (and fails). What happens if you put a short sleep in before you re-create the Picamera2 object?
Even with a 10s delay prior to re-creating Picamera2, still gives me libcamera errors.
I also found something else interesting, I found a case where using .close() with the change in here, doesn't work -
#!/usr/bin/python3
import picamera2
def takephoto(cam):
picam2 = picamera2.Picamera2(camera_num=cam)
capture_config = picam2.create_still_configuration()
picam2.start()
picam2.switch_mode_and_capture_file(capture_config, f"cam{cam}.jpg")
picam2.stop()
picam2.close()
# Works --
for cam in range(4):
takephoto(cam)
# Doesn't work -
for cam in range(4):
picam2 = picamera2.Picamera2(camera_num=cam)
capture_config = picam2.create_still_configuration()
picam2.start()
picam2.switch_mode_and_capture_file(capture_config, f"cam{cam}.jpg")
picam2.stop()
picam2.close()
I'll try and do some more digging into what's happing re. garbage collection/deletion. I found a way to get all objects from the garbage collector module ( gc.get_objects() )