树莓派ARM64系统中如何设置opencv-python读取高清摄像头画面

问题

树莓派4B 8G,2021-10-30-raspios-bullseye-arm64.imgLinux raspberrypi 5.10.63-v8+ #1459 SMP PREEMPT Wed Oct 6 16:42:49 BST 2021 aarch64 GNU/Linux,用vcgencmd get_camera监测,输出support=0 detected=0
发现使用miniconda安装的python 3.9中读取树莓派4B摄像头帧时只能以480p分辨率读取,就算代码中定义了cap的长宽也无法获取其他分辨率的图片。
树莓派的ARM64位系统用的是Bullseye版本,其中没有raspistill库,因为新版本系统舍弃了旧版本的驱动(New default camera subsystem based on libcamera. New camera demo applications (libcamera-still and libcamera-vid) have replaced raspistill and raspivid),并且我尝试自己编译userland libraries(https://github.com/robidouille/robidouille/tree/master/raspicam_cv)是提示不支持64位系统,所以转而寻找其他方法。
树莓派的ARM64位系统也不支持picamera库,缺少’libmmal.so’库的32位支持。因此,只能从驱动和opencv下手。

解决方案

/boot/config.txtcamera_auto_detect=1注释掉,加入start_x=1,设置 gpu_mem=512 ;将/etc/modules中只保留i2c-dev

sudo nano  /boot/config.txt
    # Automatically load overlays for detected cameras
    #camera_auto_detect=1   注释这一行
    start_x=1    			加入这一行
    gpu_mem=512   			大于128即可

sudo nano /etc/modules
    i2c-dev
    #bcm2835-v4l2

conda install opencv
vcgencmd get_camera
import cv2, time
# cap = cv2.VideoCapture('/dev/video0', cv2.CAP_V4L)
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 2592)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1944)
# cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))

t=time.time()
for i in range(10):
    ret, frame = cap.read()
print((time.time()-t)/10,'s')
print(ret, frame.shape)
# frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
cv2.imwrite('/home/pi/Desktop/image.jpg', frame)
cap.release()

参考资料

https://zhuanlan.zhihu.com/p/435575418
https://www.raspberrypi.com/documentation/accessories/camera.html#python-bindings-for-libcamera
https://blog.forgiveher.cn/posts/1574671872/
https://github.com/kabelhorst/userland
https://github.com/robidouille/robidouille/tree/master/raspicam_cv

官方文档提到

To override the automatic camera detection, Bullseye users will also need to delete the entry
camera_auto_detect=1
if present in the config.txt file. If you need to do edit this file then your Raspberry Pi will need to be rebooted. Setting camera_auto_detect=0 disables the boot time detection completely.

上一篇:Unity3D c# 使对象物体始终面向摄像机


下一篇:Cesium基础知识-键盘鼠标事件