2025-11-25 23:19:45 +09:00
|
|
|
# Face detection on video files
|
|
|
|
|
# Usage: python run_video_detection.py --input video.mp4 --output output.mp4
|
ref: Add comprehensive test suite and enhance model functionality
- Add new test files for age_gender, factory, landmark, recognition, scrfd, and utils
- Add new scripts for age_gender, landmarks, and video detection
- Update documentation in README.md, MODELS.md, QUICKSTART.md
- Improve model constants and face utilities
- Update detection models (retinaface, scrfd) with enhanced functionality
- Update project configuration in pyproject.toml
2025-11-15 21:09:37 +09:00
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
|
from pathlib import Path
|
2025-11-25 23:19:45 +09:00
|
|
|
|
|
|
|
|
import cv2
|
ref: Add comprehensive test suite and enhance model functionality
- Add new test files for age_gender, factory, landmark, recognition, scrfd, and utils
- Add new scripts for age_gender, landmarks, and video detection
- Update documentation in README.md, MODELS.md, QUICKSTART.md
- Improve model constants and face utilities
- Update detection models (retinaface, scrfd) with enhanced functionality
- Update project configuration in pyproject.toml
2025-11-15 21:09:37 +09:00
|
|
|
from tqdm import tqdm
|
|
|
|
|
|
2025-11-25 23:19:45 +09:00
|
|
|
from uniface import SCRFD, RetinaFace
|
ref: Add comprehensive test suite and enhance model functionality
- Add new test files for age_gender, factory, landmark, recognition, scrfd, and utils
- Add new scripts for age_gender, landmarks, and video detection
- Update documentation in README.md, MODELS.md, QUICKSTART.md
- Improve model constants and face utilities
- Update detection models (retinaface, scrfd) with enhanced functionality
- Update project configuration in pyproject.toml
2025-11-15 21:09:37 +09:00
|
|
|
from uniface.visualization import draw_detections
|
|
|
|
|
|
|
|
|
|
|
2025-11-26 00:05:24 +09:00
|
|
|
def process_video(
|
|
|
|
|
detector,
|
|
|
|
|
input_path: str,
|
|
|
|
|
output_path: str,
|
|
|
|
|
threshold: float = 0.6,
|
|
|
|
|
show_preview: bool = False,
|
|
|
|
|
):
|
ref: Add comprehensive test suite and enhance model functionality
- Add new test files for age_gender, factory, landmark, recognition, scrfd, and utils
- Add new scripts for age_gender, landmarks, and video detection
- Update documentation in README.md, MODELS.md, QUICKSTART.md
- Improve model constants and face utilities
- Update detection models (retinaface, scrfd) with enhanced functionality
- Update project configuration in pyproject.toml
2025-11-15 21:09:37 +09:00
|
|
|
cap = cv2.VideoCapture(input_path)
|
|
|
|
|
if not cap.isOpened():
|
|
|
|
|
print(f"Error: Cannot open video file '{input_path}'")
|
|
|
|
|
return
|
|
|
|
|
|
2025-11-25 23:19:45 +09:00
|
|
|
# get video properties
|
ref: Add comprehensive test suite and enhance model functionality
- Add new test files for age_gender, factory, landmark, recognition, scrfd, and utils
- Add new scripts for age_gender, landmarks, and video detection
- Update documentation in README.md, MODELS.md, QUICKSTART.md
- Improve model constants and face utilities
- Update detection models (retinaface, scrfd) with enhanced functionality
- Update project configuration in pyproject.toml
2025-11-15 21:09:37 +09:00
|
|
|
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
2025-11-25 23:19:45 +09:00
|
|
|
fps = cap.get(cv2.CAP_PROP_FPS)
|
ref: Add comprehensive test suite and enhance model functionality
- Add new test files for age_gender, factory, landmark, recognition, scrfd, and utils
- Add new scripts for age_gender, landmarks, and video detection
- Update documentation in README.md, MODELS.md, QUICKSTART.md
- Improve model constants and face utilities
- Update detection models (retinaface, scrfd) with enhanced functionality
- Update project configuration in pyproject.toml
2025-11-15 21:09:37 +09:00
|
|
|
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
|
|
|
|
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
|
|
|
|
|
2025-11-30 20:32:07 +09:00
|
|
|
print(f'Input: {input_path} ({width}x{height}, {fps:.1f} fps, {total_frames} frames)')
|
|
|
|
|
print(f'Output: {output_path}')
|
ref: Add comprehensive test suite and enhance model functionality
- Add new test files for age_gender, factory, landmark, recognition, scrfd, and utils
- Add new scripts for age_gender, landmarks, and video detection
- Update documentation in README.md, MODELS.md, QUICKSTART.md
- Improve model constants and face utilities
- Update detection models (retinaface, scrfd) with enhanced functionality
- Update project configuration in pyproject.toml
2025-11-15 21:09:37 +09:00
|
|
|
|
2025-11-30 20:32:07 +09:00
|
|
|
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # codec for .mp4
|
2025-11-25 23:19:45 +09:00
|
|
|
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
ref: Add comprehensive test suite and enhance model functionality
- Add new test files for age_gender, factory, landmark, recognition, scrfd, and utils
- Add new scripts for age_gender, landmarks, and video detection
- Update documentation in README.md, MODELS.md, QUICKSTART.md
- Improve model constants and face utilities
- Update detection models (retinaface, scrfd) with enhanced functionality
- Update project configuration in pyproject.toml
2025-11-15 21:09:37 +09:00
|
|
|
|
|
|
|
|
if not out.isOpened():
|
|
|
|
|
print(f"Error: Cannot create output video '{output_path}'")
|
|
|
|
|
cap.release()
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
frame_count = 0
|
|
|
|
|
total_faces = 0
|
|
|
|
|
|
2025-11-30 20:32:07 +09:00
|
|
|
for _ in tqdm(range(total_frames), desc='Processing', unit='frames'):
|
2025-11-25 23:19:45 +09:00
|
|
|
ret, frame = cap.read()
|
|
|
|
|
if not ret:
|
|
|
|
|
break
|
ref: Add comprehensive test suite and enhance model functionality
- Add new test files for age_gender, factory, landmark, recognition, scrfd, and utils
- Add new scripts for age_gender, landmarks, and video detection
- Update documentation in README.md, MODELS.md, QUICKSTART.md
- Improve model constants and face utilities
- Update detection models (retinaface, scrfd) with enhanced functionality
- Update project configuration in pyproject.toml
2025-11-15 21:09:37 +09:00
|
|
|
|
2025-11-25 23:19:45 +09:00
|
|
|
frame_count += 1
|
|
|
|
|
faces = detector.detect(frame)
|
|
|
|
|
total_faces += len(faces)
|
ref: Add comprehensive test suite and enhance model functionality
- Add new test files for age_gender, factory, landmark, recognition, scrfd, and utils
- Add new scripts for age_gender, landmarks, and video detection
- Update documentation in README.md, MODELS.md, QUICKSTART.md
- Improve model constants and face utilities
- Update detection models (retinaface, scrfd) with enhanced functionality
- Update project configuration in pyproject.toml
2025-11-15 21:09:37 +09:00
|
|
|
|
2025-11-30 20:32:07 +09:00
|
|
|
bboxes = [f['bbox'] for f in faces]
|
|
|
|
|
scores = [f['confidence'] for f in faces]
|
|
|
|
|
landmarks = [f['landmarks'] for f in faces]
|
2025-12-11 01:02:18 +09:00
|
|
|
draw_detections(
|
|
|
|
|
image=frame, bboxes=bboxes, scores=scores, landmarks=landmarks, vis_threshold=threshold, fancy_bbox=True
|
|
|
|
|
)
|
ref: Add comprehensive test suite and enhance model functionality
- Add new test files for age_gender, factory, landmark, recognition, scrfd, and utils
- Add new scripts for age_gender, landmarks, and video detection
- Update documentation in README.md, MODELS.md, QUICKSTART.md
- Improve model constants and face utilities
- Update detection models (retinaface, scrfd) with enhanced functionality
- Update project configuration in pyproject.toml
2025-11-15 21:09:37 +09:00
|
|
|
|
2025-11-26 00:05:24 +09:00
|
|
|
cv2.putText(
|
|
|
|
|
frame,
|
2025-11-30 20:32:07 +09:00
|
|
|
f'Faces: {len(faces)}',
|
2025-11-26 00:05:24 +09:00
|
|
|
(10, 30),
|
|
|
|
|
cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
|
|
1,
|
|
|
|
|
(0, 255, 0),
|
|
|
|
|
2,
|
|
|
|
|
)
|
2025-11-25 23:19:45 +09:00
|
|
|
out.write(frame)
|
ref: Add comprehensive test suite and enhance model functionality
- Add new test files for age_gender, factory, landmark, recognition, scrfd, and utils
- Add new scripts for age_gender, landmarks, and video detection
- Update documentation in README.md, MODELS.md, QUICKSTART.md
- Improve model constants and face utilities
- Update detection models (retinaface, scrfd) with enhanced functionality
- Update project configuration in pyproject.toml
2025-11-15 21:09:37 +09:00
|
|
|
|
|
|
|
|
if show_preview:
|
2025-11-25 23:19:45 +09:00
|
|
|
cv2.imshow("Processing - Press 'q' to cancel", frame)
|
2025-11-30 20:32:07 +09:00
|
|
|
if cv2.waitKey(1) & 0xFF == ord('q'):
|
|
|
|
|
print('\nCancelled by user')
|
2025-11-25 23:19:45 +09:00
|
|
|
break
|
ref: Add comprehensive test suite and enhance model functionality
- Add new test files for age_gender, factory, landmark, recognition, scrfd, and utils
- Add new scripts for age_gender, landmarks, and video detection
- Update documentation in README.md, MODELS.md, QUICKSTART.md
- Improve model constants and face utilities
- Update detection models (retinaface, scrfd) with enhanced functionality
- Update project configuration in pyproject.toml
2025-11-15 21:09:37 +09:00
|
|
|
|
2025-11-25 23:19:45 +09:00
|
|
|
cap.release()
|
|
|
|
|
out.release()
|
|
|
|
|
if show_preview:
|
|
|
|
|
cv2.destroyAllWindows()
|
|
|
|
|
|
|
|
|
|
avg_faces = total_faces / frame_count if frame_count > 0 else 0
|
2025-11-30 20:32:07 +09:00
|
|
|
print(f'\nDone! {frame_count} frames, {total_faces} faces ({avg_faces:.1f} avg/frame)')
|
|
|
|
|
print(f'Saved: {output_path}')
|
ref: Add comprehensive test suite and enhance model functionality
- Add new test files for age_gender, factory, landmark, recognition, scrfd, and utils
- Add new scripts for age_gender, landmarks, and video detection
- Update documentation in README.md, MODELS.md, QUICKSTART.md
- Improve model constants and face utilities
- Update detection models (retinaface, scrfd) with enhanced functionality
- Update project configuration in pyproject.toml
2025-11-15 21:09:37 +09:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2025-11-30 20:32:07 +09:00
|
|
|
parser = argparse.ArgumentParser(description='Process video with face detection')
|
|
|
|
|
parser.add_argument('--input', type=str, required=True, help='Input video path')
|
|
|
|
|
parser.add_argument('--output', type=str, required=True, help='Output video path')
|
|
|
|
|
parser.add_argument('--detector', type=str, default='retinaface', choices=['retinaface', 'scrfd'])
|
|
|
|
|
parser.add_argument('--threshold', type=float, default=0.6, help='Visualization threshold')
|
|
|
|
|
parser.add_argument('--preview', action='store_true', help='Show live preview')
|
ref: Add comprehensive test suite and enhance model functionality
- Add new test files for age_gender, factory, landmark, recognition, scrfd, and utils
- Add new scripts for age_gender, landmarks, and video detection
- Update documentation in README.md, MODELS.md, QUICKSTART.md
- Improve model constants and face utilities
- Update detection models (retinaface, scrfd) with enhanced functionality
- Update project configuration in pyproject.toml
2025-11-15 21:09:37 +09:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
if not Path(args.input).exists():
|
|
|
|
|
print(f"Error: Input file '{args.input}' does not exist")
|
|
|
|
|
return
|
|
|
|
|
|
2025-11-25 23:19:45 +09:00
|
|
|
Path(args.output).parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
2025-11-30 20:32:07 +09:00
|
|
|
detector = RetinaFace() if args.detector == 'retinaface' else SCRFD()
|
2025-11-25 23:19:45 +09:00
|
|
|
process_video(detector, args.input, args.output, args.threshold, args.preview)
|
ref: Add comprehensive test suite and enhance model functionality
- Add new test files for age_gender, factory, landmark, recognition, scrfd, and utils
- Add new scripts for age_gender, landmarks, and video detection
- Update documentation in README.md, MODELS.md, QUICKSTART.md
- Improve model constants and face utilities
- Update detection models (retinaface, scrfd) with enhanced functionality
- Update project configuration in pyproject.toml
2025-11-15 21:09:37 +09:00
|
|
|
|
|
|
|
|
|
2025-11-30 20:32:07 +09:00
|
|
|
if __name__ == '__main__':
|
ref: Add comprehensive test suite and enhance model functionality
- Add new test files for age_gender, factory, landmark, recognition, scrfd, and utils
- Add new scripts for age_gender, landmarks, and video detection
- Update documentation in README.md, MODELS.md, QUICKSTART.md
- Improve model constants and face utilities
- Update detection models (retinaface, scrfd) with enhanced functionality
- Update project configuration in pyproject.toml
2025-11-15 21:09:37 +09:00
|
|
|
main()
|