feat: implement service management API for VLM control

- Add /api/v1/mbe/health endpoint for status checks
- Add /api/v2/service/start to start llama-server
- Add /api/v2/service/stop to stop llama-server
- Use TCP connection test for port detection (more reliable than lsof)
- Add ps aux fallback for PID detection
- Implement structured bilingual VLM tags
- Add VLM integration documentation for Studio team

Endpoints tested and working with Momentry Studio AdminView UI.
This commit is contained in:
Accusys
2026-10-24 02:37:53 +08:00
parent d14cd9f3de
commit c151b49334
33 changed files with 3175 additions and 229 deletions
@@ -83,6 +83,8 @@ struct TraceInfo {
total_appearances: usize,
avg_confidence: f32,
path: Vec<TracePoint>,
#[serde(skip_serializing_if = "Vec::is_empty")]
frames: Vec<TracePoint>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
@@ -106,6 +108,50 @@ struct FaceTracedData {
traces: HashMap<String, TraceInfo>,
}
/// Load existing trace_ids from face_traced.json if it exists.
/// Returns a map of (frame, face_idx) -> trace_id.
fn load_existing_trace_ids(traced_path: &std::path::Path) -> HashMap<(i64, usize), i32> {
let mut result = HashMap::new();
if !traced_path.exists() {
return result;
}
let content = match std::fs::read_to_string(traced_path) {
Ok(c) => c,
Err(_) => return result,
};
let traced_data: serde_json::Value = match serde_json::from_str(&content) {
Ok(d) => d,
Err(_) => return result,
};
// Read trace_ids directly from frames array
if let Some(frames) = traced_data.get("frames").and_then(|f| f.as_array()) {
for frame in frames {
let frame_num = frame.get("frame").and_then(|f| f.as_i64()).unwrap_or(0);
if let Some(faces) = frame.get("faces").and_then(|f| f.as_array()) {
for (face_idx, face) in faces.iter().enumerate() {
if let Some(trace_id) = face.get("trace_id").and_then(|t| t.as_i64()) {
result.insert((frame_num, face_idx), trace_id as i32);
}
}
}
}
}
result
}
/// Check if two bboxes match within tolerance (±tolerance pixels)
fn bboxes_match(b1: &BBox, b2: &BBox, tolerance: i32) -> bool {
(b1.x - b2.x).abs() <= tolerance &&
(b1.y - b2.y).abs() <= tolerance &&
(b1.width - b2.width).abs() <= tolerance &&
(b1.height - b2.height).abs() <= tolerance
}
fn main() -> Result<()> {
let args = Args::parse();
@@ -131,11 +177,19 @@ fn main() -> Result<()> {
println!("[TRACE_STORE] Loading embeddings from Qdrant...");
let qdrant_faces = load_qdrant_faces(&args.file_uuid);
// Load existing trace_ids from face_traced.json if available
let traced_json_path = args.output_dir.join(format!("{}.face_traced.json", args.file_uuid));
let existing_trace_ids = load_existing_trace_ids(&traced_json_path);
if !existing_trace_ids.is_empty() {
println!("[TRACE_STORE] Loaded {} existing trace_ids from face_traced.json", existing_trace_ids.len());
}
// Run face tracking
println!("[TRACE_STORE] Running face tracking with embedding similarity (threshold={})...", args.similarity_threshold);
let traces = run_face_tracking(
&mut face_data,
&qdrant_faces,
&existing_trace_ids,
args.min_trace_len,
args.min_confidence,
args.similarity_threshold,
@@ -179,6 +233,7 @@ fn main() -> Result<()> {
fn run_face_tracking(
face_data: &mut FaceData,
qdrant_faces: &[FacePoint],
existing_trace_ids: &HashMap<(i64, usize), i32>,
min_trace_len: usize,
min_confidence: f32,
similarity_threshold: f64,
@@ -187,6 +242,12 @@ fn run_face_tracking(
let mut traces: HashMap<String, TraceInfo> = HashMap::new();
let mut next_trace_id = 1;
// If we have existing trace_ids, use the max existing ID as starting point
if !existing_trace_ids.is_empty() {
next_trace_id = existing_trace_ids.values().copied().max().unwrap_or(0) + 1;
println!("[TRACE] Resuming from existing trace_ids, next_id={}", next_trace_id);
}
// Build frame -> embedding map from Qdrant data
let mut frame_embeddings: HashMap<i64, Vec<(Vec<f64>, f32, BBox)>> = HashMap::new();
for point in qdrant_faces {
@@ -234,13 +295,36 @@ fn run_face_tracking(
for face_idx in 0..frame.faces.len() {
let current_face = &frame.faces[face_idx];
// Find matching embedding
// First check if we have an existing trace_id for this face
if let Some(&existing_tid) = existing_trace_ids.get(&(current_frame_num, face_idx)) {
face_trace_ids.insert((current_frame_num, face_idx), existing_tid);
let trace_key = existing_tid.to_string();
let trace = traces.entry(trace_key).or_insert(TraceInfo {
trace_id: existing_tid,
total_appearances: 0,
avg_confidence: 0.0,
path: Vec::new(),
frames: Vec::new(),
});
trace.total_appearances += 1;
trace.path.push(TracePoint {
frame: current_frame_num,
bbox: current_face.bbox.clone(),
confidence: current_face.confidence,
});
trace.frames.push(TracePoint {
frame: current_frame_num,
bbox: current_face.bbox.clone(),
confidence: current_face.confidence,
});
continue;
}
// Find matching embedding with bbox tolerance (±5px)
let current_embedding = current_embeddings.iter()
.find(|(_, _, bbox)| {
bbox.x == current_face.bbox.x &&
bbox.y == current_face.bbox.y &&
bbox.width == current_face.bbox.width &&
bbox.height == current_face.bbox.height
bboxes_match(bbox, &current_face.bbox, 5)
})
.map(|(emb, _, _)| emb.clone());
@@ -260,14 +344,11 @@ fn run_face_tracking(
if let Some(&prev_trace_id) = face_trace_ids.get(&(prev_frame.frame, prev_face_idx)) {
let iou = calculate_iou(&current_face.bbox, &prev_face.bbox);
// Get previous embedding
// Get previous embedding with bbox tolerance
let prev_embeddings = frame_embeddings.get(&prev_frame.frame).cloned().unwrap_or_default();
let prev_embedding = prev_embeddings.iter()
.find(|(_, _, bbox)| {
bbox.x == prev_face.bbox.x &&
bbox.y == prev_face.bbox.y &&
bbox.width == prev_face.bbox.width &&
bbox.height == prev_face.bbox.height
bboxes_match(bbox, &prev_face.bbox, 5)
})
.map(|(emb, _, _)| emb);
@@ -307,6 +388,7 @@ fn run_face_tracking(
total_appearances: 0,
avg_confidence: 0.0,
path: Vec::new(),
frames: Vec::new(),
});
trace.total_appearances += 1;
@@ -315,6 +397,11 @@ fn run_face_tracking(
bbox: current_face.bbox.clone(),
confidence: current_face.confidence,
});
trace.frames.push(TracePoint {
frame: current_frame_num,
bbox: current_face.bbox.clone(),
confidence: current_face.confidence,
});
}
}