fix: add min_capture_quality to FaceFilterSettings + trace profile fixes

- Add min_capture_quality to FaceFilterSettings (file_profile.rs)
- Fix store_traced_faces.py: load embeddings from Qdrant with_vector=True
- Fix face_tracker.py: force new traces when frame_gap > 30
- Fix backfill_trace_profiles.py: key_frame select=eq(n,N), bbox scaling, best_frame
- Add best_frame to FaceTraceProfile (Rust + Python)
- Fix min_capture_quality persistence in file-profile API
This commit is contained in:
Accusys
2026-08-13 10:12:21 +08:00
parent 71172fa354
commit 39e9ce4c3b
25 changed files with 723 additions and 131 deletions
+42 -17
View File
@@ -77,15 +77,12 @@ def get_video_path(file_uuid):
def extract_key_frame(video_path, frame_num, fps, output_path):
"""Extract a specific frame from video using ffmpeg."""
if fps <= 0:
return False
timestamp = frame_num / fps
"""Extract a specific frame from video using ffmpeg select filter (precise frame)."""
try:
result = subprocess.run(
["ffmpeg", "-y", "-ss", f"{timestamp:.3f}",
"-i", video_path,
"-vframes", "1", "-vf", "scale=640:-1", "-q:v", "5",
["ffmpeg", "-y", "-i", video_path,
"-vf", f"select=eq(n\\,{frame_num}),scale=640:-1",
"-vframes", "1", "-q:v", "5",
output_path],
capture_output=True, timeout=30
)
@@ -95,11 +92,14 @@ def extract_key_frame(video_path, frame_num, fps, output_path):
return False
def crop_key_face(key_frame_path, bbox, output_path):
"""Crop key_face from key_frame using bbox."""
def crop_key_face(key_frame_path, bbox, output_path, video_width=1920):
"""Crop key_face from key_frame using bbox (scaled to key_frame resolution)."""
x, y, w, h = bbox["x"], bbox["y"], bbox["width"], bbox["height"]
if w <= 0 or h <= 0:
return False
# Scale bbox to key_frame resolution (640px wide)
scale = 640.0 / video_width
x, y, w, h = int(x * scale), int(y * scale), int(w * scale), int(h * scale)
try:
result = subprocess.run(
["ffmpeg", "-y", "-i", key_frame_path,
@@ -139,7 +139,7 @@ def build_trace_profiles(file_uuid=None, dry_run=False):
{"key": "trace_id", "range": {"gte": 0}},
]
},
with_payload={"include": ["frame", "trace_id", "bbox", "confidence"]}
with_payload={"include": ["frame", "trace_id", "bbox", "confidence", "capture_quality"]}
)
if not points:
@@ -155,6 +155,7 @@ def build_trace_profiles(file_uuid=None, dry_run=False):
"frame": pl["frame"],
"bbox": pl.get("bbox", {}),
"confidence": pl.get("confidence", 0.0),
"capture_quality": pl.get("capture_quality", 0),
})
print(f" {len(points)} points, {len(traces)} traces")
@@ -165,19 +166,22 @@ def build_trace_profiles(file_uuid=None, dry_run=False):
print(f" Video not found, skipping key_frame extraction")
video_path = None
# Get FPS from DB
# Get FPS and width from DB
fps = 30.0
video_width = 1920
if video_path:
psql = "/opt/homebrew/Cellar/libpq/18.4/bin/psql"
result = subprocess.run(
[psql, "-U", "accusys", "-d", "momentry", "-t", "-A", "-c",
f"SELECT COALESCE(fps, 30.0) FROM videos WHERE file_uuid = '{fid}'"],
f"SELECT COALESCE(fps, 30.0), COALESCE(width, 1920) FROM videos WHERE file_uuid = '{fid}'"],
capture_output=True, text=True
)
if result.returncode == 0 and result.stdout.strip():
parts = result.stdout.strip().split('|')
try:
fps = float(result.stdout.strip())
except ValueError:
fps = float(parts[0])
video_width = int(parts[1])
except (ValueError, IndexError):
pass
for tid, faces in sorted(traces.items()):
@@ -186,15 +190,30 @@ def build_trace_profiles(file_uuid=None, dry_run=False):
frames = [f["frame"] for f in faces]
confidences = [f["confidence"] for f in faces]
capture_qualities = [f.get("capture_quality", 0) for f in faces]
bboxes = [f.get("bbox", {}) for f in faces if f.get("bbox")]
frame_count = len(faces)
start_frame = min(frames)
end_frame = max(frames)
avg_confidence = sum(confidences) / frame_count if frame_count > 0 else 0.0
# Representative frame: highest confidence
best = max(faces, key=lambda f: f["confidence"])
# Representative frame: highest capture_quality
best = max(faces, key=lambda f: f.get("capture_quality", 0))
best_frame = best["frame"]
best_bbox = best["bbox"]
best_confidence = best["confidence"]
cq_best_quality = best.get("capture_quality", 0)
# Average bbox (approximate centroid)
avg_bbox = {}
if bboxes:
avg_bbox = {
"x": int(sum(b.get("x", 0) for b in bboxes) / len(bboxes)),
"y": int(sum(b.get("y", 0) for b in bboxes) / len(bboxes)),
"width": int(sum(b.get("width", 0) for b in bboxes) / len(bboxes)),
"height": int(sum(b.get("height", 0) for b in bboxes) / len(bboxes)),
}
avg_capture_quality = sum(capture_qualities) / frame_count if frame_count > 0 else 0.0
trace_dir = os.path.join(OUTPUT_DIR, fid, f"trace_{tid}")
profile_path = os.path.join(trace_dir, "trace_profile.json")
@@ -209,7 +228,13 @@ def build_trace_profiles(file_uuid=None, dry_run=False):
"frame_count": frame_count,
"start_frame": start_frame,
"end_frame": end_frame,
"best_frame": best_frame,
"avg_confidence": round(avg_confidence, 6),
"best_confidence": round(best_confidence, 6),
"avg_capture_quality": round(avg_capture_quality, 6),
"best_capture_quality": round(cq_best_quality, 6),
"avg_bbox": avg_bbox,
"best_bbox": best_bbox,
"key_frame": "key_frame.jpg" if os.path.exists(kf_path) else None,
"key_face": "key_face.jpg" if os.path.exists(face_path) else None,
"status": "pending",
@@ -230,7 +255,7 @@ def build_trace_profiles(file_uuid=None, dry_run=False):
# Crop key_face.jpg from key_frame if not exists
if not os.path.exists(face_path) and os.path.exists(kf_path) and best_bbox:
crop_key_face(kf_path, best_bbox, face_path)
crop_key_face(kf_path, best_bbox, face_path, video_width)
if os.path.exists(face_path):
profile["key_face"] = "key_face.jpg"