Skip to content

Commit 87c70c0

Browse files
🔨 [examples/detection] Add simpler examples
1 parent c8946d3 commit 87c70c0

File tree

2 files changed

+68
-51
lines changed

2 files changed

+68
-51
lines changed
Lines changed: 11 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,20 @@
1-
"""
2-
Basic example showing how to use a single face detector.
3-
"""
4-
5-
import argparse
61
import os
7-
import shutil
82

93
import cv2
104

115
from mukh.detection import FaceDetector
126

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"
2710
)
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)
3712

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)
4016

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)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
Basic example showing how to use a single face detector.
3+
"""
4+
5+
import argparse
6+
import os
7+
import shutil
8+
9+
import cv2
10+
11+
from mukh.detection import FaceDetector
12+
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=False,
26+
help="Clear the output folder before saving new images.",
27+
)
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)
37+
38+
# Process all images in the demo_images folder
39+
demo_images_folder = "demo_images"
40+
for image_name in os.listdir(demo_images_folder):
41+
if image_name.endswith((".jpg", ".png")):
42+
# Get image path
43+
image_path = os.path.join(demo_images_folder, image_name)
44+
45+
# Detect faces
46+
faces, annotated_image = detector.detect_with_landmarks(image_path)
47+
48+
# Save output
49+
output_image_path = os.path.join(
50+
"output_images", f"{args.detection_model}_output_{image_name}"
51+
)
52+
cv2.imwrite(output_image_path, annotated_image)
53+
54+
# Print results
55+
print(f"Found {len(faces)} faces in {image_name}")
56+
for face in faces:
57+
print(f"Face confidence: {face.bbox.confidence:.2f}")

0 commit comments

Comments
 (0)