## v0.9.20260325_144654 ### Features - API Key Authentication System - Job Worker System - V2 Backup Versioning ### Bug Fixes - get_processor_results_by_job column mapping Co-authored-by: OpenCode
155 lines
4.4 KiB
Python
Executable File
155 lines
4.4 KiB
Python
Executable File
#!/opt/homebrew/bin/python3.11
|
|
"""
|
|
Face Processor - Face Detection
|
|
Uses OpenCV Haar Cascade (local, no extra download needed)
|
|
Alternative: MediaPipe (requires model download)
|
|
"""
|
|
|
|
import sys
|
|
import json
|
|
import argparse
|
|
import os
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
from redis_publisher import RedisPublisher
|
|
|
|
|
|
def process_face(video_path: str, output_path: str, uuid: str = ""):
|
|
"""Process video for face detection"""
|
|
|
|
publisher = RedisPublisher(uuid) if uuid else None
|
|
if publisher:
|
|
publisher.info("face", "FACE_START")
|
|
|
|
try:
|
|
import cv2
|
|
except ImportError:
|
|
if publisher:
|
|
publisher.error("face", "opencv-python not installed")
|
|
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():
|
|
if publisher:
|
|
publisher.error("face", "Could not load Haar Cascade")
|
|
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_CASCADE_LOADED")
|
|
|
|
# Get video info
|
|
cap = cv2.VideoCapture(video_path)
|
|
fps = cap.get(cv2.CAP_PROP_FPS)
|
|
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
|
cap.release()
|
|
|
|
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)
|
|
|
|
while True:
|
|
ret, frame = cap.read()
|
|
if not ret:
|
|
break
|
|
|
|
frame_count += 1
|
|
|
|
# Sample frames
|
|
if frame_count % sample_interval != 0:
|
|
continue
|
|
|
|
processed += 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
|
|
if face_list:
|
|
frames.append(
|
|
{
|
|
"frame": frame_count - 1,
|
|
"timestamp": round(timestamp, 3),
|
|
"faces": face_list,
|
|
}
|
|
)
|
|
if publisher:
|
|
publisher.progress(
|
|
"face",
|
|
processed,
|
|
total_frames // sample_interval,
|
|
f"Frame {frame_count}",
|
|
)
|
|
|
|
cap.release()
|
|
|
|
result = {"frame_count": total_frames, "fps": fps, "frames": frames}
|
|
|
|
if publisher:
|
|
publisher.complete("face", f"{len(frames)} frames with faces")
|
|
|
|
with open(output_path, "w") as f:
|
|
json.dump(result, f, indent=2)
|
|
|
|
return result
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description="Face Detection")
|
|
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="")
|
|
args = parser.parse_args()
|
|
|
|
process_face(args.video_path, args.output_path, args.uuid)
|