V4L2 mode for Raspberry Pi/zh

From wiki_veye
Jump to navigation Jump to search

English

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

1 概述

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

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

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

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

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 *

sudo ./install_driver.sh [camera module]

camera module:可以是veye327,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]

camera module:可以是veye327,csimx307,cssc132等。

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 使用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 Gstreamer应用范例

export DISPLAY=:0

7.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

7.2 运行 gstreamer videotest 命令

gst-launch-1.0 videotestsrc ! videoconvert ! autovideosink

7.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

7.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

7.5 视频预览(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 ! autovideosink sync=false -v

7.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

7.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

7.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

7.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

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

gst-launch-1.0 -e v4l2src device=/dev/video0  num-buffers=300  ! "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=6200000" ! 'video/x-h264, profile=high, level=(string)4' ! h264parse ! matroskamux ! filesink location=output.mkv

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

gst-launch-1.0 -e v4l2src device=/dev/video0  num-buffers=300  ! "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=6200000" ! 'video/x-h264, profile=high, level=(string)4' ! h264parse ! mp4mux ! filesink location=video.mp4

7.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即可。

7.13 使用opencv获取视频

gst-launch-1.0 v4l2src device=/dev/video0  ! "video/x-raw,format=(string)UYVY, width=(int)1920, height=(int)1080,framerate=(fraction)30/1" ! videoscale ! "video/x-raw,width=640,height=480" ! videoconvert ! "video/x-raw, format=(string)BGR" ! appsink

8 V4l2-ctl 应用范例

8.1 安装v4l2-utils

sudo apt-get install v4l-utils

8.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

9 yavta 应用范例

9.1 下载并编译yavta

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

cd yavta;make

9.2 yavta 例程

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

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

10 参数控制软件包使用

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

https://github.com/veyeimaging/raspberrypi/tree/master/i2c_cmd

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

11 CM4的补充说明

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

11.1 更新dt-blob.bin

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

11.2 更新双摄像头的dtbo

以csimx307为例:

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

sudo reboot

11.3 设备文件说明

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

description i2c bus num video node
CAM0 0 video0
CAM1 10 video2

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

11.4 gstreamer应用范例

export DISPLAY=:0

  • CAM0视频预览(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 videosink=autovideosink sync=false text-overlay=false -v

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

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

12 从源代码编译驱动方法

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

13 本文修改记录

  • 20220505

完善关于CM4的说明。

  • 20220411

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

  • 20220302

增加v4l2-ctl和yavta例程。

  • 20220301

开始支持 64-bit piOS。

  • 20220225

增加gstreamer的应用例程。

  • 20211125

增加VLC播放图像一节。