检测场景变化并将h264视频剪辑保存到Raspberry Pi中而不进行解码


下午好。在本文中,我将远非一次告诉您如何在Raspberry Pi 3和较弱的平台上同时检测运动并以H264格式保存/广播视频。我将与Raspberry Pi领域的新成员分享我几天后发现的问题,同时我想出了解决问题的方法。我将讨论以一种简单的人类语言使用Raspberry Pi相机。


" CSI- Raspberry Pi v.1.3 2.1 720p @ 25 FPS, 1 h264, , ".


, , .


, , , ( ) , , .

RPi, , OpenCV, , - , ffmpeg, - .


OpenCV, , : " RGB/MJPEG, - H264 MP4...".


Raspberry Pi , - , datasheets- ( ), — , .


, Raspberry Pi . , , , 2.8-3.2 . , , CS-mount , . - 3d-, — , Thingverse .


Raspberry 3 / — . FullHD OpenMAX.


: Raspberry Pi - H264 -.


FFmpeg :


#!/bin/bash

SS=$1
FPS=$2

WIDTH=1600
HEIGHT=1200

while :
do

ffmpeg -f video4linux2 -input_format h264 -video_size ${WIDTH}x${HEIGHT} \
    -framerate $FPS -i /dev/video0 -vcodec copy -map 0 \
    -segment_time $SS -f segment \
    -strftime 1 -reset_timestamps 1 \
    -segment_format mp4 /video/file_%s.mp4
done

$SS FPS $FPS , unix-timestamp. , MP4. CPU . , FTP.


v2.1 1080p — ( FOV). 1600x1200 — FPS. , , ffmpeg raspivid (. ) . .


, , , RAM GPU Raspberry Pi, - — .

H264, , . : Raspberry Pi 3 FullHD OpenCV , 1280x720 @ 30 FPS .


H264? — H264.


Raspberry Pi (motion vectors) H264. . , .


Raspberry Pi , MV :


typedef struct
{
   signed char x_vector;
   signed char y_vector;
   short sad;
} INLINE_MOTION_VECTOR; 

{x,y}_vector — , sad. sad vectors . sad, motion vectors.


FFmpeg motion vectors , Raspivid:


#!/bin/bash

SS=$1
FPS=$2

W=1600
H=1200

while :
do
    raspivid -pts -stm -w $W -h $H -fps $FPS -t 0 \
                    -sn $(date +%s) -sg $(($SS*1000)) \
                    -o /video/video_%d.h264 -x /video/mv_%d.txt
done

, raspivid +1, , timestamp, stat .

FFmpeg CVLC, RTSP . , — motion vectors .


, "", , , . . FPS=25 ( 30) , , 50Hz , .


— , , MV, , . . , , .

, , RPi — H264 , MV.


, *.h264 — H264, MP4, VLC, , , . Raspberry Pi gpac:


sudo apt install -y gpac

h264 mp4 :


time MP4Box -add /video/video_1588645858.h264 /video/video_1588645858.mp4
AVC-H264 import - frame size 1280 x 720 at 25.000 FPS
AVC Import results: 1560 samples - Slices: 26 I 1534 P 0 B - 0 SEI - 26 IDR
Saving to /video/video_1588645858.mp4: 0.500 secs Interleaving

real    0m11.164s
user    0m0.548s
sys 0m0.469s

11 Raspberry Pi 3, .


MV :


import os
import sys
import struct
import numpy as np

resolution = [1280, 720]

framelength = int(((resolution[0] + 16) / 16) * (resolution[1] / 16) * 4)

framedata = []

cnt = 0

while True:
    data = sys.stdin.read(framelength)

    if data == '':
      break

    cnt += 1

    framedata = struct.unpack('>%db' % framelength, data)
    vectors = np.reshape(framedata[:(resolution[1]/16 * (resolution[0]/16 + 1) * 4)], (resolution[1]/16, resolution[0]/16 + 1, 4)).astype(np.float32)
    vectors = np.multiply(vectors[:, :, 0:1], vectors[:, :, 0:1])

    print int(np.sum(vectors))

C, .


time $(cat /video/mv_1588645871.txt| python proc.py > /dev/null)

real    0m26.288s
user    0m25.005s
sys 0m0.583s

, NumPy ( C), , , , 50% , . MV, , .


, :


, , — . — . .


:


cat /video/mv_1588645871.txt | python proc.py | awk '{ if ($1 > 0) print "motion" }' | grep motion > /dev/null
echo $?
0

, . , FTP, .


, " " , , Raspberry Pi .


, , , MV . Medium, MV Python.


对于专业人士而言,这篇文章似乎很平常,但是我不得不花大量的时间来处理这些事情。我希望对于初学者来说,在Raspberry Pi平台上开始使用视频会很有用。


All Articles