Raspberry pi 5 上加装摄像头模组的一点经验

  • fennng 

摄像头插上就能用,不需要额外设置

网上很多教程是针对 Raspberry pi 4 和以前的,不太适用 5.

打开摄像头的串流后,显示器上会显示摄像头画面,这个有点神奇。我的树莓派是直接启动到命令行的。

在树霉派桌面环境输入 libcamera-hello 可以测试摄像头,摄像头画面会弹出几秒,然后退出

https://www.waveshare.com/wiki/RPi_Camera_(I)#libcamera-vid

libcamera-hello --list-cameras


Available cameras
-----------------
0 : ov5647 [2592x1944 10-bit GBRG] (/base/axi/pcie@120000/rp1/i2c@80000/ov5647@36)
    Modes: 'SGBRG10_CSI2P' : 640x480 [30.00 fps - (65535, 65535)/65535x65535 crop]
                             1296x972 [30.00 fps - (65535, 65535)/65535x65535 crop]
                             1920x1080 [30.00 fps - (65535, 65535)/65535x65535 crop]
                             2592x1944 [30.00 fps - (65535, 65535)/65535x65535 crop]

从客户端连上串流后再退出, 串流服务也退出了, 所以要写一个循环让它一直跑

#!/bin/bash

while true; do
  libcamera-vid -t 0 --width 1920 --height 1080 --inline --listen -o tcp://0.0.0.0:8000
  sleep 1 # optional delay before restart
done

然后可以用 mpv 播放

mpv tcp://192.168.1.57:8000

我发现可以串流 udp, 但我发现 mpv 播放不了, vlc 也播放不了。

用 mpv 播放延时大概三秒。 我发现用 Websocket 在网页上播放延时只有大概1秒。看看这里 从这里抄来的 https://www.mkmark.net/blog/solution/raspberry-pi-libcamera-low-latency-http-streaming/ 

# 用 tmux 窗口打开
# 这样的话断开后就停了,要一下能开的话用上面的循环脚本
libcamera-vid -t 0 --width 1920 --height 1080 --inline --listen -o tcp://0.0.0.0:8000
# 用另一个 tmux 窗口打开
pip install websockify
websockify 0.0.0.0:8001 0.0.0.0:8000

有两条错误和一个警告,但不影响使用。

[0:20:12.003237414] [2143] ERROR IPAModule ipa_module.cpp:172 Symbol ipaModuleInfo not found
[0:20:12.003274894] [2143] ERROR IPAModule ipa_module.cpp:292 v4l2-compat.so: IPA module has no valid info
[0:20:12.003300930] [2143]  INFO Camera camera_manager.cpp:313 libcamera v0.3.0+65-6ddd79b5
[0:20:12.011245417] [2148]  INFO RPI pisp.cpp:695 libpisp version v1.0.6 b567f0455680 17-06-2024 (10:20:00)
[0:20:12.027920944] [2148]  INFO RPI pisp.cpp:1154 Registered camera /base/axi/pcie@120000/rp1/i2c@80000/ov5647@36 to CFE device /dev/media0 and ISP device /dev/media1 using PiSP variant BCM2712_C0
Made DRM preview window
[0:20:12.183023637] [2143]  WARN V4L2 v4l2_pixelformat.cpp:344 Unsupported V4L2 pixel format RPBP
Mode selection for 1920:1080:12:P
    SGBRG10_CSI2P,640x480/0 - Score: 6093.33
    SGBRG10_CSI2P,1296x972/0 - Score: 3797.33
    SGBRG10_CSI2P,1920x1080/0 - Score: 1000
    SGBRG10_CSI2P,2592x1944/0 - Score: 2717.33
Stream configuration adjusted
[0:20:12.183238743] [2143]  INFO Camera camera.cpp:1183 configuring streams: (0) 1920x1080-YUV420 (1) 1920x1080-GBRG_PISP_COMP1
[0:20:12.183345574] [2148]  INFO RPI pisp.cpp:1450 Sensor: /base/axi/pcie@120000/rp1/i2c@80000/ov5647@36 - Selected sensor format: 1920x1080-SGBRG10_1X10 - Selected CFE format: 1920x1080-PC1g
[libx264 @ 0x555634f382e0] using cpu capabilities: ARMv8 NEON
[libx264 @ 0x555634f382e0] profile High, level 4.0, 4:2:0, 8-bit
Output #0, h264, to 'tcp://0.0.0.0:8000?listen=1':
  Stream #0:0: Video: h264, yuv420p(tv, bt709), 1920x1080, q=2-31, 30 fps, 30 tbr, 1000k tbn
<img src="https://fennng.visualstudio.com/1c50733b-7e60-4ef1-b705-ff7ee350693e/_apis/git/repositories/50c5e7af-75a8-4397-b510-cd219a2d0f8c/Items?path=/.attachments/image-946f17d7-cb4b-40a9-9fc2-689131d3a2cb.png&download=false&resolveLfs=true&%24format=octetStream&api-version=5.0-preview.1&sanitize=true&versionDescriptor.version=wikiMaster">

然后写一个简单的网页

<head>
<meta name="color-scheme" content="dark">
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/jmuxer@2.0.4/dist/jmuxer.min.js"></script>
<style>
    body{
        margin: 0;
    }
</style>
</head>
<body>
<div id="container" style="width: 100%; margin: 0 auto;">
    <div class="vsc-controller"></div>
    <video width="100%" autoplay muted id="player"></video>
</div>
<script>
    function parse(data) {
        var input = new Uint8Array(data),
        video = input;

        return {
            video: video,
        };
    }

    window.onload = function () {
        var socketURL = document.location.href.replace('http', 'ws')+'ws/';
        var socketURL = "ws://192.168.1.57:8001/ws/"
        var jmuxer = new JMuxer({
            node: 'player',
            mode: 'video',
            flushingTime: 0,
            fps: 30,
            debug: false
        });

        var ws = new WebSocket(socketURL);
        ws.binaryType = 'arraybuffer';
        ws.addEventListener('message', function (event) {
            var data = parse(event.data);
            jmuxer.feed(data);
        });
    }
</script>
</body>

然后运行一个简单的 python web 服务器

python3 -m http.server --bind 0.0.0.0 --directory $(dirname $(realpath ./index.html)) 8080

然后在浏览器打开

http://192.168.1.57:8080 

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注