chore: backup before migration to new repo

This commit is contained in:
Warren
2026-04-23 16:46:02 +08:00
parent 13dd3b30f3
commit 59809dae1f
40 changed files with 5566 additions and 1783 deletions

View File

@@ -1,8 +1,8 @@
#!/opt/homebrew/bin/python3.11
"""
Face Processor - Face Detection
Uses OpenCV Haar Cascade (local, no extra download needed)
Alternative: MediaPipe (requires model download)
Face Processor - Face Detection & Demographics
Uses InsightFace for detection, age, and gender analysis.
Falls back to OpenCV Haar Cascade if InsightFace fails.
"""
import sys
@@ -15,7 +15,7 @@ from redis_publisher import RedisPublisher
def process_face(video_path: str, output_path: str, uuid: str = ""):
"""Process video for face detection"""
"""Process video for face detection and demographics analysis"""
publisher = RedisPublisher(uuid) if uuid else None
if publisher:
@@ -23,56 +23,82 @@ def process_face(video_path: str, output_path: str, uuid: str = ""):
try:
import cv2
except ImportError:
import numpy as np
import insightface
except ImportError as e:
error_msg = f"Missing dependency: {e.name}"
if publisher:
publisher.error("face", "opencv-python not installed")
publisher.error("face", error_msg)
result = {"frame_count": 0, "fps": 0.0, "frames": []}
if publisher:
publisher.complete("face", "0 frames")
with open(output_path, "w") as f:
json.dump(result, f, indent=2)
return result
if publisher:
publisher.info("face", "FACE_LOADING_CASCADE")
# Try to use OpenCV's built-in Haar Cascade
# This is included with OpenCV
face_cascade = cv2.CascadeClassifier(
cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
)
if face_cascade.empty():
# 1. Initialize InsightFace
use_insightface = False
app = None
try:
if publisher:
publisher.error("face", "Could not load Haar Cascade")
result = {"frame_count": 0, "fps": 0.0, "frames": []}
publisher.info("face", "LOADING_INSIGHTFACE")
# 'buffalo_l' is a robust model. det_size can be adjusted.
app = insightface.app.FaceAnalysis(
name="buffalo_l", providers=["CPUExecutionProvider"]
)
app.prepare(ctx_id=0, det_size=(320, 320))
use_insightface = True
if publisher:
publisher.complete("face", "0 frames")
with open(output_path, "w") as f:
json.dump(result, f, indent=2)
return result
publisher.info("face", "INSIGHTFACE_LOADED")
except Exception as e:
print(f"[WARNING] InsightFace failed to load: {e}")
use_insightface = False
# 2. Fallback to Haar Cascade
face_cascade = None
if not use_insightface:
if publisher:
publisher.info("face", "LOADING_HAAR_CASCADE")
face_cascade = cv2.CascadeClassifier(
cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
)
if face_cascade.empty():
if publisher:
publisher.error("face", "Could not load Haar Cascade")
result = {"frame_count": 0, "fps": 0.0, "frames": []}
with open(output_path, "w") as f:
json.dump(result, f, indent=2)
return result
if publisher:
publisher.info("face", "HAAR_CASCADE_LOADED")
if publisher:
publisher.info("face", "FACE_CASCADE_LOADED")
publisher.info("face", "PROCESSING_VIDEO")
# Get video info
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
if publisher:
publisher.error("face", "Could not open video")
result = {"frame_count": 0, "fps": 0.0, "frames": []}
with open(output_path, "w") as f:
json.dump(result, f, indent=2)
return result
fps = cap.get(cv2.CAP_PROP_FPS)
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
cap.release()
# Optimization: Process every N frames to speed up analysis
# Since we just need attributes for the person identity, we don't need every single frame.
sample_interval = 30
if total_frames > 0:
estimated_samples = total_frames // sample_interval
else:
estimated_samples = 0
frame_count = 0
processed_count = 0
frames_data = []
if publisher:
publisher.info("face", f"fps={fps}, frames={total_frames}")
publisher.progress("face", 0, total_frames, "Starting")
# Process every N frames to speed up
sample_interval = 30 # Process every 30 frames
frames = []
frame_count = 0
processed = 0
cap = cv2.VideoCapture(video_path)
publisher.progress("face", 0, estimated_samples, "Starting")
while True:
ret, frame = cap.read()
@@ -81,62 +107,92 @@ def process_face(video_path: str, output_path: str, uuid: str = ""):
frame_count += 1
# Sample frames
# Sampling
if frame_count % sample_interval != 0:
continue
processed += 1
processed_count += 1
timestamp = (frame_count - 1) / fps if fps > 0 else 0
# Convert to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces
try:
faces = face_cascade.detectMultiScale(
gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)
)
except Exception as e:
if publisher:
publisher.error("face", f"Frame {frame_count}: {e}")
faces = []
face_list = []
for x, y, w, h in faces:
face_list.append(
{
"face_id": None,
"x": int(x),
"y": int(y),
"width": int(w),
"height": int(h),
"confidence": 0.8, # Haar cascade doesn't provide confidence
}
)
# Only add frames with faces
try:
if use_insightface and app:
# InsightFace Detection & Analysis
faces = app.get(frame)
for face in faces:
bbox = face.bbox.astype(int)
bx, by, bw, bh = (
bbox[0],
bbox[1],
bbox[2] - bbox[0],
bbox[3] - bbox[1],
)
# Extract Attributes
age = int(face.age) if hasattr(face, "age") else None
gender_val = face.gender if hasattr(face, "gender") else None
gender = (
"female"
if gender_val == 0
else ("male" if gender_val == 1 else None)
)
face_list.append(
{
"x": int(bx),
"y": int(by),
"width": int(bw),
"height": int(bh),
"confidence": float(face.det_score)
if hasattr(face, "det_score")
else 0.9,
"attributes": {"age": age, "gender": gender},
}
)
else:
# Haar Cascade Fallback (No Age/Gender)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(
gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)
)
for x, y, w, h in faces:
face_list.append(
{
"x": int(x),
"y": int(y),
"width": int(w),
"height": int(h),
"confidence": 0.8,
"attributes": {"age": None, "gender": None},
}
)
except Exception as e:
print(f"[ERROR] Frame processing error: {e}")
if face_list:
frames.append(
frames_data.append(
{
"frame": frame_count - 1,
"timestamp": round(timestamp, 3),
"faces": face_list,
}
)
if publisher:
publisher.progress(
"face",
processed,
total_frames // sample_interval,
processed_count,
estimated_samples,
f"Frame {frame_count}",
)
cap.release()
result = {"frame_count": total_frames, "fps": fps, "frames": frames}
result = {"frame_count": total_frames, "fps": fps, "frames": frames_data}
if publisher:
publisher.complete("face", f"{len(frames)} frames with faces")
publisher.complete("face", f"{len(frames_data)} frames processed")
with open(output_path, "w") as f:
json.dump(result, f, indent=2)
@@ -145,7 +201,7 @@ def process_face(video_path: str, output_path: str, uuid: str = ""):
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Face Detection")
parser = argparse.ArgumentParser(description="Face Detection & Demographics")
parser.add_argument("video_path", help="Path to video file")
parser.add_argument("output_path", help="Output JSON path")
parser.add_argument("--uuid", "-u", help="UUID for Redis progress", default="")