SIYI A8 Mini 相机脚本集
GStreamer 硬件加速 · YOLOv8 实时检测 · MKV/MP4 自动录像
GStreamer 流水线 YOLOv8 人员检测 自动录像 (MKV/MP4) Jetson 硬件解码 RTSP 实时流
Jetson 端脚本目录
nvidia@drobotics:~/siyi_cam$ ls
capture_mkv.py detect_user_input.py detect_warn_save.py yolov8n.pt
detect_people.py detect_warn.py yolo_siyi.py
所有脚本位于
~/siyi_cam 目录,依赖 YOLOv8 模型文件 yolov8n.pt。 网络配置(以太网静态 IP)
sudo ifconfig enx00e04c680a7c 192.168.144.2 netmask 255.255.255.0 up
sudo ip link set enx00e04c680a7c up
ip a show enx00e04c680a7c
9: enx00e04c680a7c: <BROADCAST,MULTICAST,UP,LOWER_UP>
inet 192.168.144.2/24 scope global enx00e04c680a7c
相机默认 IP 为 192.168.144.25,测试连通性:
ping 192.168.144.25 GStreamer 命令行(Jetson 硬件加速)
实时预览(带 FPS 显示)
gst-launch-1.0 -e rtspsrc location=rtsp://192.168.144.25:8554/main.264 protocols=tcp latency=0 ! rtph264depay ! h264parse ! nvv4l2decoder ! nvvidconv ! fpsdisplaysink sync=false
调试流水线(fakesink)
gst-launch-1.0 -v rtspsrc location="rtsp://192.168.144.25:8554/main.264" latency=41 ! rtph265depay ! h265parse ! nvv4l2decoder ! nvvidconv ! videoconvert ! fakesink
直接录制 MP4(无 Python 依赖)
gst-launch-1.0 -e rtspsrc location="rtsp://192.168.144.25:8554/main.264" latency=41 ! rtph265depay ! h265parse ! nvv4l2decoder ! nvvidconv ! videoconvert ! x264enc ! mp4mux ! filesink location=capture_direct.mp4
capture_mkv.py – MKV 录像脚本
#!/usr/bin/env python3
import subprocess, signal, sys, time
filename = f"capture_{time.strftime('%Y%m%d_%H%M%S')}.mkv"
cmd = [
"gst-launch-1.0", "-e",
"rtspsrc", "location=rtsp://192.168.144.25:8554/main.264", "latency=41",
"!", "rtph265depay",
"!", "h265parse",
"!", "nvv4l2decoder",
"!", "nvvidconv",
"!", "videoconvert",
"!", "x264enc",
"!", "matroskamux",
"!", "filesink", f"location={filename}"
]
print(f"Recording to {filename}")
process = subprocess.Popen(cmd)
def signal_handler(sig, frame):
print("\nStopping recording...")
process.send_signal(signal.SIGINT)
process.wait(timeout=10)
print(f"Saved as: {filename}")
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
process.wait()
使用 GStreamer 硬件解码 + x264 编码,输出 MKV 格式,按
Ctrl+C 正常停止。 detect_people.py – 实时人员检测(YOLO)
#!/usr/bin/env python3
from ultralytics import YOLO
import cv2
model = YOLO("yolov8n.pt")
results = model.predict(
source="rtsp://192.168.144.25:8554/main.264",
stream=True,
classes=[0], # 只检测人
conf=0.5,
show=True
)
for result in results:
if result.boxes is not None:
print(f"Detected {len(result.boxes)} person(s)")
if cv2.waitKey(1) & 0xFF == ord('q'):
break
直接调用 YOLO 内置 RTSP 拉流,显示检测窗口,按
q 退出。 detect_warn_save.py – 检测 + 录制 MP4
#!/usr/bin/env python3
import cv2
from ultralytics import YOLO
import warnings, os
from datetime import datetime
warnings.filterwarnings("ignore")
os.environ["OPENCV_LOG_LEVEL"] = "ERROR"
RTSP_URL = "rtsp://192.168.144.25:8554/main.264"
model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture(RTSP_URL, cv2.CAP_FFMPEG)
# 等待首帧,获取分辨率
for _ in range(50):
ret, frame = cap.read()
if ret and frame is not None:
break
h, w, _ = frame.shape
fps = cap.get(cv2.CAP_PROP_FPS) or 30
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
output_filename = f"detection_{timestamp}.mp4"
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_filename, fourcc, fps, (w, h))
frame_count = 0
while True:
ret, frame = cap.read()
if not ret:
break
frame_count += 1
if frame_count % 5 == 0:
results = model(frame, classes=[0], conf=0.5, verbose=False)
if results[0].boxes is not None:
for box in results[0].boxes:
x1, y1, x2, y2 = map(int, box.xyxy[0])
cv2.rectangle(frame, (x1, y1), (x2, y2), (0,255,0), 2)
cv2.putText(frame, "Person", (x1, y1-10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)
out.write(frame)
cap.release()
out.release()
print(f"[DONE] Saved: {output_filename}")
每 5 帧执行一次 YOLO 推理,检测人并绘制框,同时录制 MP4 文件。
SIYI 数据流架构
SIYI RTSP (HEVC)
↓
┌───────────────────────────────────────────────┐
│ GStreamer (nvv4l2decoder) │
│ ├── capture_mkv.py → MKV 录制 │
│ └── gst-launch-1.0 → 预览/调试 │
├───────────────────────────────────────────────┤
│ OpenCV (FFmpeg) → YOLOv8 │
│ ├── detect_people.py → 实时显示 │
│ └── detect_warn_save.py → MP4 录制+框 │
└───────────────────────────────────────────────┘
快速参考卡
| 操作 | 命令/文件 |
|---|---|
| 测试相机连通性 | ping 192.168.144.25 |
| 手动配置以太网 IP | sudo ifconfig enx00e04c680a7c 192.168.144.2 netmask 255.255.255.0 up |
| GStreamer 预览 (FPS) | gst-launch-1.0 -e rtspsrc location=rtsp://192.168.144.25:8554/main.264 protocols=tcp latency=0 ! rtph264depay ! h264parse ! nvv4l2decoder ! nvvidconv ! fpsdisplaysink sync=false |
| GStreamer 直接录像 | gst-launch-1.0 -e rtspsrc location="rtsp://192.168.144.25:8554/main.264" latency=41 ! rtph265depay ! h265parse ! nvv4l2decoder ! nvvidconv ! videoconvert ! x264enc ! mp4mux ! filesink location=capture.mp4 |
| MKV 录像脚本 | python3 capture_mkv.py |
| YOLO 实时检测 | python3 detect_people.py |
| 检测 + 录制 MP4 | python3 detect_warn_save.py |
本页面完整涵盖了 SIYI A8 Mini 在 Jetson 平台上的脚本用法、GStreamer 硬件加速命令以及 YOLO 实时检测方案。