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);
+64
View File
@@ -91,6 +91,31 @@ pub fn validate_crop(
Ok(())
}
/// Clamp a crop rectangle so it fully fits inside the frame dimensions.
///
/// `x`/`y` are clamped into `[0, frame_size)`, and `w`/`h` are reduced so the
/// rectangle never extends past the frame boundary. The result always has
/// positive dimensions. Out-of-bounds bboxes that previously made ffmpeg
/// silently shift the crop (root cause of the "key frame OK, key face broken"
/// bug) now produce a correctly-aligned crop.
pub fn clamp_crop(
x: i32,
y: i32,
w: i32,
h: i32,
video_width: i32,
video_height: i32,
) -> (i32, i32, i32, i32) {
if video_width <= 0 || video_height <= 0 {
return (x.max(0), y.max(0), w.max(1), h.max(1));
}
let cx = x.clamp(0, video_width - 1);
let cy = y.clamp(0, video_height - 1);
let cw = w.clamp(1, (video_width - cx).max(1));
let ch = h.clamp(1, (video_height - cy).max(1));
(cx, cy, cw, ch)
}
#[cfg(test)]
mod tests {
use super::*;
@@ -167,6 +192,45 @@ mod tests {
assert!(validate_crop(100, -1, 200, 200, 1920, 1080).is_err());
}
#[test]
fn test_clamp_crop_in_bounds_unchanged() {
assert_eq!(
clamp_crop(100, 100, 200, 200, 1920, 1080),
(100, 100, 200, 200)
);
assert_eq!(clamp_crop(0, 0, 1920, 1080, 1920, 1080), (0, 0, 1920, 1080));
}
#[test]
fn test_clamp_crop_exceeds_right_bottom() {
// Regression for trace 355b: bbox 11px past the frame edge used to
// make ffmpeg silently shift the crop content.
assert_eq!(clamp_crop(65, 301, 70, 70, 1280, 360), (65, 301, 70, 59));
assert_eq!(
clamp_crop(1800, 900, 200, 200, 1920, 1080),
(1800, 900, 120, 180)
);
}
#[test]
fn test_clamp_crop_negative_origin() {
assert_eq!(clamp_crop(-10, -20, 200, 100, 1920, 1080), (0, 0, 200, 100));
}
#[test]
fn test_clamp_crop_fully_outside() {
assert_eq!(
clamp_crop(3000, 3000, 50, 50, 1920, 1080),
(1919, 1079, 1, 1)
);
}
#[test]
fn test_clamp_crop_zero_frame_dimensions() {
// Unknown frame size: fall back to legacy max(0)/max(1) behavior.
assert_eq!(clamp_crop(-10, -20, 0, 0, 0, 0), (0, 0, 1, 1));
}
#[test]
fn test_is_valid_jpeg() {
let valid_jpeg = vec![