之前的Ender3 v2 3D打印机提到过我用了jetson nano来连接3d打印机,用octoprint来提交和监控打印任务,但是没有仔细说明如何配置摄像头监控。本文记录下摄像头相关的内容。

jetson nano支持csi和usb摄像头,在命令上基本类似。我使用的是usb摄像头。

查看摄像头支持的格式

根据Jetson Nano FAQ,安装v4l-utils并使用如下命令查看摄像头/dev/video0支持的原始图片格式。

v4l2-ctl -d /dev/video0 --list-formats-ext
# 我的结果打印如下
ioctl: VIDIOC_ENUM_FMT
    Index       : 0
    Type        : Video Capture
    Pixel Format: 'MJPG' (compressed)
    Name        : Motion-JPEG
        Size: Discrete 1920x1080
            Interval: Discrete 0.033s (30.000 fps)
            Interval: Discrete 0.040s (25.000 fps)
            Interval: Discrete 0.050s (20.000 fps)
            Interval: Discrete 0.067s (15.000 fps)
            Interval: Discrete 0.100s (10.000 fps)
            Interval: Discrete 0.200s (5.000 fps)
        Size: Discrete 1280x720
            Interval: Discrete 0.033s (30.000 fps)
            Interval: Discrete 0.050s (20.000 fps)
            Interval: Discrete 0.067s (15.000 fps)
            Interval: Discrete 0.100s (10.000 fps)
            Interval: Discrete 0.200s (5.000 fps)
        Size: Discrete 640x480
            Interval: Discrete 0.033s (30.000 fps)
            Interval: Discrete 0.050s (20.000 fps)
            Interval: Discrete 0.067s (15.000 fps)
            Interval: Discrete 0.100s (10.000 fps)
            Interval: Discrete 0.200s (5.000 fps)

    Index       : 1
    Type        : Video Capture
    Pixel Format: 'YUYV'
    Name        : YUYV 4:2:2
        Size: Discrete 1920x1080
            Interval: Discrete 0.200s (5.000 fps)
        Size: Discrete 1280x720
            Interval: Discrete 0.100s (10.000 fps)
        Size: Discrete 640x480
            Interval: Discrete 0.033s (30.000 fps)

从结果可以看到,我用的摄像头支持的格式为MJPGYUYV,支持的分辨率如上所示。

编译运行一个简单的rtsp server

在板子上下载编译命令如下,

wget https://github.com/GStreamer/gst-rtsp-server/raw/1.18/examples/test-launch.c
sudo apt-get install libgstrtspserver-1.0 libgstreamer1.0-dev
gcc test-launch.c -o test-launch $(pkg-config --cflags --libs gstreamer-1.0 gstreamer-rtsp-server-1.0)

测试命令如下,

./test-launch "v4l2src device=/dev/video0 ! video/x-raw,format=YUY2,width=640,height=480,framerate=30/1 ! nvvidconv ! nvv4l2h264enc ! h264parse ! rtph264pay name=pay0 pt=96"

v4l2src device=/dev/video0是jetson提供的api来处理摄像头设备作为信号源。

format=YUY2指定了原始图像的格式。我不清楚为什么尽管在上一步中,获得的摄像头支持的格式为YUYV,但是如果我在这里指定格式为YUYV,无法成功获得视频画面。理论上YUY2只是YUYV的别名。

width=640,height=480,framerate=30/1注意这里一定要和摄像头支持的像素以及fps帧率一致。

命令后面的部分大概意思是将视频原始数据解码为h264格式,最后传递给rtph264pay。

!是作为管道将上一步处理的数据传递给下一个命令。

如果显示stream ready at rtsp://127.0.0.1:8554/test则代表server运行起来了。

在你的本地PC上,使用vlc等支持rtsp串流的播放器打开rtsp://你的jetsonip:8554/test

vlc rtsp://192.168.1.199:8554/test

如果在jetson端,test-launch的命令输出如下代表成功解析了视频。

Opening in BLOCKING MODE 
NvMMLiteOpen : Block : BlockType = 4 
===== NVMEDIA: NVENC =====
NvMMLiteBlockCreate : Block : BlockType = 4 
H264: Profile = 66, Level = 0 
NVMEDIA_ENC: bBlitMode is set to TRUE 

在你本地PC的播放器里,就可以看到实时的画面。

使用ffmpeg转换rtsp为hls

安装nvidia为jetson官方编译的使用nvenc硬件加速的ffmpeg,

echo "deb https://repo.download.nvidia.com/jetson/ffmpeg main main" | sudo tee -a /etc/apt/sources.list
echo "deb-src https://repo.download.nvidia.com/jetson/ffmpeg main main" | sudo tee -a /etc/apt/sources.list
sudo apt update
apt policy ffmpeg
sudo apt install ffmpeg=7:4.2.2-nvidia

注意!jetson nano的系统已经不再继续更新,但是上面repo里的ffmpeg还在维护更新,所以如果直接安装最新的4.2.7版本,则会出现依赖错误。因为jetson nano的ubuntu18.04系统的包都是旧的,无法安装依赖包的最新版本。所以需要指定安装旧的4.2.2版本。

安装完成后,ffmpeg命令的输出如下,

ffmpeg version n4.2.2-15-g6878ea5a44 Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 7 (Ubuntu/Linaro 7.5.0-3ubuntu1~18.04)
  configuration: --prefix=/usr --enable-nvv4l2dec --enable-libv4l2 --enable-shared --extra-libs='-L/usr/lib/aarch64-linux-gnu/tegra -lnvbuf_utils' --extra-cflags='-I /usr/src/jetson_multimedia_api/include/'

看到--enable-nvv4l2dec以及tegra等代表使用的是正确的版本。

执行如下命令将本地(或者远程)的rtsp转换为hls。命令执行后,会在/tmp/hls目录下看到一个test.m3u8以及15个test*.ts文件。ts文件是生成的片段文件,而test.m3u8则是最终我们需要的视频文件。随着视频文件的产生,新ts文件会循环覆盖之前旧的ts文件的内容。

mkdir /tmp/hls
ffmpeg -i "rtsp://localhost:8554/test" -c copy -f hls -hls_time 10.0 -hls_list_size 0 -hls_wrap 15 "/tmp/hls/test.m3u8"

建立http server

video.js是一个前端视频播放器,可以很容易的嵌入到你自己的网页里来播放视频。

复制如下内容到/tmp/hls/index.html,将其中的http://192.168.1.199/test.m3u8替换为你自己的ip地址。

<head>
  <link href="https://vjs.zencdn.net/7.20.3/video-js.css" rel="stylesheet" />

  <!-- If you'd like to support IE8 (for Video.js versions prior to v7) -->
  <!-- <script src="https://vjs.zencdn.net/ie8/1.1.2/videojs-ie8.min.js"></script> -->
</head>

<body>
  <video
    id="my-video"
    class="video-js"
    controls
    preload="auto"
    width="640"
    height="264"
    poster="MY_VIDEO_POSTER.jpg"
    data-setup="{}"
  >
    <source src="http://192.168.1.199/test.m3u8" type="application/x-mpegURL" />
    <p class="vjs-no-js">
      To view this video please enable JavaScript, and consider upgrading to a
      web browser that
      <a href="https://videojs.com/html5-video-support/" target="_blank"
        >supports HTML5 video</a
      >
    </p>
  </video>

  <script src="https://vjs.zencdn.net/7.20.3/video.min.js"></script>
</body>

先使用python自带的http server测试下效果:

cd /tmp/hls/
python3 -m http.server 9000

在你本地PC的浏览器中打开http://你板子的ip:9000/即可访问新建立的http server,点击视频的播放键测试是否串流成功。

如果测试成功,则sudo apt install nginx安装nginx。修改/etc/nginx/sites-available/defaultroot字段为root /tmp/hlssudo service nginx restart重启nginx服务。

在你本地PC的浏览器打开http://你板子的ip即可看到之前的测试页面。推荐用firefox,这个api在chrome下表现不稳定。

Tips

# take a picture
v4l2-ctl --device /dev/video0 --set-fmt-video=width=1280,height=720,pixelformat=MJPEG --stream-mmap --stream-to=frame.jpg --stream-count=1
nvgstcapture-1.0 --camsrc=0 --cap-dev-node=<N> --automate --capture-auto (where N is the /dev/videoN Node

Reference

NVIDIA Jetson 配置笔记

Jetson Nano FAQ

to-install-the-ffmpeg-binary-package

https://blog.csdn.net/weixin_39510813/article/details/123542903

https://forums.developer.nvidia.com/t/stream-csi-camera-to-http-in-mjpeg-format/179204/18

https://forums.developer.nvidia.com/t/live-streaming-on-the-nano-via-rtsp-test-launch-server/83505

https://docs.nvidia.com/video-technologies/video-codec-sdk/ffmpeg-with-nvidia-gpu/index.html

https://forums.developer.nvidia.com/t/hardware-assisted-ffmpeg-with-jetpack-4-5-1/178931

https://forums.developer.nvidia.com/t/hardware-assisted-ffmpeg-with-jetpack-4-5-1/178931/3

https://github.com/valdivj/Nvidia_Deepstream_FFMPEG_RTSP_to_HTTP

https://forums.developer.nvidia.com/t/jetson-nano-included-ffmpeg-package-segmentation-fault-no-matter-the-decoder-no-matter-the-file/171012/12

https://developer.nvidia.com/embedded/learn/tutorials/first-picture-csi-usb-camera

https://forums.developer.nvidia.com/t/some-problems-of-command-nvgstcapture-1-0/78513/4

https://raspberrypi.stackexchange.com/questions/112743/v4l2-ctl-single-frame-capture-produces-image-with-green-ending


文章版权归 FindHao 所有丨本站默认采用CC-BY-NC-SA 4.0协议进行授权|
转载必须包含本声明,并以超链接形式注明作者 FindHao 和本文原始地址:
https://findhao.net/easycoding/2602.html

Comments