feat: add regenerate trace thumbnail endpoint with bbox clamping

- validator::clamp_crop: clamp crop rect to frame dims (fixes OOB shift)
- tkg.rs: use clamp_crop in key_face crop paths (root-cause fix)
- trace_agent_api: POST /api/v1/file/:file_uuid/trace/:trace_id/regenerate-thumbnail
- docs: 26_trace_thumbnail_regenerate.md for Studio team
This commit is contained in:
Accusys
2026-08-12 03:04:19 +08:00
parent aa28bf93fb
commit 59ca6cdf83
5 changed files with 553 additions and 25 deletions
+49 -25
View File
@@ -1245,6 +1245,19 @@ async fn build_face_track_nodes_from_qdrant(
.ok()
.flatten();
// Get video frame dimensions for bbox clamping when cropping key_face
let video_dim: Option<(Option<i32>, Option<i32>)> = sqlx::query_as(&format!(
"SELECT width, height FROM {} WHERE file_uuid = $1",
crate::core::db::schema::table_name("videos")
))
.bind(file_uuid)
.fetch_optional(pool)
.await
.ok()
.flatten();
let video_width = video_dim.and_then(|d| d.0).unwrap_or(0);
let video_height = video_dim.and_then(|d| d.1).unwrap_or(0);
// Get actual fps for timestamp calculation
let actual_fps: f64 = sqlx::query_scalar(&format!(
"SELECT fps FROM {} WHERE file_uuid = $1",
@@ -1272,12 +1285,7 @@ async fn build_face_track_nodes_from_qdrant(
let key_frame_jpeg: Option<Vec<u8>> = video_path.as_ref().and_then(|vp| {
let select = format!("select=eq(n\\,{})", agg.best_frame);
let output = std::process::Command::new("ffmpeg")
.args([
"-y",
"-v",
"quiet",
"-i",
])
.args(["-y", "-v", "quiet", "-i"])
.arg(vp)
.args([
"-vf",
@@ -1325,13 +1333,15 @@ async fn build_face_track_nodes_from_qdrant(
return None;
}
let crop_filter = format!(
"crop={}:{}:{}:{}",
(bw as i64).max(1),
(bh as i64).max(1),
(bx as i64).max(0),
(by as i64).max(0),
let (cx, cy, cw, ch) = crate::core::thumbnail::validator::clamp_crop(
bx as i32,
by as i32,
bw as i32,
bh as i32,
video_width,
video_height,
);
let crop_filter = format!("crop={}:{}:{}:{}", cw, ch, cx, cy);
let mut cmd = std::process::Command::new("ffmpeg");
cmd.args(["-y", "-v", "quiet", "-f", "image2pipe", "-i", "pipe:0"])
.args(["-frames:v", "1", "-q:v", "2", "-vf"])
@@ -1410,13 +1420,7 @@ async fn build_face_track_nodes_from_qdrant(
// Retry key_frame extraction
if !kf_path.exists() {
if let Ok(output) = std::process::Command::new("ffmpeg")
.args([
"-y",
"-v",
"quiet",
"-i",
vp,
])
.args(["-y", "-v", "quiet", "-i", vp])
.args([
"-vf",
&select,
@@ -1464,13 +1468,15 @@ async fn build_face_track_nodes_from_qdrant(
bh
);
} else {
let crop_filter = format!(
"crop={}:{}:{}:{}",
(bw as i64).max(1),
(bh as i64).max(1),
(bx as i64).max(0),
(by as i64).max(0),
let (cx, cy, cw, ch) = crate::core::thumbnail::validator::clamp_crop(
bx as i32,
by as i32,
bw as i32,
bh as i32,
video_width,
video_height,
);
let crop_filter = format!("crop={}:{}:{}:{}", cw, ch, cx, cy);
if let Ok(kf_bytes) = std::fs::read(&kf_path) {
let mut cmd = std::process::Command::new("ffmpeg");
cmd.args(["-y", "-v", "quiet", "-f", "image2pipe", "-i", "pipe:0"])
@@ -3594,6 +3600,8 @@ mod tests {
y: 100.0,
w: 50.0,
h: 50.0,
confidence: 0.9,
point_id: None,
},
FacePoint {
trace_id: 1,
@@ -3602,6 +3610,8 @@ mod tests {
y: 200.0,
w: 50.0,
h: 50.0,
confidence: 0.9,
point_id: None,
},
FacePoint {
trace_id: 2,
@@ -3610,6 +3620,8 @@ mod tests {
y: 150.0,
w: 50.0,
h: 50.0,
confidence: 0.9,
point_id: None,
},
FacePoint {
trace_id: 1,
@@ -3618,6 +3630,8 @@ mod tests {
y: 120.0,
w: 50.0,
h: 50.0,
confidence: 0.9,
point_id: None,
},
];
@@ -3646,6 +3660,8 @@ mod tests {
y: 100.0,
w: 100.0,
h: 100.0,
confidence: 0.9,
point_id: None,
},
FacePoint {
trace_id: 2,
@@ -3654,6 +3670,8 @@ mod tests {
y: 300.0,
w: 100.0,
h: 100.0,
confidence: 0.9,
point_id: None,
},
];
let index = build_frame_index(&points);
@@ -3673,6 +3691,8 @@ mod tests {
y: 100.0,
w: 100.0,
h: 100.0,
confidence: 0.9,
point_id: None,
},
FacePoint {
trace_id: 2,
@@ -3681,6 +3701,8 @@ mod tests {
y: 500.0,
w: 100.0,
h: 100.0,
confidence: 0.9,
point_id: None,
},
];
let index = build_frame_index(&points);
@@ -3699,6 +3721,8 @@ mod tests {
y: 100.0,
w: 100.0,
h: 100.0,
confidence: 0.9,
point_id: None,
}];
let index = build_frame_index(&points);