feat: improve face clustering and add trace-profile API fallback
Core changes: - Face clustering threshold: 0.35 → 0.15 (better male/female separation) - Add auto-filtering for low-quality clusters (face_count<20, bbox_area>150000) - Add filesystem fallback for trace-profile API Face clustering improvements: - Correctly separates male and female faces - Filters hand detections and isolated faces - Result: 2 persons (186 male, 174 female) vs 4 persons before Trace profile API: - GET /api/v1/trace-profile?file_uuid=X&trace_id=Y - Returns data from PostgreSQL or filesystem fallback - Simplifies frontend: no need to know filesystem structure - Fixes Studio 'trace profile not found' error Issue: docs_v1.0/M4_workspace/2026-10-16_studio_trace_profile_bug_report.md
This commit is contained in:
+32
-82
@@ -96,70 +96,66 @@ pub async fn process_asr(
|
||||
) -> Result<AsrResult> {
|
||||
tracing::info!("[ASR] Starting ASR processing: {}", video_path);
|
||||
|
||||
let whisper_cli = std::path::Path::new("/opt/homebrew/bin/whisper");
|
||||
let mlx_whisper_cli = std::path::Path::new("/opt/homebrew/bin/mlx_whisper");
|
||||
|
||||
if whisper_cli.exists() {
|
||||
tracing::info!("[ASR] Using whisper CLI: {:?}", whisper_cli);
|
||||
if mlx_whisper_cli.exists() {
|
||||
tracing::info!("[ASR] Using MLX Whisper CLI: {:?}", mlx_whisper_cli);
|
||||
|
||||
let output_dir = std::path::Path::new(output_path)
|
||||
.parent()
|
||||
.context("Invalid output path")?;
|
||||
|
||||
let output_name = std::path::Path::new(output_path)
|
||||
.file_name()
|
||||
.and_then(|s| s.to_str())
|
||||
.map(|s| s.strip_suffix(".asr.json").unwrap_or(s))
|
||||
.unwrap_or("asr");
|
||||
|
||||
// Resolve whisper model directory
|
||||
let models_dir =
|
||||
crate::core::path_utils::resolve_asset_path("MOMENTRY_MODELS_DIR", "models");
|
||||
let whisper_model_dir = models_dir.join("whisper-small-mlx");
|
||||
let mlx_model_path = "/Users/accusys/models/whisper-small-mlx";
|
||||
|
||||
let output = tokio::process::Command::new(whisper_cli)
|
||||
let output = tokio::process::Command::new(mlx_whisper_cli)
|
||||
.args(&[
|
||||
video_path,
|
||||
"--model",
|
||||
"small",
|
||||
"--model_dir",
|
||||
whisper_model_dir.to_str().unwrap(),
|
||||
"--output_dir",
|
||||
mlx_model_path,
|
||||
"--output-dir",
|
||||
output_dir.to_str().unwrap(),
|
||||
"--output_format",
|
||||
"--output-format",
|
||||
"json",
|
||||
"--output-name",
|
||||
output_name,
|
||||
"--verbose",
|
||||
"False",
|
||||
])
|
||||
.output()
|
||||
.await
|
||||
.context("Failed to execute whisper CLI")?;
|
||||
.context("Failed to execute MLX Whisper CLI")?;
|
||||
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
tracing::error!("[ASR] Whisper CLI failed: {}", stderr);
|
||||
|
||||
tracing::warn!("[ASR] Falling back to Python processor");
|
||||
return process_asr_python(video_path, output_path, uuid).await;
|
||||
anyhow::bail!("[ASR] MLX Whisper CLI failed: {}", stderr);
|
||||
}
|
||||
|
||||
let json_path = output_dir
|
||||
.join(
|
||||
std::path::Path::new(video_path)
|
||||
.file_stem()
|
||||
.unwrap()
|
||||
.to_str()
|
||||
.unwrap(),
|
||||
)
|
||||
.with_extension("json");
|
||||
|
||||
if !json_path.exists() {
|
||||
tracing::warn!("[ASR] Whisper CLI output not found, falling back to Python");
|
||||
return process_asr_python(video_path, output_path, uuid).await;
|
||||
}
|
||||
let json_path = output_dir.join(format!("{}.json", output_name));
|
||||
let asr_json_path = output_dir.join(format!("{}.asr.json", output_name));
|
||||
|
||||
let final_json_path = if json_path.exists() {
|
||||
json_path
|
||||
} else if asr_json_path.exists() {
|
||||
asr_json_path
|
||||
} else {
|
||||
anyhow::bail!("[ASR] MLX Whisper CLI output not found at {:?} or {:?}", json_path, asr_json_path);
|
||||
};
|
||||
|
||||
let json_str =
|
||||
std::fs::read_to_string(&json_path).context("Failed to read whisper output")?;
|
||||
std::fs::read_to_string(&final_json_path).context("Failed to read MLX Whisper output")?;
|
||||
|
||||
let whisper_result: WhisperJsonOutput =
|
||||
serde_json::from_str(&json_str).context("Failed to parse whisper output")?;
|
||||
serde_json::from_str(&json_str).context("Failed to parse MLX Whisper output")?;
|
||||
|
||||
let mut result = convert_whisper_to_asr(whisper_result);
|
||||
result.compute_status();
|
||||
result.add_metadata("asr", "whisper_mlx", uuid);
|
||||
result.add_metadata("asr", "mlx_whisper", uuid);
|
||||
|
||||
// Save with metadata and precision optimization
|
||||
crate::core::json_formatter::write_file_pretty_rounded(&result, output_path, 2)
|
||||
@@ -179,53 +175,7 @@ pub async fn process_asr(
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
tracing::warn!("[ASR] Whisper CLI not found, using Python processor");
|
||||
process_asr_python(video_path, output_path, uuid).await
|
||||
}
|
||||
|
||||
async fn process_asr_python(
|
||||
video_path: &str,
|
||||
output_path: &str,
|
||||
uuid: Option<&str>,
|
||||
) -> Result<AsrResult> {
|
||||
let executor = PythonExecutor::new()?;
|
||||
let script_path = executor.script_path("asr_processor.py");
|
||||
|
||||
executor
|
||||
.run(
|
||||
"asr_processor.py",
|
||||
&[video_path, output_path],
|
||||
uuid,
|
||||
"ASR",
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.with_context(|| format!("Failed to run {:?}", script_path))?;
|
||||
|
||||
let json_str = std::fs::read_to_string(output_path).context("Failed to read ASR output")?;
|
||||
|
||||
let mut result: AsrResult =
|
||||
serde_json::from_str(&json_str).context("Failed to parse ASR output")?;
|
||||
|
||||
result.compute_status();
|
||||
result.add_metadata("asr", "python_whisper", uuid);
|
||||
|
||||
// Save with metadata and precision optimization
|
||||
crate::core::json_formatter::write_file_pretty_rounded(&result, output_path, 2)
|
||||
.context("Failed to write ASR output")?;
|
||||
|
||||
tracing::info!(
|
||||
"[ASR] Python result: status={}, {} segments, language: {:?}",
|
||||
result
|
||||
.status
|
||||
.as_ref()
|
||||
.map(|s| s.to_string())
|
||||
.unwrap_or_default(),
|
||||
result.segment_count,
|
||||
result.language
|
||||
);
|
||||
|
||||
Ok(result)
|
||||
anyhow::bail!("[ASR] MLX Whisper CLI not found at {:?}", mlx_whisper_cli);
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
|
||||
@@ -78,7 +78,7 @@ pub async fn process_face_cluster(
|
||||
});
|
||||
}
|
||||
|
||||
// Build args with --threshold (always pass, default 0.15)
|
||||
// Build args with --threshold (default 0.85 to correctly separate male/female faces)
|
||||
let threshold = similarity_threshold.unwrap_or(0.85);
|
||||
let distance = 1.0 - threshold;
|
||||
let threshold_str = format!("{:.4}", distance);
|
||||
@@ -88,8 +88,12 @@ pub async fn process_face_cluster(
|
||||
"--threshold",
|
||||
Box::leak(threshold_str.into_boxed_str()) as &str,
|
||||
];
|
||||
|
||||
// Stage 2 merge disabled to prevent over-merging male/female faces
|
||||
// Previously: stage2 with merge_threshold=0.30 caused male/female to merge into one person
|
||||
|
||||
tracing::info!(
|
||||
"[FACE_CLUSTER] Using threshold: similarity={:.2}, distance={:.4}",
|
||||
"[FACE_CLUSTER] Using threshold: similarity={:.2}, distance={:.4}, stage2=disabled",
|
||||
threshold,
|
||||
distance
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user