调试开发摄像头,难免会出现程序崩掉,没有能够释放摄像头资源,导致再次打开摄像头失败,出现 Device or resource busy, 设备或资源忙。错误如下:
[3:44:57.208889526] [2819] INFO Camera camera_manager.cpp:284 libcamera v0.1.0+118-563cd78e
[3:44:57.227755605] [2822] INFO RPI pisp.cpp:653 libpisp version v1.0.2 fa44a258644a 22-11-2023 (21:59:22)
[3:44:57.228626716] [2822] ERROR V4L2 v4l2_device.cpp:353 'imx477 6-001a': Unable to set controls: Device or resource busy
[3:44:57.238122459] [2822] INFO RPI pisp.cpp:1112 Registered camera /base/axi/pcie@120000/rp1/i2c@88000/imx477@1a to CFE device /dev/media0 and ISP device /dev/media2 using PiSP variant BCM2712_C0
[3:44:57.238191756] [2822] INFO RPI pisp.cpp:653 libpisp version v1.0.2 fa44a258644a 22-11-2023 (21:59:22)
[3:44:57.247154628] [2822] INFO RPI pisp.cpp:1112 Registered camera /base/axi/pcie@120000/rp1/i2c@80000/imx477@1a to CFE device /dev/media1 and ISP device /dev/media3 using PiSP variant BCM2712_C0
[3:44:57.249098944] [2819] INFO Camera camera_manager.cpp:284 libcamera v0.1.0+118-563cd78e
[3:44:57.267723744] [2827] INFO RPI pisp.cpp:653 libpisp version v1.0.2 fa44a258644a 22-11-2023 (21:59:22)
[3:44:57.268346911] [2827] ERROR V4L2 v4l2_device.cpp:353 'imx477 6-001a': Unable to set controls: Device or resource busy
[3:44:57.277560432] [2827] INFO RPI pisp.cpp:1112 Registered camera /base/axi/pcie@120000/rp1/i2c@88000/imx477@1a to CFE device /dev/media0 and ISP device /dev/media2 using PiSP variant BCM2712_C0
[3:44:57.277625210] [2827] INFO RPI pisp.cpp:653 libpisp version v1.0.2 fa44a258644a 22-11-2023 (21:59:22)
[3:44:57.287310249] [2827] INFO RPI pisp.cpp:1112 Registered camera /base/axi/pcie@120000/rp1/i2c@80000/imx477@1a to CFE device /dev/media1 and ISP device /dev/media3 using PiSP variant BCM2712_C0
[3:44:57.288244787] [2819] INFO Camera camera.cpp:990 Pipeline handler in use by another process
Camera __init__ sequence did not complete.
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 254, in __init__
self._open_camera()
File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 465, in _open_camera
self.camera.acquire()
RuntimeError: Failed to acquire camera: Device or resource busyDuring handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/pi/camnet/mjpeg_server.py", line 86, in <module>
picam2 = Picamera2()
^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 266, in __init__
raise RuntimeError("Camera __init__ sequence did not complete.")
RuntimeError: Camera __init__ sequence did not complete.
当然,解决该问题,最简单的办法就是重启电脑。
我们仔细查看错误,发现是 /dev/media* 设备打不开,用该命令查看资源被占用情况:
lsof /dev/media2
显示:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
pipewire 1427 pi 59u CHR 238,2 0t0 572 /dev/media2
wireplumb 1430 pi 39u CHR 238,2 0t0 572 /dev/media2
python 2535 pi 12uW CHR 238,2 0t0 572 /dev/media2
看到有一个python程序,PID为2535占用了资源,可以使用该命令详细查看该PID情况:
ps axl | grep 2535
结束掉该进程即可释放资源:
kill -9 2535
再次查看 lsof /dev/media2
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
pipewire 1427 pi 59u CHR 238,2 0t0 572 /dev/media2
wireplumb 1430 pi 39u CHR 238,2 0t0 572 /dev/media2
[1]+ Killed python mjpeg_server_2.py
看到该进程已经被kill掉了,现在可以正常使用相机了。