Upload scripts/detect_video.py with huggingface_hub
Browse files- scripts/detect_video.py +110 -0
scripts/detect_video.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Video face detection with tracking and temporal smoothing.
|
| 4 |
+
|
| 5 |
+
Usage:
|
| 6 |
+
# Process video file
|
| 7 |
+
python scripts/detect_video.py \\
|
| 8 |
+
--model scrfd_34g \\
|
| 9 |
+
--checkpoint checkpoints/scrfd_34g_best.pth \\
|
| 10 |
+
--input video.mp4 \\
|
| 11 |
+
--output output.mp4
|
| 12 |
+
|
| 13 |
+
# Webcam (real-time)
|
| 14 |
+
python scripts/detect_video.py \\
|
| 15 |
+
--model scrfd_2.5g \\
|
| 16 |
+
--checkpoint checkpoints/scrfd_2.5g_best.pth \\
|
| 17 |
+
--input 0 \\
|
| 18 |
+
--show
|
| 19 |
+
|
| 20 |
+
# RTSP stream
|
| 21 |
+
python scripts/detect_video.py \\
|
| 22 |
+
--model scrfd_2.5g \\
|
| 23 |
+
--checkpoint checkpoints/scrfd_2.5g_best.pth \\
|
| 24 |
+
--input rtsp://192.168.1.100/stream \\
|
| 25 |
+
--output output.mp4
|
| 26 |
+
"""
|
| 27 |
+
|
| 28 |
+
import os
|
| 29 |
+
import sys
|
| 30 |
+
import argparse
|
| 31 |
+
from pathlib import Path
|
| 32 |
+
|
| 33 |
+
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
| 34 |
+
|
| 35 |
+
from models.detector import build_detector
|
| 36 |
+
from engine.video_detector import VideoFaceDetector
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def parse_args():
|
| 40 |
+
parser = argparse.ArgumentParser(description='Video Face Detection')
|
| 41 |
+
parser.add_argument('--model', type=str, default='scrfd_34g')
|
| 42 |
+
parser.add_argument('--checkpoint', type=str, required=True)
|
| 43 |
+
parser.add_argument('--input', type=str, required=True,
|
| 44 |
+
help='Video file, webcam index (0), or RTSP URL')
|
| 45 |
+
parser.add_argument('--output', type=str, default=None,
|
| 46 |
+
help='Output video path')
|
| 47 |
+
parser.add_argument('--input-size', type=int, default=640)
|
| 48 |
+
parser.add_argument('--score-thresh', type=float, default=0.4)
|
| 49 |
+
parser.add_argument('--nms-thresh', type=float, default=0.4)
|
| 50 |
+
parser.add_argument('--device', type=str, default='cuda')
|
| 51 |
+
parser.add_argument('--no-tracking', action='store_true')
|
| 52 |
+
parser.add_argument('--no-smoothing', action='store_true')
|
| 53 |
+
parser.add_argument('--keyframe-interval', type=int, default=0,
|
| 54 |
+
help='Run detector every N frames (0=every frame)')
|
| 55 |
+
parser.add_argument('--max-frames', type=int, default=-1)
|
| 56 |
+
parser.add_argument('--show', action='store_true',
|
| 57 |
+
help='Display video in window')
|
| 58 |
+
parser.add_argument('--onnx', type=str, default=None,
|
| 59 |
+
help='Use ONNX model instead of PyTorch')
|
| 60 |
+
return parser.parse_args()
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def main():
|
| 64 |
+
args = parse_args()
|
| 65 |
+
|
| 66 |
+
# Build model
|
| 67 |
+
if args.onnx:
|
| 68 |
+
model = None
|
| 69 |
+
model_path = args.onnx
|
| 70 |
+
else:
|
| 71 |
+
model = build_detector(
|
| 72 |
+
args.model,
|
| 73 |
+
score_threshold=args.score_thresh,
|
| 74 |
+
nms_threshold=args.nms_thresh,
|
| 75 |
+
)
|
| 76 |
+
model_path = args.checkpoint
|
| 77 |
+
|
| 78 |
+
# Build video detector
|
| 79 |
+
detector = VideoFaceDetector(
|
| 80 |
+
model=model,
|
| 81 |
+
model_path=model_path,
|
| 82 |
+
model_name=args.model,
|
| 83 |
+
device=args.device,
|
| 84 |
+
score_threshold=args.score_thresh,
|
| 85 |
+
nms_threshold=args.nms_thresh,
|
| 86 |
+
input_size=args.input_size,
|
| 87 |
+
use_tracking=not args.no_tracking,
|
| 88 |
+
use_smoothing=not args.no_smoothing,
|
| 89 |
+
keyframe_interval=args.keyframe_interval,
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
# Process video
|
| 93 |
+
source = int(args.input) if args.input.isdigit() else args.input
|
| 94 |
+
stats = detector.process_video(
|
| 95 |
+
source=source,
|
| 96 |
+
max_frames=args.max_frames,
|
| 97 |
+
output_path=args.output,
|
| 98 |
+
show=args.show,
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
print(f"\nProcessing complete:")
|
| 102 |
+
print(f" Frames: {stats['total_frames']}")
|
| 103 |
+
print(f" Average FPS: {stats['avg_fps']:.1f}")
|
| 104 |
+
print(f" Average faces/frame: {stats['avg_faces_per_frame']:.1f}")
|
| 105 |
+
if args.output:
|
| 106 |
+
print(f" Output saved to: {args.output}")
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
if __name__ == '__main__':
|
| 110 |
+
main()
|