2025-11-25 23:19:45 +09:00
|
|
|
# Age and gender prediction on detected faces
|
|
|
|
|
# Usage: python run_age_gender.py --image path/to/image.jpg
|
|
|
|
|
# python run_age_gender.py --webcam
|
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
|
2025-11-25 23:19:45 +09:00
|
|
|
import os
|
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 pathlib import Path
|
|
|
|
|
|
2025-11-25 23:19:45 +09:00
|
|
|
import cv2
|
|
|
|
|
|
|
|
|
|
from uniface import SCRFD, AgeGender, 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-30 20:32:07 +09:00
|
|
|
def draw_age_gender_label(image, bbox, gender_id: int, age: int):
|
2025-11-25 23:19:45 +09:00
|
|
|
"""Draw age/gender label above the bounding box."""
|
|
|
|
|
x1, y1 = int(bbox[0]), int(bbox[1])
|
2025-11-30 20:32:07 +09:00
|
|
|
gender_str = 'Female' if gender_id == 0 else 'Male'
|
|
|
|
|
text = f'{gender_str}, {age}y'
|
2025-11-25 23:19:45 +09:00
|
|
|
(tw, th), _ = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 2)
|
|
|
|
|
cv2.rectangle(image, (x1, y1 - th - 10), (x1 + tw + 10, y1), (0, 255, 0), -1)
|
|
|
|
|
cv2.putText(image, text, (x1 + 5, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 0), 2)
|
|
|
|
|
|
|
|
|
|
|
2025-11-26 00:05:24 +09:00
|
|
|
def process_image(
|
|
|
|
|
detector,
|
|
|
|
|
age_gender,
|
|
|
|
|
image_path: str,
|
2025-11-30 20:32:07 +09:00
|
|
|
save_dir: str = 'outputs',
|
2025-11-26 00:05:24 +09:00
|
|
|
threshold: float = 0.6,
|
|
|
|
|
):
|
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(image_path)
|
|
|
|
|
if image is None:
|
|
|
|
|
print(f"Error: Failed to load image from '{image_path}'")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
faces = detector.detect(image)
|
2025-11-30 20:32:07 +09:00
|
|
|
print(f'Detected {len(faces)} face(s)')
|
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 faces:
|
|
|
|
|
return
|
|
|
|
|
|
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=image, 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
|
|
|
|
|
|
|
|
for i, face in enumerate(faces):
|
2025-11-30 20:32:07 +09:00
|
|
|
gender_id, age = age_gender.predict(image, face['bbox'])
|
|
|
|
|
gender_str = 'Female' if gender_id == 0 else 'Male'
|
|
|
|
|
print(f' Face {i + 1}: {gender_str}, {age} years old')
|
|
|
|
|
draw_age_gender_label(image, face['bbox'], gender_id, age)
|
2025-11-25 23:19:45 +09:00
|
|
|
|
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
|
|
|
os.makedirs(save_dir, exist_ok=True)
|
2025-11-30 20:32:07 +09:00
|
|
|
output_path = os.path.join(save_dir, f'{Path(image_path).stem}_age_gender.jpg')
|
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(output_path, image)
|
2025-11-30 20:32:07 +09:00
|
|
|
print(f'Output 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
|
|
|
|
|
|
|
|
|
2025-11-25 23:19:45 +09:00
|
|
|
def run_webcam(detector, age_gender, threshold: float = 0.6):
|
|
|
|
|
cap = cv2.VideoCapture(0) # 0 = default webcam
|
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 cap.isOpened():
|
2025-11-30 20:32:07 +09:00
|
|
|
print('Cannot open webcam')
|
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("Press 'q' to quit")
|
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
|
|
|
while True:
|
|
|
|
|
ret, frame = cap.read()
|
|
|
|
|
frame = cv2.flip(frame, 1) # mirror for natural interaction
|
|
|
|
|
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
|
|
|
faces = detector.detect(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
|
|
|
|
2025-11-25 23:19:45 +09:00
|
|
|
# unpack face data for visualization
|
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-14 14:07:46 +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-25 23:19:45 +09:00
|
|
|
for face in faces:
|
2025-11-30 20:32:07 +09:00
|
|
|
gender_id, age = age_gender.predict(frame, face['bbox']) # predict per face
|
|
|
|
|
draw_age_gender_label(frame, face['bbox'], gender_id, age)
|
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-30 20:32:07 +09:00
|
|
|
cv2.imshow('Age & Gender Detection', 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
|
|
|
|
2025-11-30 20:32:07 +09:00
|
|
|
if cv2.waitKey(1) & 0xFF == ord('q'):
|
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()
|
|
|
|
|
cv2.destroyAllWindows()
|
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='Run age and gender detection')
|
|
|
|
|
parser.add_argument('--image', type=str, help='Path to input image')
|
|
|
|
|
parser.add_argument('--webcam', action='store_true', help='Use webcam')
|
|
|
|
|
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('--save_dir', type=str, default='outputs')
|
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 args.image and not args.webcam:
|
2025-11-30 20:32:07 +09:00
|
|
|
parser.error('Either --image or --webcam must be specified')
|
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
|
|
|
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
|
|
|
age_gender = AgeGender()
|
|
|
|
|
|
|
|
|
|
if args.webcam:
|
|
|
|
|
run_webcam(detector, age_gender, args.threshold)
|
|
|
|
|
else:
|
|
|
|
|
process_image(detector, age_gender, args.image, args.save_dir, args.threshold)
|
|
|
|
|
|
|
|
|
|
|
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()
|