2025-11-25 23:19:45 +09:00
|
|
|
# 106-point facial landmark detection
|
|
|
|
|
# Usage: python run_landmarks.py --image path/to/image.jpg
|
|
|
|
|
# python run_landmarks.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, Landmark106, 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
|
|
|
|
|
|
|
|
|
2025-11-30 20:32:07 +09:00
|
|
|
def process_image(detector, landmarker, image_path: str, save_dir: str = '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
|
|
|
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
|
|
|
|
|
|
|
|
|
|
for i, face in enumerate(faces):
|
2025-11-30 20:32:07 +09:00
|
|
|
bbox = face['bbox']
|
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
|
|
|
x1, y1, x2, y2 = map(int, bbox)
|
|
|
|
|
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
|
|
|
|
|
|
|
|
|
landmarks = landmarker.get_landmarks(image, bbox)
|
2025-11-30 20:32:07 +09:00
|
|
|
print(f' Face {i + 1}: {len(landmarks)} landmarks')
|
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 x, y in landmarks.astype(int):
|
|
|
|
|
cv2.circle(image, (x, y), 1, (0, 255, 0), -1)
|
|
|
|
|
|
2025-11-26 00:05:24 +09:00
|
|
|
cv2.putText(
|
|
|
|
|
image,
|
2025-11-30 20:32:07 +09:00
|
|
|
f'Face {i + 1}',
|
2025-11-26 00:05:24 +09:00
|
|
|
(x1, y1 - 10),
|
|
|
|
|
cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
|
|
0.5,
|
|
|
|
|
(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
|
|
|
|
|
|
|
|
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}_landmarks.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
|
|
|
|
|
|
|
|
|
|
|
|
|
def run_webcam(detector, landmarker):
|
2025-11-25 23:19:45 +09:00
|
|
|
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
|
|
|
for face in faces:
|
2025-11-30 20:32:07 +09:00
|
|
|
bbox = face['bbox']
|
2025-11-25 23:19:45 +09:00
|
|
|
x1, y1, x2, y2 = map(int, bbox)
|
|
|
|
|
cv2.rectangle(frame, (x1, y1), (x2, y2), (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
|
|
|
|
2025-11-25 23:19:45 +09:00
|
|
|
landmarks = landmarker.get_landmarks(frame, bbox) # 106 points
|
|
|
|
|
for x, y in landmarks.astype(int):
|
|
|
|
|
cv2.circle(frame, (x, y), 1, (0, 255, 0), -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-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('106-Point Landmarks', 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 facial landmark 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('--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
|
|
|
landmarker = Landmark106()
|
|
|
|
|
|
|
|
|
|
if args.webcam:
|
|
|
|
|
run_webcam(detector, landmarker)
|
|
|
|
|
else:
|
|
|
|
|
process_image(detector, landmarker, args.image, args.save_dir)
|
|
|
|
|
|
|
|
|
|
|
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()
|