
Buenas tardes. En este artículo, le diré, lejos de la primera vez, cómo detectar simultáneamente el movimiento y guardar / transmitir video en formato H264 en Raspberry Pi 3 y plataformas más débiles. Compartiré con los mismos recién llegados en el mundo de Raspberry Pi, como yo, lo que descubrí en unos días, mientras descubría cómo resolver el problema. Hablaré sobre trabajar con la cámara Raspberry Pi en un lenguaje humano simple.
" 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.
El artículo puede parecer un lugar común para los profesionales, pero tuve que pasar un tiempo considerable para tratar estas cosas. Espero que para los principiantes que comiencen a trabajar con video en la plataforma Raspberry Pi esto sea útil.