feat: opt-in face trace quality filter (min_trace_len/min_confidence, default off)
This commit is contained in:
@@ -59,6 +59,58 @@ def merge_traces_within_cuts(face_data: dict, cut_scenes: list) -> dict:
|
||||
return face_data
|
||||
|
||||
|
||||
def filter_traces(
|
||||
face_data: dict, min_trace_len: int = 0, min_confidence: float = 0.0
|
||||
) -> dict:
|
||||
"""Optionally drop low-quality traces before writing face_traced.json.
|
||||
|
||||
A trace is dropped when:
|
||||
- its path length (total_appearances) < min_trace_len, OR
|
||||
- its avg_confidence < min_confidence (only when min_confidence > 0).
|
||||
|
||||
For every dropped trace, the corresponding faces in `frames` have their
|
||||
`trace_id` set to None (NOT deleted) so frames/traces stay consistent and
|
||||
downstream readers (`trace_id is None` → skip) are unaffected. Trace ids of
|
||||
surviving traces are NOT renumbered.
|
||||
|
||||
Both thresholds default to "no filtering" so existing behaviour is unchanged
|
||||
unless the caller opts in (env vars / CLI flags).
|
||||
"""
|
||||
traces = face_data.get("traces", {})
|
||||
if not traces or (min_trace_len <= 0 and min_confidence <= 0.0):
|
||||
return face_data
|
||||
|
||||
drop_ids = set()
|
||||
for tid_str, trace in traces.items():
|
||||
appearances = trace.get("total_appearances", len(trace.get("path", [])))
|
||||
avg_conf = trace.get("avg_confidence", 1.0)
|
||||
if appearances < min_trace_len:
|
||||
drop_ids.add(tid_str)
|
||||
elif min_confidence > 0.0 and avg_conf < min_confidence:
|
||||
drop_ids.add(tid_str)
|
||||
|
||||
if not drop_ids:
|
||||
return face_data
|
||||
|
||||
# Remove dropped traces from the traces dict
|
||||
for tid_str in drop_ids:
|
||||
traces.pop(tid_str, None)
|
||||
|
||||
# Clear trace_id on frames for dropped traces (keep the face detection itself)
|
||||
drop_ints = {int(t) for t in drop_ids}
|
||||
for frame_data in face_data.get("frames", {}).values():
|
||||
for face in frame_data.get("faces", []):
|
||||
if face.get("trace_id") in drop_ints:
|
||||
face["trace_id"] = None
|
||||
|
||||
print(
|
||||
f"[TRACE] Quality filter: dropped {len(drop_ids)} traces "
|
||||
f"(min_trace_len={min_trace_len}, min_confidence={min_confidence}), "
|
||||
f"kept {len(traces)}"
|
||||
)
|
||||
return face_data
|
||||
|
||||
|
||||
def run_face_tracker(
|
||||
face_json_path: str, traced_json_path: str, filter_eyes: bool = False
|
||||
) -> str:
|
||||
@@ -176,6 +228,12 @@ def run_face_tracker(
|
||||
if cut_scenes and len(cut_scenes) > 0:
|
||||
face_data = merge_traces_within_cuts(face_data, cut_scenes)
|
||||
|
||||
# Optional quality filter (opt-in via env; both default to 0 = disabled).
|
||||
min_trace_len = int(os.environ.get("MOMENTRY_FACE_MIN_TRACE_LEN", "0"))
|
||||
min_confidence = float(os.environ.get("MOMENTRY_FACE_MIN_CONFIDENCE", "0.0"))
|
||||
if min_trace_len > 0 or min_confidence > 0.0:
|
||||
face_data = filter_traces(face_data, min_trace_len, min_confidence)
|
||||
|
||||
metadata = face_data.get("metadata", {})
|
||||
metadata["tracking_method"] = "iou_only"
|
||||
metadata["tracked_at"] = datetime.now().isoformat()
|
||||
|
||||
Reference in New Issue
Block a user