2025-11-25 23:19:45 +09:00
|
|
|
# Batch face detection on a folder of images
|
|
|
|
|
# Usage: python batch_process.py --input images/ --output results/
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_image_files(input_dir: Path, extensions: tuple) -> list:
|
2025-11-25 23:19:45 +09:00
|
|
|
files = []
|
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
|
|
|
for ext in extensions:
|
2025-11-25 23:19:45 +09:00
|
|
|
files.extend(input_dir.glob(f"*.{ext}"))
|
|
|
|
|
files.extend(input_dir.glob(f"*.{ext.upper()}"))
|
|
|
|
|
return sorted(files)
|
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
|
|
|
def process_image(detector, image_path: Path, output_path: Path, threshold: float) -> int:
|
|
|
|
|
"""Process single image. Returns face count or -1 on error."""
|
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
|
|
|
image = cv2.imread(str(image_path))
|
|
|
|
|
if image is None:
|
2025-11-25 23:19:45 +09:00
|
|
|
return -1
|
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
|
|
|
faces = detector.detect(image)
|
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
|
|
|
# unpack face data for visualization
|
|
|
|
|
bboxes = [f["bbox"] for f in faces]
|
|
|
|
|
scores = [f["confidence"] for f in faces]
|
|
|
|
|
landmarks = [f["landmarks"] for f in faces]
|
|
|
|
|
draw_detections(image, bboxes, scores, landmarks, vis_threshold=threshold)
|
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(
|
|
|
|
|
image,
|
|
|
|
|
f"Faces: {len(faces)}",
|
|
|
|
|
(10, 30),
|
|
|
|
|
cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
|
|
1,
|
|
|
|
|
(0, 255, 0),
|
|
|
|
|
2,
|
|
|
|
|
)
|
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
|
|
|
cv2.imwrite(str(output_path), image)
|
|
|
|
|
|
2025-11-25 23:19:45 +09:00
|
|
|
return len(faces)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
parser = argparse.ArgumentParser(description="Batch process images with face detection")
|
|
|
|
|
parser.add_argument("--input", type=str, required=True, help="Input directory")
|
|
|
|
|
parser.add_argument("--output", type=str, required=True, help="Output directory")
|
|
|
|
|
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("--extensions", type=str, default="jpg,jpeg,png,bmp", help="Image extensions")
|
|
|
|
|
args = parser.parse_args()
|
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
|
|
|
input_path = Path(args.input)
|
|
|
|
|
output_path = Path(args.output)
|
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
|
|
|
if not input_path.exists():
|
|
|
|
|
print(f"Error: Input directory '{args.input}' does not exist")
|
|
|
|
|
return
|
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
|
|
|
|
|
|
|
|
output_path.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
2025-11-25 23:19:45 +09:00
|
|
|
extensions = tuple(ext.strip() for ext in args.extensions.split(","))
|
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
|
|
|
image_files = get_image_files(input_path, extensions)
|
|
|
|
|
|
|
|
|
|
if not image_files:
|
2025-11-25 23:19:45 +09:00
|
|
|
print(f"No images found with extensions {extensions}")
|
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
|
|
|
return
|
|
|
|
|
|
2025-11-25 23:19:45 +09:00
|
|
|
print(f"Found {len(image_files)} images")
|
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
|
|
|
detector = RetinaFace() if args.detector == "retinaface" else SCRFD()
|
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
|
|
|
success, errors, total_faces = 0, 0, 0
|
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
|
|
|
for img_path in tqdm(image_files, desc="Processing", unit="img"):
|
|
|
|
|
out_path = output_path / f"{img_path.stem}_detected{img_path.suffix}"
|
|
|
|
|
result = process_image(detector, img_path, out_path, args.threshold)
|
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
|
|
|
if result >= 0:
|
|
|
|
|
success += 1
|
|
|
|
|
total_faces += result
|
|
|
|
|
else:
|
|
|
|
|
errors += 1
|
|
|
|
|
print(f"\nFailed: {img_path.name}")
|
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
|
|
|
print(f"\nDone! {success} processed, {errors} errors, {total_faces} faces total")
|
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 __name__ == "__main__":
|
|
|
|
|
main()
|