test: add Rust face_tracker test suite and documentation
- Add 3 unit tests for core algorithms (IoU, distance, similarity) - Add integration test stubs for real data testing - Create comprehensive test report - Document performance expectations - Note playground compilation issues for future fixes
This commit is contained in:
@@ -0,0 +1,197 @@
|
||||
use momentry_core::core::processor::face::FaceResult;
|
||||
use momentry_core::core::processor::face_tracker::track_faces;
|
||||
|
||||
#[test]
|
||||
fn test_track_faces_with_real_data() {
|
||||
// 測試數據
|
||||
let face_json = r#"{
|
||||
"status": "has_faces",
|
||||
"frame_count": 3,
|
||||
"fps": 30.0,
|
||||
"total_faces": 3,
|
||||
"frames": [
|
||||
{
|
||||
"frame": 0,
|
||||
"timestamp": 0.0,
|
||||
"faces": [
|
||||
{
|
||||
"x": 100,
|
||||
"y": 100,
|
||||
"width": 50,
|
||||
"height": 60,
|
||||
"confidence": 0.9,
|
||||
"embedding": [0.1, 0.2, 0.3],
|
||||
"trace_id": null
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"frame": 1,
|
||||
"timestamp": 0.033,
|
||||
"faces": [
|
||||
{
|
||||
"x": 102,
|
||||
"y": 101,
|
||||
"width": 51,
|
||||
"height": 61,
|
||||
"confidence": 0.88,
|
||||
"embedding": [0.11, 0.21, 0.31],
|
||||
"trace_id": null
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"frame": 2,
|
||||
"timestamp": 0.066,
|
||||
"faces": [
|
||||
{
|
||||
"x": 105,
|
||||
"y": 102,
|
||||
"width": 52,
|
||||
"height": 62,
|
||||
"confidence": 0.87,
|
||||
"embedding": [0.12, 0.22, 0.32],
|
||||
"trace_id": null
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}"#;
|
||||
|
||||
// 解析
|
||||
let mut face_result: FaceResult = serde_json::from_str(face_json)
|
||||
.expect("Failed to parse test data");
|
||||
|
||||
println!("測試數據:");
|
||||
println!(" Frames: {}", face_result.frames.len());
|
||||
println!(" Total faces: {}", face_result.frames.iter().map(|f| f.faces.len()).sum::<usize>());
|
||||
|
||||
// 執行追蹤
|
||||
track_faces(&mut face_result, 0.3, 0.7, 100.0, true, None)
|
||||
.expect("Failed to track faces");
|
||||
|
||||
println!("\n追蹤完成");
|
||||
|
||||
// 驗證 trace_id
|
||||
let mut trace_ids = Vec::new();
|
||||
for frame in &face_result.frames {
|
||||
for face in &frame.faces {
|
||||
if let Some(trace_id) = face.trace_id {
|
||||
trace_ids.push(trace_id);
|
||||
println!(" Frame {} face: trace_id = {}", frame.frame, trace_id);
|
||||
} else {
|
||||
panic!("Face without trace_id!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 所有 faces 應該有相同的 trace_id(因為它們是同一個人)
|
||||
assert_eq!(trace_ids.len(), 3);
|
||||
assert!(trace_ids.iter().all(|&id| id == trace_ids[0]));
|
||||
|
||||
// 驗證 metadata
|
||||
assert!(face_result.metadata.is_some());
|
||||
let metadata = face_result.metadata.as_ref().unwrap();
|
||||
assert!(metadata.get("traces").is_some());
|
||||
|
||||
println!("\n✅ 測試通過");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_track_multiple_faces() {
|
||||
let face_json = r#"{
|
||||
"status": "has_faces",
|
||||
"frame_count": 2,
|
||||
"fps": 30.0,
|
||||
"total_faces": 4,
|
||||
"frames": [
|
||||
{
|
||||
"frame": 0,
|
||||
"timestamp": 0.0,
|
||||
"faces": [
|
||||
{
|
||||
"x": 100,
|
||||
"y": 100,
|
||||
"width": 50,
|
||||
"height": 60,
|
||||
"confidence": 0.9,
|
||||
"embedding": [0.1, 0.2, 0.3],
|
||||
"trace_id": null
|
||||
},
|
||||
{
|
||||
"x": 500,
|
||||
"y": 100,
|
||||
"width": 50,
|
||||
"height": 60,
|
||||
"confidence": 0.85,
|
||||
"embedding": [0.9, 0.8, 0.7],
|
||||
"trace_id": null
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"frame": 1,
|
||||
"timestamp": 0.033,
|
||||
"faces": [
|
||||
{
|
||||
"x": 102,
|
||||
"y": 101,
|
||||
"width": 51,
|
||||
"height": 61,
|
||||
"confidence": 0.88,
|
||||
"embedding": [0.11, 0.21, 0.31],
|
||||
"trace_id": null
|
||||
},
|
||||
{
|
||||
"x": 502,
|
||||
"y": 101,
|
||||
"width": 51,
|
||||
"height": 61,
|
||||
"confidence": 0.84,
|
||||
"embedding": [0.91, 0.81, 0.71],
|
||||
"trace_id": null
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}"#;
|
||||
|
||||
let mut face_result: FaceResult = serde_json::from_str(face_json)
|
||||
.expect("Failed to parse test data");
|
||||
|
||||
track_faces(&mut face_result, 0.3, 0.7, 100.0, true, None)
|
||||
.expect("Failed to track faces");
|
||||
|
||||
// 應該有兩個不同的 trace_id
|
||||
let mut trace_ids = std::collections::HashSet::new();
|
||||
for frame in &face_result.frames {
|
||||
for face in &frame.faces {
|
||||
if let Some(trace_id) = face.trace_id {
|
||||
trace_ids.insert(trace_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert_eq!(trace_ids.len(), 2);
|
||||
println!("✅ 多人追蹤測試通過: {} 個 traces", trace_ids.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_track_no_faces() {
|
||||
let face_json = r#"{
|
||||
"status": "no_faces",
|
||||
"frame_count": 10,
|
||||
"fps": 30.0,
|
||||
"total_faces": 0,
|
||||
"frames": []
|
||||
}"#;
|
||||
|
||||
let mut face_result: FaceResult = serde_json::from_str(face_json)
|
||||
.expect("Failed to parse test data");
|
||||
|
||||
track_faces(&mut face_result, 0.3, 0.7, 100.0, true, None)
|
||||
.expect("Failed to track faces");
|
||||
|
||||
// 不應該崩潰
|
||||
println!("✅ 無人臉測試通過");
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
use momentry_core::core::processor::face::FaceResult;
|
||||
use momentry_core::core::processor::face_tracker::track_faces;
|
||||
|
||||
#[test]
|
||||
fn test_track_real_face_json() {
|
||||
// 讀取實際的 face.json
|
||||
let face_json_path = "/Users/accusys/momentry/output/b496a68800f90770aa7246c877f38985.face.json";
|
||||
|
||||
if !std::path::Path::new(face_json_path).exists() {
|
||||
println!("⚠️ face.json not found, skipping real data test");
|
||||
return;
|
||||
}
|
||||
|
||||
let face_json = std::fs::read_to_string(face_json_path)
|
||||
.expect("Failed to read face.json");
|
||||
|
||||
let mut face_result: FaceResult = serde_json::from_str(&face_json)
|
||||
.expect("Failed to parse face.json");
|
||||
|
||||
println!("\n實際數據測試:");
|
||||
println!(" Frames: {}", face_result.frames.len());
|
||||
println!(" FPS: {}", face_result.fps);
|
||||
println!(" Total faces: {}", face_result.frames.iter().map(|f| f.faces.len()).sum::<usize>());
|
||||
|
||||
// 執行追蹤
|
||||
let start = std::time::Instant::now();
|
||||
track_faces(&mut face_result, 0.3, 0.7, 100.0, true, None)
|
||||
.expect("Failed to track faces");
|
||||
let duration = start.elapsed();
|
||||
|
||||
println!("\n✅ 追蹤完成");
|
||||
println!(" 耗時: {:?}", duration);
|
||||
|
||||
// 統計 trace_id
|
||||
let mut total_faces = 0;
|
||||
let mut faces_with_trace_id = 0;
|
||||
let mut trace_ids = std::collections::HashSet::new();
|
||||
|
||||
for frame in &face_result.frames {
|
||||
for face in &frame.faces {
|
||||
total_faces += 1;
|
||||
if let Some(trace_id) = face.trace_id {
|
||||
faces_with_trace_id += 1;
|
||||
trace_ids.insert(trace_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("\n追蹤結果:");
|
||||
println!(" Total faces: {}", total_faces);
|
||||
println!(" Faces with trace_id: {}", faces_with_trace_id);
|
||||
println!(" Unique traces: {}", trace_ids.len());
|
||||
|
||||
// 驗證所有 faces 都有 trace_id
|
||||
assert_eq!(faces_with_trace_id, total_faces, "Not all faces have trace_id!");
|
||||
|
||||
// 驗證 metadata
|
||||
assert!(face_result.metadata.is_some(), "Metadata is missing!");
|
||||
|
||||
let metadata = face_result.metadata.as_ref().unwrap();
|
||||
let traces = metadata.get("traces")
|
||||
.expect("Traces missing in metadata")
|
||||
.as_object()
|
||||
.expect("Traces is not an object");
|
||||
|
||||
println!(" Traces in metadata: {}", traces.len());
|
||||
|
||||
// 顯示一些追蹤信息
|
||||
println!("\n前 5 個 traces:");
|
||||
for (i, (trace_id, trace_data)) in traces.iter().take(5).enumerate() {
|
||||
if let Some(start_frame) = trace_data.get("start_frame") {
|
||||
if let Some(end_frame) = trace_data.get("end_frame") {
|
||||
println!(" Trace {}: frames {} - {}", trace_id, start_frame, end_frame);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("\n✅ 實際數據測試通過");
|
||||
}
|
||||
Reference in New Issue
Block a user