Difference between revisions of "V4L2 mode for Raspberry Pi/zh"

From wiki_veye
Jump to navigation Jump to search
 
(59 intermediate revisions by 2 users not shown)
Line 11: Line 11:
  
 
本文描述怎样在树莓派平台,通过V4L2方式调用VEYE系列和CS系列的摄像头模组。
 
本文描述怎样在树莓派平台,通过V4L2方式调用VEYE系列和CS系列的摄像头模组。
 +
====支持的模组:====
 +
{| class="wikitable"
 +
|+模组型号支持表
 +
!系列
 +
!型号
 +
! 状态
 +
|-
 +
|VEYE系列
 +
|VEYE-MIPI-IMX327S
 +
|完成
 +
|-
 +
|VEYE系列
 +
|VEYE-MIPI-IMX385
 +
|完成
 +
|-
 +
|VEYE系列
 +
|VEYE-MIPI-IMX462
 +
|完成
 +
|-
 +
|CS系列
 +
|CS-MIPI-IMX307
 +
|完成
 +
|-
 +
|CS系列
 +
|CS-MIPI-SC132
 +
|完成
 +
|}VEYE系列和CS系列MIPI摄像头模组是内置ISP的,输出为MIPI-CSI2接口。
  
 +
此外,现在也支持FPD-Link3模式的连接。
 
=== 硬件准备及安装 ===
 
=== 硬件准备及安装 ===
 
[[VEYE MIPI 290/327 for Raspberry Pi/zh#.E7.A1.AC.E4.BB.B6.E5.87.86.E5.A4.87.E5.8F.8A.E5.AE.89.E8.A3.85|VEYE系列的硬件准备及安装]]
 
[[VEYE MIPI 290/327 for Raspberry Pi/zh#.E7.A1.AC.E4.BB.B6.E5.87.86.E5.A4.87.E5.8F.8A.E5.AE.89.E8.A3.85|VEYE系列的硬件准备及安装]]
Line 21: Line 49:
  
 
=== 驱动安装 ===
 
=== 驱动安装 ===
 +
====下载驱动包====
 +
<code>wget <nowiki>https://github.com/veyeimaging/raspberrypi_v4l2/releases/latest/download/raspberrypi_v4l2.tgz</nowiki></code>
 +
====安装驱动 ====
 +
<code>tar -xzvf raspberrypi_v4l2.tgz</code>
  
==== 下载驱动包 ====
 
<code>git clone https://github.com/veyeimaging/raspberrypi_v4l2<nowiki/>.git</code><code><nowiki/></code>
 
 
==== 安装驱动 ====
 
 
<code>cd raspberrypi_v4l2/release/</code>
 
<code>cd raspberrypi_v4l2/release/</code>
  
 
<code>chmod +x *</code>
 
<code>chmod +x *</code>
  
<code>sudo ./install_driver.sh [camera module]</code>
+
* 对于树莓派5
 +
 
 +
<code>sudo ./install_driver_rpi5.sh [camera module]</code>
 +
 
 +
会在/boot/config.txt中安装CAM1和CAM0两个dtvoerlay。
  
camera module:可以是veye327,csimx307,cssc132,veyecam2m等。
+
* 其他树莓派
  
'''备注:veyecam2m是新版本驱动,可以替代veye327,支持VEYE系列200w分辨率的所有模组。'''
+
<code>sudo ./install_driver.sh camera_module_name</code>
 +
 
 +
然后重启树莓派。
 +
 
 +
camera_module_name:可以是csimx307,cssc132,veyecam2m等。
 +
 
 +
备注:如果提示无法找到对应版本的驱动,说明我们没有提供对应您的piOS版本编译后的驱动程序。请尝试从源码编译。
 +
 
 +
'''备注:veyecam2m是新版本驱动,可以替代veye327,支持VEYE系列200w分辨率的所有模组。请使用veyecam2m,而不是veye327,以后我们会废弃掉veye327.ko。'''
  
 
然后重启树莓派。
 
然后重启树莓派。
Line 45: Line 85:
 
如需更换为raspicam模式,或者想要更换成其他摄像头模组型号的驱动,必须要先卸载现在的驱动。
 
如需更换为raspicam模式,或者想要更换成其他摄像头模组型号的驱动,必须要先卸载现在的驱动。
  
<code>sudo ./uninstall_driver.sh [camera module]</code>
+
<code>sudo ./uninstall_driver.sh camera_module_name</code>
  
camera module:可以是veye327,csimx307,cssc132等。
+
camera_module_name:可以是veyecam2m,csimx307,cssc132等。
  
==== 列出支持的模式 ====
+
==== FPD-Link3模式的相机 ====
<code>v4l2-ctl --list-formats-ext</code>
+
对于FPD-Link3传输模式的相机,需要首先保证ds90ub954驱动的加载并完成初始化,然后才能加载摄像头驱动并进行探测。下面以FPD-Link3模式的VEYE-MIPI-IMX327S为例:
 +
 
 +
正常安装驱动:
 +
 
 +
<code>sudo ./install_driver.sh ds90ub954</code>
  
 +
<code>sudo ./install_driver.sh veyecam2m</code>
 
=== 摄像头状态验证 ===
 
=== 摄像头状态验证 ===
 
以veye327为例:
 
以veye327为例:
Line 70: Line 115:
  
 
<code>veyecam2m camera probed</code>
 
<code>veyecam2m camera probed</code>
 +
==== 列出支持的模式 ====
 +
<code>v4l2-ctl --list-formats-ext</code>
 +
===树莓派5的配置===
 +
在树莓派5上,驱动程序现在使用media control API,我们必须先正确设置media graph。这包括正确设置media pad的格式并正确链接它们。
  
 +
我们提供了一系列脚本实现这个功能,保存在rpi5_scripts目录。
 +
 +
*./find_entity.sh
 +
 +
<code>$ ./find_entity.sh</code>
 +
 +
<code>Found veyecam2m @ i2c-6 entity on /dev/media0</code>
 +
 +
<code>Plese get frame from /dev/video0 and use /dev/v4l-subdev2 for camera setting.</code>
 +
 +
树莓派5重启之后,相机的media node,video node可能发生变化,因此,建议执行后续操作前执行<code>./find_entity.sh</code>以明确设备节点。
 +
 +
上述提示信息中,可以看到系统插入了两个相机,以及每个相机的对应的设备节点。
 +
 +
<code>i2c-4</code>对应板子上的CAM1插口,<code>i2c-6</code>对应板子上的CAM0插口。
 +
 +
*media_setting_rpi5.sh
 +
 +
<code>./media_setting_rpi5.sh</code>
 +
 +
<code>Usage: ./media_setting_rpi5.sh veyecam2m/csimx307/cssc132/mvcam -fmt [UYVY/RAW8/RAW10/RAW12] -w [width] -h [height]</code>
 +
 +
<code>This shell script is designed to detect the connection of a camera on Raspberry Pi 5.</code>
 +
 +
<code>    It utilizes media-ctl and v4l2-ctl commands to configure the linking relationships and data formats of the media pad.</code>
 +
 +
<code>    Once completed, you can directly use /dev/video0 or /dev/video8 to obtain image data</code>
 +
 +
'''注意:每次启动后,都必须执行此脚本以完成参数的配置,然后再进行后续操作。'''
 +
 +
举例:
 +
 +
<code>./media_setting_rpi5.sh veyecam2m</code>
 +
 +
=== 实时预览 ===
 +
在树莓派5上,vlc进行播放现在有问题,不能使用。
 +
 +
====使用VLC预览图像====
 +
1. Open VLC with command line <code>vlc</code> , or click the icon to launch.
 +
 +
2. Hit the <code>▶</code>(Play) button to call the open media window.
 +
 +
3. In <code>Capture Device</code> >> <code>Device Selection</code> >> <code>Video device name</code>, select the camera video node.
 +
 +
4. Hit <code>Advanced Options...</code> button
 +
 +
5. In Advanced Options window >> <code>Video input chroma format</code>, type <code>UYUV</code>.
 +
 +
6. Type in the <code>width</code> and <code>height</code>, for example, 1920 and 1080.
 +
 +
7. Hit <code>OK</code> to save the settings and see the video feed.[[File:Play camera using VLC on RPI.png|alt=Play camera using VLC on RPI|center|thumb|1000x1000px|Play camera using VLC on RPI|link=http://wiki.veye.cc/index.php/File:Play_camera_using_VLC_on_RPI.png]]<br />
 +
====使用qv4l2播放图像====
 +
首先安装qv4l2
 +
 +
<code>sudo apt install qv4l2</code>
 +
 +
1. Open VLC with command line <code>vlc</code> to launch.
 +
 +
2. Hit the <code>▶</code>(Play) button to call the open media window.
 
=== Gstreamer应用范例 ===
 
=== Gstreamer应用范例 ===
 
<code>export DISPLAY=:0</code>
 
<code>export DISPLAY=:0</code>
  
* 安装gstreamer
+
==== 安装gstreamer ====
 +
<code>sudo apt-get install gstreamer1.0-tools</code>
  
<code>sudo apt-get install gstreamer1.0-tools</code>
+
<code>sudo apt-get install libx264-dev libjpeg-dev</code>
 +
 
 +
<code>sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgstreamer-plugins-bad1.0-dev gstreamer1.0-plugins-ugly gstreamer1.0-tools gstreamer1.0-gl gstreamer1.0-gtk3</code>
  
* 抓拍一张图片(VEYE-MIPI-327,CS-MIPI-IMX307 @1080p mode)
+
==== 运行 gstreamer videotest 命令 ====
 +
<code>gst-launch-1.0 videotestsrc ! videoconvert ! autovideosink</code>
  
 +
==== 抓拍一张图片(VEYE-MIPI-327,CS-MIPI-IMX307 @1080p mode) ====
 
<code>gst-launch-1.0 v4l2src num-buffers=1 device=/dev/video0 ! 'video/x-raw, format=(string)UYVY, width=1920,height=1080' ! jpegenc ! filesink location=test_image.jpg</code>
 
<code>gst-launch-1.0 v4l2src num-buffers=1 device=/dev/video0 ! 'video/x-raw, format=(string)UYVY, width=1920,height=1080' ! jpegenc ! filesink location=test_image.jpg</code>
  
* 视频预览(VEYE-MIPI-327,CS-MIPI-IMX307 @1080p mode)
+
==== 帧率测试(VEYE-MIPI-327,CS-MIPI-IMX307 @1080p mode) ====
 +
<code>gst-launch-1.0 v4l2src device=/dev/video0 ! "video/x-raw,format=(string)UYVY, width=(int)1920, height=(int)1080,framerate=(fraction)30/1" ! videoconvert ! fpsdisplaysink video-sink=fakesink -v</code>
 +
 
 +
==== 视频预览(VEYE-MIPI-327,CS-MIPI-IMX307 @1080p mode) ====
 +
<code>gst-launch-1.0 v4l2src io-mode=dmabuf device=/dev/video0 ! "video/x-raw,format=(string)UYVY, width=(int)1920, height=(int)1080,framerate=(fraction)30/1" ! v4l2convert capture-io-mode=dmabuf output-io-mode=dmabuf ! autovideosink sync=false -v</code>
  
<code>gst-launch-1.0 v4l2src device=/dev/video0 ! "video/x-raw,format=(string)UYVY, width=(int)1920, height=(int)1080,framerate=(fraction)30/1" ! videoconvert ! autovideosink sync=false -v</code>
+
或者:
  
*视频预览(CS-MIPI-IMX307,CS-MIPI-SC132 @720p mode)
+
<code>gst-launch-1.0 v4l2src device=/dev/video0 ! "video/x-raw,format=(string)UYVY, width=(int)1920, height=(int)1080,framerate=(fraction)30/1" ! videoconvert ! autovideosink  sync=false -v</code>
  
 +
==== 视频预览并加时间戳显示(VEYE-MIPI-327,CS-MIPI-IMX307 @1080p mode) ====
 +
<code>gst-launch-1.0 v4l2src device=/dev/video0 ! "video/x-raw,format=(string)UYVY, width=(int)1920, height=(int)1080,framerate=(fraction)30/1"  ! videoconvert ! videoscale ! clockoverlay time-format="%D %H:%M:%S" ! video/x-raw, width=640, height=360 ! autovideosink sync=false -v</code>
 +
 +
==== 视频预览(CS-MIPI-IMX307,CS-MIPI-SC132 @720p mode) ====
 
<code>gst-launch-1.0 v4l2src device=/dev/video0 ! "video/x-raw,format=(string)UYVY, width=(int)1280, height=(int)720,framerate=(fraction)60/1" ! videoconvert ! autovideosink sync=false -v</code>
 
<code>gst-launch-1.0 v4l2src device=/dev/video0 ! "video/x-raw,format=(string)UYVY, width=(int)1280, height=(int)720,framerate=(fraction)60/1" ! videoconvert ! autovideosink sync=false -v</code>
  
*视频预览(CS-MIPI-IMX307,CS-MIPI-SC132 @vga mode)
+
==== 视频预览(CS-MIPI-IMX307,CS-MIPI-SC132 @vga mode) ====
 +
<code>gst-launch-1.0 v4l2src device=/dev/video0 ! "video/x-raw,format=(string)UYVY, width=(int)640, height=(int)480" ! videoconvert ! autovideosink sync=false -v</code>
 +
 
 +
==== 视频预览(CS-MIPI-SC132 @1280*1080@45 mode) ====
 +
<code>gst-launch-1.0 v4l2src device=/dev/video0 ! "video/x-raw,format=(string)UYVY, width=(int)1280, height=(int)1080,framerate=(fraction)45/1" ! videoconvert ! autovideosink sync=false -v</code>
 +
 
 +
==== h.264编码并保存为mkv文件(VEYE-MIPI-X,CS-MIPI-IMX307 @1080p mode) ====
 +
<code>gst-launch-1.0 -e v4l2src io-mode=dmabuf device=/dev/video0 num-buffers=300 ! "video/x-raw,format=(string)UYVY, width=(int)1920, height=(int)1080,framerate=(fraction)30/1" ! v4l2h264enc capture-io-mode=dmabuf output-io-mode=dmabuf extra-controls="controls, h264_profile=4, video_bitrate=6200000" ! 'video/x-h264, profile=high, level=(string)4' ! h264parse ! matroskamux ! filesink location=output.mkv</code>
 +
 
 +
==== h.264编码并保存为mp4文件(VEYE-MIPI-X,CS-MIPI-IMX307 @1080p mode) ====
 +
<code>gst-launch-1.0 -e v4l2src io-mode=dmabuf device=/dev/video0  num-buffers=300 ! "video/x-raw,format=(string)UYVY, width=(int)1920, height=(int)1080,framerate=(fraction)30/1" ! v4l2h264enc capture-io-mode=dmabuf output-io-mode=dmabuf extra-controls="controls, h264_profile=4, video_bitrate=6200000" ! 'video/x-h264, profile=high, level=(string)4' ! h264parse ! mp4mux ! filesink location=video.mp4</code>
 +
==== 网络传输 TCP streaming====
 +
'''''树莓派(Server)一侧执行'''''
 +
 
 +
<code>gst-launch-1.0 -v v4l2src device=/dev/video0 num-buffers=-1 ! "video/x-raw,format=(string)UYVY, width=(int)1920, height=(int)1080,framerate=(fraction)30/1" ! v4l2h264enc extra-controls="controls, h264_profile=4, video_bitrate=4000000" ! 'video/x-h264, profile=high, level=(string)4' ! h264parse ! rtph264pay config-interval=1 pt=96 ! gdppay ! tcpserversink  host=x.x.x.x port=5000</code>
 +
 
 +
码流带宽为4Mbps,持续传输,监听端口为5000。
 +
 
 +
'''''Client一侧执行'''''
 +
 
 +
<code>gst-launch-1.0 -v tcpclientsrc host=x.x.x.x port=5000 ! gdpdepay ! rtph264depay ! avdec_h264 ! autovideosink sync=false</code>
 +
 
 +
其中x.x.x.x为树莓派(Server)的IP地址。
 +
 
 +
Client一侧,如为windows系统,建议使用powershell。[https://gstreamer.freedesktop.org/ gstreamer]的windows版本[https://gstreamer.freedesktop.org/documentation/installing/on-windows.html?gi-language=c 下载地址]。只安装runtime files即可。
 +
 
 +
=== 使用opencv获取图片 ===
 +
<code>pip uninstall opencv-python</code>
 +
 
 +
<code>sudo apt install python3-opencv</code>
 +
 
 +
为了将摄像头数据从v4l2设备导入到opencv,我们提供了几个[https://github.com/veyeimaging/raspberrypi_v4l2/tree/main/samples/opencv sample]。
 +
===V4l2-ctl 应用范例===
 +
====安装v4l2-utils====
 +
<code>sudo apt-get install v4l-utils</code>
 +
====v4l2-ctl例程====
 +
 
 +
*抓拍一张UYVY原始数据图片 (1080p mode)
 +
 
 +
<code>v4l2-ctl --set-fmt-video=width=1920,height=1080,pixelformat=UYVY --stream-mmap --stream-count=1 --stream-to=uyvy-1920x1080.yuv</code>
  
<code>gst-launch-1.0 v4l2src device=/dev/video0 ! "video/x-raw,format=(string)UYVY, width=(int)640, height=(int)480" ! videoconvert ! autovideosink sync=false -v</code>
+
* 抓拍10张UYVY原始数据图片 (1080p mode)
 +
 
 +
<code>v4l2-ctl --set-fmt-video=width=1920,height=1080,pixelformat=UYVY --stream-mmap --stream-count=10 --stream-to=uyvy-1920x1080_stream.yuv</code>
  
*视频预览(CS-MIPI-SC132 @1280*1080@45 mode)
+
=== yavta 应用范例 ===
  
<code>gst-launch-1.0 v4l2src device=/dev/video0 ! "video/x-raw,format=(string)UYVY, width=(int)1280, height=(int)1080,framerate=(fraction)45/1" ! videoconvert ! autovideosink sync=false -v</code>
+
==== 下载并编译yavta ====
 +
<code>git clone <nowiki>git://git.ideasonboard.org/yavta.git</nowiki></code>
  
* 帧率测试(VEYE-MIPI-327,CS-MIPI-IMX307 @1080p mode)
+
<code>cd yavta;make</code>
  
<code>gst-launch-1.0 v4l2src device=/dev/video0 ! "video/x-raw,format=(string)UYVY, width=(int)1920, height=(int)1080,framerate=(fraction)30/1" ! videoconvert ! fpsdisplaysink video-sink=fakesink -v</code>
+
==== yavta 例程 ====
 +
<code>./yavta -c8 -Fuyvy_1920X1080.yuv --skip 0 -f UYVY -s 1920x1080 /dev/video0</code>
  
 +
PS, UYVY文件可以用这个播放器: [https://yuv-player-deluxe.software.informer.com/2.6/ YUV Displayer Deluxe].
 
=== 参数控制软件包使用 ===
 
=== 参数控制软件包使用 ===
 
由于我们的摄像头参数自由度比较高,并没有采用V4L2参数进行控制,而是使用脚本进行参数配置。
 
由于我们的摄像头参数自由度比较高,并没有采用V4L2参数进行控制,而是使用脚本进行参数配置。
  
https://github.com/veyeimaging/raspberrypi/tree/master/i2c_cmd
+
https://github.com/veyeimaging/raspberrypi_v4l2/tree/main/i2c_cmd
  
 
[[VEYE MIPI 290/327 for Raspberry Pi/zh#.E8.A7.86.E9.A2.91.E6.8E.A7.E5.88.B6.E8.BD.AF.E4.BB.B6.E5.8C.85.E4.BD.BF.E7.94.A8|VEYE系列参数控制软件包使用说明]]<br />[[CS-MIPI-X for Raspberry Pi/zh#.E8.A7.86.E9.A2.91.E6.8E.A7.E5.88.B6.E8.BD.AF.E4.BB.B6.E5.8C.85.E4.BD.BF.E7.94.A8|CS系列参数控制软件包使用说明]]
 
[[VEYE MIPI 290/327 for Raspberry Pi/zh#.E8.A7.86.E9.A2.91.E6.8E.A7.E5.88.B6.E8.BD.AF.E4.BB.B6.E5.8C.85.E4.BD.BF.E7.94.A8|VEYE系列参数控制软件包使用说明]]<br />[[CS-MIPI-X for Raspberry Pi/zh#.E8.A7.86.E9.A2.91.E6.8E.A7.E5.88.B6.E8.BD.AF.E4.BB.B6.E5.8C.85.E4.BD.BF.E7.94.A8|CS系列参数控制软件包使用说明]]
Line 112: Line 277:
 
cm4支持两路摄像头同时使用,采用上面的步骤安装的dtbo将只能使用CAM1。如需使用两路摄像头,请按如下步骤:
 
cm4支持两路摄像头同时使用,采用上面的步骤安装的dtbo将只能使用CAM1。如需使用两路摄像头,请按如下步骤:
  
 +
===== 硬件连接 =====
 +
参考树莓派这个[https://www.raspberrypi.com/documentation/computers/compute-module.html 说明],将J6进行短接。
 
==== 更新dt-blob.bin ====
 
==== 更新dt-blob.bin ====
<code>wget <nowiki>https://www.raspberrypi.org/documentation/hardware/computemodule/dt-blob-dualcam.bin</nowiki></code>
+
<code>sudo wget <nowiki>https://datasheets.raspberrypi.com/cmio/dt-blob-dualcam.bin</nowiki> -O /boot/dt-blob.bin</code>
  
<code>sudo cp dt-blob-dualcam.bin /boot/dt-blob.bin</code>
+
==== 对于Buster系统(kernel5.10) ====
 +
更新双摄像头的dtbo,以csimx307为例:
  
==== 更新双摄像头的dtbo ====
+
<code>sudo cp raspberrypi_v4l2/release/driver_bin/$(uname -r)/csimx307-dual-cm4.dtbo /boot/overlays/csimx307.dtbo</code>
以csimx307,5.10.17-v7l+版本kernel为例:
 
  
<code>sudo cp raspberrypi_v4l2/release/driver_bin/5.10.17-v7l+/csimx307-dual-cm4.dtbo /boot/overlays/csimx307.dtbo</code>
+
<code>sudo reboot</code>
 +
 
 +
==== 对于Bullseye系统(kernel5.15+) ====
 +
手动编辑<code>/boot/config.txt</code>文件,增加一行<code>dtoverlay=[camera],cam0</code>,如下所示:
 +
 
 +
<code>[all]</code>
 +
 
 +
<code>dtparam=i2c_vc=on</code>
  
<code>sudo reboot</code>
+
<code>dtoverlay=veyecam2m</code>
 +
 
 +
<code>dtoverlay=veyecam2m,cam0</code>
  
 
==== 设备文件说明 ====
 
==== 设备文件说明 ====
Line 137: Line 313:
 
|CAM1
 
|CAM1
 
|10
 
|10
|video2
+
|video2(Buster),video1(Bullseye)
 
|}
 
|}
 
注:只连一个摄像头,无论CAM0还是CAM1,都是video0。
 
注:只连一个摄像头,无论CAM0还是CAM1,都是video0。
Line 146: Line 322:
 
* CAM0视频预览(VEYE-MIPI-327,CS-MIPI-IMX307 @1080p mode)
 
* CAM0视频预览(VEYE-MIPI-327,CS-MIPI-IMX307 @1080p mode)
  
<code>gst-launch-1.0 v4l2src device=/dev/video0 ! "video/x-raw,format=(string)UYVY, width=(int)1920, height=(int)1080,framerate=(fraction)30/1" ! videoconvert ! fpsdisplaysink videosink=autovideosink sync=false text-overlay=false -v</code>
+
<code>gst-launch-1.0 v4l2src io-mode=dmabuf device=/dev/video0 ! "video/x-raw,format=(string)UYVY, width=(int)1920, height=(int)1080,framerate=(fraction)30/1" ! v4l2convert capture-io-mode=dmabuf output-io-mode=dmabuf ! autovideosink sync=false -v</code>
  
 
* CAM1视频预览(VEYE-MIPI-327,CS-MIPI-IMX307 @1080p mode)
 
* CAM1视频预览(VEYE-MIPI-327,CS-MIPI-IMX307 @1080p mode)
  
<code>gst-launch-1.0 v4l2src device=/dev/video2 ! "video/x-raw,format=(string)UYVY, width=(int)1920, height=(int)1080,framerate=(fraction)30/1" ! videoconvert ! fpsdisplaysink videosink=autovideosink sync=false text-overlay=false -v</code>
+
<code>gst-launch-1.0 v4l2src io-mode=dmabuf device=/dev/video1 ! "video/x-raw,format=(string)UYVY, width=(int)1920, height=(int)1080,framerate=(fraction)30/1" ! v4l2convert capture-io-mode=dmabuf output-io-mode=dmabuf ! autovideosink sync=false -v</code>
 
 
=== 源代码编译方法 ===
 
本节主要参考资料为[https://www.raspberrypi.org/documentation/linux/kernel/building.md 官方piOS编译方法]。我们采用交叉编译的方法,5.4.72版本32bitOS为例进行说明。
 
 
 
以下操作均在ubuntu PC上进行。
 
 
 
==== 开发环境准备 ====
 
<code>sudo apt install git bc bison flex libssl-dev make libc6-dev libncurses5-dev</code>
 
 
 
<code>sudo apt install crossbuild-essential-armhf</code>
 
 
 
==== 下载标准版本 ====
 
 
 
===== 首先,请确认自己的树莓派上的piOS版本: =====
 
 
 
* Release version
 
 
 
<code>$ uname -a</code>
 
 
 
<code>Linux raspberrypi 5.4.72-v7l+ #1356 SMP Thu Oct 22 13:57:51 BST 2020 armv7l GNU/Linux</code>
 
 
 
* Code tag
 
 
 
<code>$ cp /usr/share/doc/raspberrypi-bootloader/changelog.Debian.gz ./</code>
 
 
 
<code>gunzip changelog.Debian.gz</code>
 
 
 
查看最上面一条,确定代码tag。
 
 
 
===== 有两种方法可以得到自己想要的版本的源码: =====
 
在PC上:
 
 
 
1. 采用git获取到对应branch上的对应tag版本代码
 
 
 
<code>git clone --branch rpi-5.4.y <nowiki>https://github.com/raspberrypi/linux</nowiki></code>
 
 
 
<code>git checkout raspberrypi-kernel_1.20201022-1</code>
 
 
 
PS:以上两条命令请替换为自己对应的版本。
 
 
 
2. 直接从如下链接手动下载对应tag的版本代码
 
 
 
https://github.com/raspberrypi/linux/tags
 
 
 
==== patch我们的驱动代码 ====
 
 
 
* Driver source code
 
 
 
camera驱动源码路径为: linux/drivers/media/i2c,将camera驱动的源码放到对应目录。
 
 
 
* Modify the the Makefile and Kconfig files
 
 
 
修改同路径下的Config和Makefile,增加对应camera驱动。
 
 
 
* dts file
 
  
dts文件路径为:linux/arch/arm/boot/dts/overlays,将[camera]-overlay.dts文件放到此路径。
+
=== 从源代码编译驱动方法 ===
 +
请参考:[[Build drivers from source for rpi/zh|树莓派平台上摄像头驱动源码编译指南]]
  
* Modify dts Makefile
+
=== 本文修改记录 ===
  
修改同路径下的Makefile,增加对应dts编译选项。
+
* 2024-01-01
  
==== 编译 ====
+
增加对树莓派5的相关说明。
  
===== 编译准备 =====
+
* 20230607
<code>export ARCH=arm</code>
 
  
<code>export CROSS_COMPILE=arm-linux-gnueabihf-</code>
+
增加FPD-Link3驱动及其描述。
  
不同树莓派的编译选项不同,区别如下:
+
* 20230304
  
* Raspberry Pi 1, Pi Zero, Pi Zero W, and Compute Module default build configuration
+
增加Bullseye系统中CM4的相关说明。
  
<code>KERNEL=kernel</code>
+
* 20220706
  
<code>make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcmrpi_defconfig</code>
+
优化gstreamer的pipeline,建议使用v4l2convert替代videoconvert,并且尽量使用capture-io-mode output-io-mode io-mode选项。
  
* Raspberry Pi 2, Pi 3, Pi 3+, and Compute Module 3 default build configuration
+
*20220505
  
<code>KERNEL=kernel7</code>
+
完善关于CM4的说明。
  
<code>make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2709_defconfig</code>
+
*20220411
  
* Raspberry Pi 4 default build configuration
+
源码编译方法独立到另外一篇文章。
  
<code>KERNEL=kernel7l</code>
+
* 20220302
  
<code>make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2711_defconfig</code>
+
增加v4l2-ctl和yavta例程。
  
===== 增加编译选项 =====
+
* 20220301
<code>make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig</code>
 
  
增加对应camera模块驱动的编译选项,linux5.4版本路径为driver-->multimedia-->i2c;
+
开始支持 64-bit piOS。
  
5.10版本kernel的路径是Device Drivers --> Multimedia Support --> Media ancillary drivers --> Camera sensor devices。
+
* 20220225
  
===== 编译输出 =====
+
增加gstreamer的应用例程。
<code>make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage modules dtbs -j4</code>
 
<br />
 
=== 参考资料 ===
 
  
* 树莓派的piOS编译方案:
+
* 20211125
  
https://www.raspberrypi.org/documentation/linux/kernel/building.md
+
增加VLC播放图像一节。

Latest revision as of 09:28, 20 March 2024

English

如何在树莓派平台使用VEYE和CS系列摄像头模组(V4L2模式)

1 概述

针对VEYE系列和CS系列摄像头模组在树莓派平台的使用方式,我们提供了类似raspicam模式的应用层开源软件——veye_raspicam软件

该系列软件,无需驱动支持,对piOS不同版本的兼容性好。

但是,我们认为V4L2驱动模式,也有比较广泛的应用场合。两种模式不能同时使用,具体体现在驱动是否安装上。后文会有详细描述。

本文描述怎样在树莓派平台,通过V4L2方式调用VEYE系列和CS系列的摄像头模组。

1.1 支持的模组:

模组型号支持表
系列 型号 状态
VEYE系列 VEYE-MIPI-IMX327S 完成
VEYE系列 VEYE-MIPI-IMX385 完成
VEYE系列 VEYE-MIPI-IMX462 完成
CS系列 CS-MIPI-IMX307 完成
CS系列 CS-MIPI-SC132 完成

VEYE系列和CS系列MIPI摄像头模组是内置ISP的,输出为MIPI-CSI2接口。

此外,现在也支持FPD-Link3模式的连接。

2 硬件准备及安装

VEYE系列的硬件准备及安装

CS系列的硬件准备及安装

3 树莓派系统配置

树莓派系统配置

4 驱动安装

4.1 下载驱动包

wget https://github.com/veyeimaging/raspberrypi_v4l2/releases/latest/download/raspberrypi_v4l2.tgz

4.2 安装驱动

tar -xzvf raspberrypi_v4l2.tgz

cd raspberrypi_v4l2/release/

chmod +x *

  • 对于树莓派5

sudo ./install_driver_rpi5.sh [camera module]

会在/boot/config.txt中安装CAM1和CAM0两个dtvoerlay。

  • 其他树莓派

sudo ./install_driver.sh camera_module_name

然后重启树莓派。

camera_module_name:可以是csimx307,cssc132,veyecam2m等。

备注:如果提示无法找到对应版本的驱动,说明我们没有提供对应您的piOS版本编译后的驱动程序。请尝试从源码编译。

备注:veyecam2m是新版本驱动,可以替代veye327,支持VEYE系列200w分辨率的所有模组。请使用veyecam2m,而不是veye327,以后我们会废弃掉veye327.ko。

然后重启树莓派。

Note: For VEYE-MIPI-IMX327S or VEYE-MIPI-IMX462, please make sure hdver must >= 0x5.

http://wiki.veye.cc/index.php/VEYE-MIPI-IMX327S_version_log

4.3 卸载驱动

如需更换为raspicam模式,或者想要更换成其他摄像头模组型号的驱动,必须要先卸载现在的驱动。

sudo ./uninstall_driver.sh camera_module_name

camera_module_name:可以是veyecam2m,csimx307,cssc132等。

4.4 FPD-Link3模式的相机

对于FPD-Link3传输模式的相机,需要首先保证ds90ub954驱动的加载并完成初始化,然后才能加载摄像头驱动并进行探测。下面以FPD-Link3模式的VEYE-MIPI-IMX327S为例:

正常安装驱动:

sudo ./install_driver.sh ds90ub954

sudo ./install_driver.sh veyecam2m

5 摄像头状态验证

以veye327为例:

dmesg | grep veye

可以看到:

veye327 camera probed
并且/dev/video0 节点存在,证明摄像头状态正常。

如果采用veyecam2m驱动,会提示sensor型号及版型,如:

camera id is veyecam2m

sensor is IMX327

board type is ONE board

veyecam2m camera probed

5.1 列出支持的模式

v4l2-ctl --list-formats-ext

6 树莓派5的配置

在树莓派5上,驱动程序现在使用media control API,我们必须先正确设置media graph。这包括正确设置media pad的格式并正确链接它们。

我们提供了一系列脚本实现这个功能,保存在rpi5_scripts目录。

  • ./find_entity.sh

$ ./find_entity.sh

Found veyecam2m @ i2c-6 entity on /dev/media0

Plese get frame from /dev/video0 and use /dev/v4l-subdev2 for camera setting.

树莓派5重启之后,相机的media node,video node可能发生变化,因此,建议执行后续操作前执行./find_entity.sh以明确设备节点。

上述提示信息中,可以看到系统插入了两个相机,以及每个相机的对应的设备节点。

i2c-4对应板子上的CAM1插口,i2c-6对应板子上的CAM0插口。

  • media_setting_rpi5.sh

./media_setting_rpi5.sh

Usage: ./media_setting_rpi5.sh veyecam2m/csimx307/cssc132/mvcam -fmt [UYVY/RAW8/RAW10/RAW12] -w [width] -h [height]

This shell script is designed to detect the connection of a camera on Raspberry Pi 5.

    It utilizes media-ctl and v4l2-ctl commands to configure the linking relationships and data formats of the media pad.

    Once completed, you can directly use /dev/video0 or /dev/video8 to obtain image data

注意:每次启动后,都必须执行此脚本以完成参数的配置,然后再进行后续操作。

举例:

./media_setting_rpi5.sh veyecam2m

7 实时预览

在树莓派5上,vlc进行播放现在有问题,不能使用。

7.1 使用VLC预览图像

1. Open VLC with command line vlc , or click the icon to launch.

2. Hit the (Play) button to call the open media window.

3. In Capture Device >> Device Selection >> Video device name, select the camera video node.

4. Hit Advanced Options... button

5. In Advanced Options window >> Video input chroma format, type UYUV.

6. Type in the width and height, for example, 1920 and 1080.

7. Hit OK to save the settings and see the video feed.

Play camera using VLC on RPI
Play camera using VLC on RPI


7.2 使用qv4l2播放图像

首先安装qv4l2

sudo apt install qv4l2

1. Open VLC with command line vlc to launch.

2. Hit the (Play) button to call the open media window.

8 Gstreamer应用范例

export DISPLAY=:0

8.1 安装gstreamer

sudo apt-get install gstreamer1.0-tools

sudo apt-get install libx264-dev libjpeg-dev

sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgstreamer-plugins-bad1.0-dev gstreamer1.0-plugins-ugly gstreamer1.0-tools gstreamer1.0-gl gstreamer1.0-gtk3

8.2 运行 gstreamer videotest 命令

gst-launch-1.0 videotestsrc ! videoconvert ! autovideosink

8.3 抓拍一张图片(VEYE-MIPI-327,CS-MIPI-IMX307 @1080p mode)

gst-launch-1.0 v4l2src num-buffers=1 device=/dev/video0 ! 'video/x-raw, format=(string)UYVY, width=1920,height=1080' ! jpegenc ! filesink location=test_image.jpg

8.4 帧率测试(VEYE-MIPI-327,CS-MIPI-IMX307 @1080p mode)

gst-launch-1.0 v4l2src device=/dev/video0 ! "video/x-raw,format=(string)UYVY, width=(int)1920, height=(int)1080,framerate=(fraction)30/1" ! videoconvert ! fpsdisplaysink video-sink=fakesink -v

8.5 视频预览(VEYE-MIPI-327,CS-MIPI-IMX307 @1080p mode)

gst-launch-1.0 v4l2src io-mode=dmabuf device=/dev/video0 ! "video/x-raw,format=(string)UYVY, width=(int)1920, height=(int)1080,framerate=(fraction)30/1" ! v4l2convert capture-io-mode=dmabuf output-io-mode=dmabuf ! autovideosink sync=false -v

或者:

gst-launch-1.0 v4l2src device=/dev/video0 ! "video/x-raw,format=(string)UYVY, width=(int)1920, height=(int)1080,framerate=(fraction)30/1" ! videoconvert ! autovideosink  sync=false -v

8.6 视频预览并加时间戳显示(VEYE-MIPI-327,CS-MIPI-IMX307 @1080p mode)

gst-launch-1.0 v4l2src device=/dev/video0 ! "video/x-raw,format=(string)UYVY, width=(int)1920, height=(int)1080,framerate=(fraction)30/1"  ! videoconvert ! videoscale ! clockoverlay time-format="%D %H:%M:%S" ! video/x-raw, width=640, height=360 ! autovideosink sync=false -v

8.7 视频预览(CS-MIPI-IMX307,CS-MIPI-SC132 @720p mode)

gst-launch-1.0 v4l2src device=/dev/video0 ! "video/x-raw,format=(string)UYVY, width=(int)1280, height=(int)720,framerate=(fraction)60/1" ! videoconvert ! autovideosink sync=false -v

8.8 视频预览(CS-MIPI-IMX307,CS-MIPI-SC132 @vga mode)

gst-launch-1.0 v4l2src device=/dev/video0 ! "video/x-raw,format=(string)UYVY, width=(int)640, height=(int)480" ! videoconvert ! autovideosink sync=false -v

8.9 视频预览(CS-MIPI-SC132 @1280*1080@45 mode)

gst-launch-1.0 v4l2src device=/dev/video0 ! "video/x-raw,format=(string)UYVY, width=(int)1280, height=(int)1080,framerate=(fraction)45/1" ! videoconvert ! autovideosink sync=false -v

8.10 h.264编码并保存为mkv文件(VEYE-MIPI-X,CS-MIPI-IMX307 @1080p mode)

gst-launch-1.0 -e v4l2src io-mode=dmabuf device=/dev/video0 num-buffers=300 ! "video/x-raw,format=(string)UYVY, width=(int)1920, height=(int)1080,framerate=(fraction)30/1" ! v4l2h264enc capture-io-mode=dmabuf output-io-mode=dmabuf extra-controls="controls, h264_profile=4, video_bitrate=6200000" ! 'video/x-h264, profile=high, level=(string)4' ! h264parse ! matroskamux ! filesink location=output.mkv

8.11 h.264编码并保存为mp4文件(VEYE-MIPI-X,CS-MIPI-IMX307 @1080p mode)

gst-launch-1.0 -e v4l2src io-mode=dmabuf device=/dev/video0  num-buffers=300 ! "video/x-raw,format=(string)UYVY, width=(int)1920, height=(int)1080,framerate=(fraction)30/1" ! v4l2h264enc capture-io-mode=dmabuf output-io-mode=dmabuf extra-controls="controls, h264_profile=4, video_bitrate=6200000" ! 'video/x-h264, profile=high, level=(string)4' ! h264parse ! mp4mux ! filesink location=video.mp4

8.12 网络传输 TCP streaming

树莓派(Server)一侧执行

gst-launch-1.0 -v v4l2src device=/dev/video0 num-buffers=-1 ! "video/x-raw,format=(string)UYVY, width=(int)1920, height=(int)1080,framerate=(fraction)30/1" ! v4l2h264enc extra-controls="controls, h264_profile=4, video_bitrate=4000000" ! 'video/x-h264, profile=high, level=(string)4' ! h264parse ! rtph264pay config-interval=1 pt=96 ! gdppay ! tcpserversink  host=x.x.x.x port=5000

码流带宽为4Mbps,持续传输,监听端口为5000。

Client一侧执行

gst-launch-1.0 -v tcpclientsrc host=x.x.x.x port=5000 ! gdpdepay ! rtph264depay ! avdec_h264 ! autovideosink sync=false

其中x.x.x.x为树莓派(Server)的IP地址。

Client一侧,如为windows系统,建议使用powershell。gstreamer的windows版本下载地址。只安装runtime files即可。

9 使用opencv获取图片

pip uninstall opencv-python

sudo apt install python3-opencv

为了将摄像头数据从v4l2设备导入到opencv,我们提供了几个sample

10 V4l2-ctl 应用范例

10.1 安装v4l2-utils

sudo apt-get install v4l-utils

10.2 v4l2-ctl例程

  • 抓拍一张UYVY原始数据图片 (1080p mode)

v4l2-ctl --set-fmt-video=width=1920,height=1080,pixelformat=UYVY --stream-mmap --stream-count=1 --stream-to=uyvy-1920x1080.yuv

  • 抓拍10张UYVY原始数据图片 (1080p mode)

v4l2-ctl --set-fmt-video=width=1920,height=1080,pixelformat=UYVY --stream-mmap --stream-count=10 --stream-to=uyvy-1920x1080_stream.yuv

11 yavta 应用范例

11.1 下载并编译yavta

git clone git://git.ideasonboard.org/yavta.git

cd yavta;make

11.2 yavta 例程

./yavta -c8 -Fuyvy_1920X1080.yuv --skip 0 -f UYVY -s 1920x1080 /dev/video0

PS, UYVY文件可以用这个播放器: YUV Displayer Deluxe.

12 参数控制软件包使用

由于我们的摄像头参数自由度比较高,并没有采用V4L2参数进行控制,而是使用脚本进行参数配置。

https://github.com/veyeimaging/raspberrypi_v4l2/tree/main/i2c_cmd

VEYE系列参数控制软件包使用说明
CS系列参数控制软件包使用说明

13 CM4的补充说明

cm4支持两路摄像头同时使用,采用上面的步骤安装的dtbo将只能使用CAM1。如需使用两路摄像头,请按如下步骤:

13.1 硬件连接

参考树莓派这个说明,将J6进行短接。

13.2 更新dt-blob.bin

sudo wget https://datasheets.raspberrypi.com/cmio/dt-blob-dualcam.bin -O /boot/dt-blob.bin

13.3 对于Buster系统(kernel5.10)

更新双摄像头的dtbo,以csimx307为例:

sudo cp raspberrypi_v4l2/release/driver_bin/$(uname -r)/csimx307-dual-cm4.dtbo /boot/overlays/csimx307.dtbo

sudo reboot

13.4 对于Bullseye系统(kernel5.15+)

手动编辑/boot/config.txt文件,增加一行dtoverlay=[camera],cam0,如下所示:

[all]

dtparam=i2c_vc=on

dtoverlay=veyecam2m

dtoverlay=veyecam2m,cam0

13.5 设备文件说明

CM4模块采用两路I2C分别与两个摄像头通信。

description i2c bus num video node
CAM0 0 video0
CAM1 10 video2(Buster),video1(Bullseye)

注:只连一个摄像头,无论CAM0还是CAM1,都是video0。

13.6 gstreamer应用范例

export DISPLAY=:0

  • CAM0视频预览(VEYE-MIPI-327,CS-MIPI-IMX307 @1080p mode)

gst-launch-1.0 v4l2src io-mode=dmabuf device=/dev/video0 ! "video/x-raw,format=(string)UYVY, width=(int)1920, height=(int)1080,framerate=(fraction)30/1" ! v4l2convert capture-io-mode=dmabuf output-io-mode=dmabuf ! autovideosink sync=false -v

  • CAM1视频预览(VEYE-MIPI-327,CS-MIPI-IMX307 @1080p mode)

gst-launch-1.0 v4l2src io-mode=dmabuf device=/dev/video1 ! "video/x-raw,format=(string)UYVY, width=(int)1920, height=(int)1080,framerate=(fraction)30/1" ! v4l2convert capture-io-mode=dmabuf output-io-mode=dmabuf ! autovideosink sync=false -v

14 从源代码编译驱动方法

请参考:树莓派平台上摄像头驱动源码编译指南

15 本文修改记录

  • 2024-01-01

增加对树莓派5的相关说明。

  • 20230607

增加FPD-Link3驱动及其描述。

  • 20230304

增加Bullseye系统中CM4的相关说明。

  • 20220706

优化gstreamer的pipeline,建议使用v4l2convert替代videoconvert,并且尽量使用capture-io-mode output-io-mode io-mode选项。

  • 20220505

完善关于CM4的说明。

  • 20220411

源码编译方法独立到另外一篇文章。

  • 20220302

增加v4l2-ctl和yavta例程。

  • 20220301

开始支持 64-bit piOS。

  • 20220225

增加gstreamer的应用例程。

  • 20211125

增加VLC播放图像一节。