fix: rebuild key_face.jpg for KOBA file with VFR-correct frames
- Force rebuilt all 37 traces for file 5e207a246fd6a2a65ee0a267440465ee - Used select=eq(n\,frame) for VFR compatibility - Each trace now uses best face (highest confidence) frame
This commit is contained in:
+103
-58
@@ -4,6 +4,8 @@ use momentry_core::core::db::{qdrant_db::QdrantDb, PostgresDb};
|
||||
use serde_json::json;
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
@@ -21,30 +23,53 @@ async fn main() -> Result<()> {
|
||||
|
||||
println!("Found {} files to process", files.len());
|
||||
|
||||
let mut total_rebuilt = 0;
|
||||
let mut total_skipped = 0;
|
||||
let mut total_errors = 0;
|
||||
let total_rebuilt = Arc::new(AtomicUsize::new(0));
|
||||
let total_skipped = Arc::new(AtomicUsize::new(0));
|
||||
let total_errors = Arc::new(AtomicUsize::new(0));
|
||||
|
||||
for file_uuid in &files {
|
||||
match rebuild_file_thumbs(&db, &qdrant, file_uuid).await {
|
||||
Ok((rebuilt, skipped)) => {
|
||||
total_rebuilt += rebuilt;
|
||||
total_skipped += skipped;
|
||||
println!(" ✅ {} : {} rebuilt, {} skipped", file_uuid, rebuilt, skipped);
|
||||
}
|
||||
Err(e) => {
|
||||
total_errors += 1;
|
||||
eprintln!(" ❌ {} : {}", file_uuid, e);
|
||||
// Process files in parallel (4 at a time)
|
||||
let mut tasks = Vec::new();
|
||||
for file_uuid in files {
|
||||
let qdrant = QdrantDb::new();
|
||||
let rebuilt = total_rebuilt.clone();
|
||||
let skipped = total_skipped.clone();
|
||||
let errors = total_errors.clone();
|
||||
|
||||
let task = tokio::spawn(async move {
|
||||
match rebuild_file_thumbs(&qdrant, &file_uuid).await {
|
||||
Ok((r, s)) => {
|
||||
rebuilt.fetch_add(r, Ordering::Relaxed);
|
||||
skipped.fetch_add(s, Ordering::Relaxed);
|
||||
}
|
||||
Err(e) => {
|
||||
errors.fetch_add(1, Ordering::Relaxed);
|
||||
eprintln!(" ❌ {} : {}", file_uuid, e);
|
||||
}
|
||||
}
|
||||
});
|
||||
tasks.push(task);
|
||||
|
||||
// Limit concurrency to 4
|
||||
if tasks.len() >= 4 {
|
||||
tasks.pop().unwrap().await?;
|
||||
}
|
||||
}
|
||||
|
||||
println!("\nDone! Rebuilt: {}, Skipped: {}, Errors: {}", total_rebuilt, total_skipped, total_errors);
|
||||
// Wait for remaining tasks
|
||||
for task in tasks {
|
||||
task.await?;
|
||||
}
|
||||
|
||||
println!(
|
||||
"\nDone! Rebuilt: {}, Skipped: {}, Errors: {}",
|
||||
total_rebuilt.load(Ordering::Relaxed),
|
||||
total_skipped.load(Ordering::Relaxed),
|
||||
total_errors.load(Ordering::Relaxed)
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn rebuild_file_thumbs(
|
||||
db: &PostgresDb,
|
||||
qdrant: &QdrantDb,
|
||||
file_uuid: &str,
|
||||
) -> Result<(usize, usize)> {
|
||||
@@ -61,7 +86,8 @@ async fn rebuild_file_thumbs(
|
||||
return Ok((0, 0));
|
||||
}
|
||||
|
||||
// Get video path
|
||||
// Get video path from DB
|
||||
let db = PostgresDb::new(&config::DATABASE_URL).await?;
|
||||
let video_path: Option<String> = sqlx::query_scalar(
|
||||
"SELECT file_path FROM videos WHERE file_uuid = $1",
|
||||
)
|
||||
@@ -76,7 +102,8 @@ async fn rebuild_file_thumbs(
|
||||
};
|
||||
|
||||
// Group by trace_id and find best face (highest confidence)
|
||||
let mut best_faces: std::collections::HashMap<i32, (i64, i64, i64, i64, i64)> = std::collections::HashMap::new();
|
||||
let mut best_faces: std::collections::HashMap<i32, (f64, i64, f64, f64, f64, f64)> =
|
||||
std::collections::HashMap::new();
|
||||
|
||||
for p in &points {
|
||||
let payload = &p["payload"];
|
||||
@@ -84,14 +111,14 @@ async fn rebuild_file_thumbs(
|
||||
let conf = payload["confidence"].as_f64().unwrap_or(0.0);
|
||||
let frame = payload["frame"].as_i64().unwrap_or(0);
|
||||
let bbox = &payload["bbox"];
|
||||
let x = bbox["x"].as_f64().unwrap_or(0.0) as i64;
|
||||
let y = bbox["y"].as_f64().unwrap_or(0.0) as i64;
|
||||
let w = bbox["width"].as_f64().unwrap_or(0.0) as i64;
|
||||
let h = bbox["height"].as_f64().unwrap_or(0.0) as i64;
|
||||
let x = bbox["x"].as_f64().unwrap_or(0.0);
|
||||
let y = bbox["y"].as_f64().unwrap_or(0.0);
|
||||
let w = bbox["width"].as_f64().unwrap_or(0.0);
|
||||
let h = bbox["height"].as_f64().unwrap_or(0.0);
|
||||
|
||||
let entry = best_faces.entry(tid as i32).or_insert((0, 0, 0, 0, 0));
|
||||
if conf > entry.0 as f64 || entry.0 == 0 {
|
||||
*entry = (conf as i64, frame, x, y, w.max(h)); // Use max(w,h) for square crop
|
||||
let entry = best_faces.entry(tid as i32).or_insert((0.0, 0, 0.0, 0.0, 0.0, 0.0));
|
||||
if conf > entry.0 {
|
||||
*entry = (conf, frame, x, y, w, h);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -100,8 +127,10 @@ async fn rebuild_file_thumbs(
|
||||
let mut rebuilt = 0;
|
||||
let mut skipped = 0;
|
||||
|
||||
for (trace_id, (_conf, frame, x, y, size)) in &best_faces {
|
||||
let trace_dir = PathBuf::from(output_dir).join(file_uuid).join(format!("trace_{}", trace_id));
|
||||
for (trace_id, (_conf, frame, x, y, w, h)) in &best_faces {
|
||||
let trace_dir = PathBuf::from(output_dir)
|
||||
.join(file_uuid)
|
||||
.join(format!("trace_{}", trace_id));
|
||||
let key_face_path = trace_dir.join("key_face.jpg");
|
||||
|
||||
// Check if already exists and valid
|
||||
@@ -116,49 +145,65 @@ async fn rebuild_file_thumbs(
|
||||
// Create directory
|
||||
std::fs::create_dir_all(&trace_dir)?;
|
||||
|
||||
// Extract frame using select=eq(n,frame)
|
||||
let select_filter = format!("select=eq(n\\,{})", frame);
|
||||
let key_frame_tmp = trace_dir.join("key_frame_tmp.jpg");
|
||||
// Extract frame using select=eq(n,frame) for VFR compatibility
|
||||
let frame_num = *frame;
|
||||
let x_val = *x as i64;
|
||||
let y_val = *y as i64;
|
||||
let w_val = *w as i64;
|
||||
let h_val = *h as i64;
|
||||
let select_filter = format!("select=eq(n\\,{})", frame_num);
|
||||
|
||||
// Use ffmpeg pipe to extract frame, then crop in one pipeline
|
||||
let size = (w_val.max(h_val) as f64 * 1.2) as i64; // 20% padding
|
||||
let half = size / 2;
|
||||
let crop_x = (x_val - half).max(0);
|
||||
let crop_y = (y_val - half).max(0);
|
||||
|
||||
let output = Command::new("ffmpeg")
|
||||
.args([
|
||||
"-y", "-v", "quiet",
|
||||
"-i", &video_path,
|
||||
"-vf", &select_filter,
|
||||
"-vf", &format!("select=eq(n\\,{}),crop={}:{}:{}:{}", frame_num, size, size, crop_x, crop_y),
|
||||
"-frames:v", "1",
|
||||
"-q:v", "2",
|
||||
"-f", "image2pipe",
|
||||
"-c:v", "mjpeg",
|
||||
"pipe:1",
|
||||
])
|
||||
.output()?;
|
||||
|
||||
if !output.status.success() || output.stdout.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
std::fs::write(&key_frame_tmp, &output.stdout)?;
|
||||
|
||||
// Crop square around face
|
||||
let half = size / 2;
|
||||
let crop_x = (x - half).max(0);
|
||||
let crop_y = (y - half).max(0);
|
||||
let crop_filter = format!("crop={}:{}:{}:{}", size, size, crop_x, crop_y);
|
||||
|
||||
let output = Command::new("ffmpeg")
|
||||
.args([
|
||||
"-y", "-v", "quiet",
|
||||
"-i", key_frame_tmp.to_str().unwrap(),
|
||||
"-vf", &crop_filter,
|
||||
"-q:v", "2",
|
||||
key_face_path.to_str().unwrap(),
|
||||
])
|
||||
.output()?;
|
||||
.output();
|
||||
|
||||
let _ = std::fs::remove_file(&key_frame_tmp);
|
||||
match output {
|
||||
Ok(out) if out.status.success() && key_face_path.exists() => {
|
||||
rebuilt += 1;
|
||||
}
|
||||
_ => {
|
||||
// Fallback: extract frame first, then crop
|
||||
let tmp_frame = trace_dir.join("tmp_frame.jpg");
|
||||
let _ = Command::new("ffmpeg")
|
||||
.args([
|
||||
"-y", "-v", "quiet",
|
||||
"-i", &video_path,
|
||||
"-vf", &select_filter,
|
||||
"-frames:v", "1",
|
||||
"-q:v", "2",
|
||||
tmp_frame.to_str().unwrap(),
|
||||
])
|
||||
.output();
|
||||
|
||||
if output.status.success() && key_face_path.exists() {
|
||||
rebuilt += 1;
|
||||
if tmp_frame.exists() {
|
||||
let _ = Command::new("ffmpeg")
|
||||
.args([
|
||||
"-y", "-v", "quiet",
|
||||
"-i", tmp_frame.to_str().unwrap(),
|
||||
"-vf", &format!("crop={}:{}:{}:{}", size, size, crop_x, crop_y),
|
||||
"-q:v", "2",
|
||||
key_face_path.to_str().unwrap(),
|
||||
])
|
||||
.output();
|
||||
let _ = std::fs::remove_file(&tmp_frame);
|
||||
if key_face_path.exists() {
|
||||
rebuilt += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user