|
1 | | -""" |
2 | | -Basic example showing how to use a single face detector. |
3 | | -""" |
4 | | - |
5 | | -import argparse |
6 | 1 | import os |
7 | | -import shutil |
8 | 2 |
|
9 | 3 | import cv2 |
10 | 4 |
|
11 | 5 | from mukh.detection import FaceDetector |
12 | 6 |
|
13 | | -# Set up argument parser |
14 | | -parser = argparse.ArgumentParser(description="Face Detection Example") |
15 | | -parser.add_argument( |
16 | | - "--detection_model", |
17 | | - type=str, |
18 | | - choices=["blazeface", "mediapipe", "ultralight"], |
19 | | - default="mediapipe", |
20 | | - help="Choose the face detection model to use.", |
21 | | -) |
22 | | -parser.add_argument( |
23 | | - "--clear_output", |
24 | | - type=bool, |
25 | | - default=True, |
26 | | - help="Clear the output folder before saving new images.", |
| 7 | +# Initialize detector |
| 8 | +detection_model = ( |
| 9 | + "blazeface" # Available models: "blazeface", "mediapipe", "ultralight" |
27 | 10 | ) |
28 | | -args = parser.parse_args() |
29 | | - |
30 | | -# Clear output folder if specified |
31 | | -if args.clear_output and os.path.exists("output_images"): |
32 | | - shutil.rmtree("output_images") |
33 | | -os.makedirs("output_images", exist_ok=True) |
34 | | - |
35 | | -# Create detector |
36 | | -detector = FaceDetector.create(args.detection_model) |
| 11 | +detector = FaceDetector.create(detection_model) |
37 | 12 |
|
38 | | -# To view available models |
39 | | -# print(detector.list_available_models()) |
| 13 | +# Detect faces |
| 14 | +image_path = "demo_images/1.jpg" |
| 15 | +faces, annotated_image = detector.detect_with_landmarks(image_path) |
40 | 16 |
|
41 | | -# Process all images in the demo_images folder |
42 | | -demo_images_folder = "demo_images" |
43 | | -for image_name in os.listdir(demo_images_folder): |
44 | | - if image_name.endswith((".jpg", ".png")): |
45 | | - # Get image path |
46 | | - image_path = os.path.join(demo_images_folder, image_name) |
47 | | - |
48 | | - # Detect faces |
49 | | - faces, annotated_image = detector.detect_with_landmarks(image_path) |
50 | | - |
51 | | - # Save output |
52 | | - output_image_path = os.path.join( |
53 | | - "output_images", f"{args.detection_model}_output_{image_name}" |
54 | | - ) |
55 | | - cv2.imwrite(output_image_path, annotated_image) |
56 | | - |
57 | | - # Print results |
58 | | - print(f"Found {len(faces)} faces in {image_name}") |
59 | | - for face in faces: |
60 | | - print(f"Face confidence: {face.bbox.confidence:.2f}") |
| 17 | +# Save output |
| 18 | +os.makedirs("output_images", exist_ok=True) |
| 19 | +output_path = f"output_images/1.jpg" |
| 20 | +cv2.imwrite(output_path, annotated_image) |
0 commit comments