机械臂摄像头

This commit is contained in:
ywb
2026-05-21 19:44:25 +08:00
parent 56729c23e8
commit 457f32919c
3 changed files with 44 additions and 18 deletions
+35
View File
@@ -45,6 +45,7 @@ def _ensure_ffmpeg():
"ffmpeg",
"-f", "v4l2",
"-input_format", "mjpeg",
"-re",
"-i", f"/dev/video{ARM_CAMERA_INDEX}",
"-vf", "rotate=PI",
"-q:v", "8",
@@ -103,6 +104,40 @@ def arm_camera_restart():
_ensure_ffmpeg()
return jsonify({"ok": True})
@arm_video_app.route("/api/camera/snapshot")
def arm_camera_snapshot():
"""机械臂摄像头单帧 JPEG — pkill -9 强杀旧 ffmpeg,再临时抓一帧"""
import subprocess
global _ffmpeg_proc
# 用 pkill -9 强杀所有 ffmpeg 进程,释放 /dev/video0
subprocess.run(["pkill", "-9", "-f", "ffmpeg"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=3)
time.sleep(0.3)
_ffmpeg_proc = None
proc = subprocess.run(
[
"ffmpeg",
"-f", "v4l2",
"-input_format", "mjpeg",
"-i", f"/dev/video{ARM_CAMERA_INDEX}",
"-vf", "rotate=PI",
"-vframes", "1",
"-q:v", "8",
"-f", "mjpeg",
"pipe:1"
],
stdout=subprocess.PIPE,
timeout=5,
stderr=subprocess.DEVNULL
)
if proc.returncode == 0 and proc.stdout:
r = Response(proc.stdout, mimetype="image/jpeg")
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate, max-age=0"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
return r
logger.warning(f"ffmpeg snapshot failed: rc={proc.returncode}")
return "", 500
# ========== RoboFlow 630 Socket API 客户端 ==========
class RoboFlowClient: