-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_usage.py
More file actions
209 lines (158 loc) · 7.17 KB
/
example_usage.py
File metadata and controls
209 lines (158 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env python3
"""
Example usage script for the age_recognize package using PIL Image.
This script demonstrates how to use the age recognition API with PIL Images.
"""
import os
from PIL import Image
# Import the age_recognize package
from age_recognize import EnhancedAgeRecognizer
def example_basic_usage():
"""Example using the EnhancedAgeRecognizer class."""
print("=== Basic Usage Example ===")
# Load an image
image_path = "mock.jpg"
if os.path.exists(image_path):
# Using PIL Image with default models
img = Image.open(image_path)
recognizer = EnhancedAgeRecognizer()
results = recognizer.run_age_recognize(img)
print(f"Found {len(results)} people:")
for i, result in enumerate(results):
print(f" Person {i+1}: Age={result['age']:.1f}, "
f"Gender={result['gender']} ({result['gender_probability']:.2f})")
else:
print(f"Image not found: {image_path}")
def example_custom_model_paths():
"""Example using custom model paths."""
print("\n=== Custom Model Paths Example ===")
image_path = "mock.jpg"
if os.path.exists(image_path):
img = Image.open(image_path)
# Example 1: Custom YOLO model path only
print("Using custom YOLO model path:")
recognizer = EnhancedAgeRecognizer(yolo_weights="models/age/yolov8x_person_face.pt")
results = recognizer.run_age_recognize(img)
print(f" Found {len(results)} people with custom YOLO model")
# Example 2: Custom MiVOLO model path only
print("Using custom MiVOLO model path:")
recognizer = EnhancedAgeRecognizer(mivolo_model_path="./models/mivolo_v2_local")
results = recognizer.run_age_recognize(img)
print(f" Found {len(results)} people with custom MiVOLO model")
# Example 3: Both custom model paths
print("Using both custom model paths:")
recognizer = EnhancedAgeRecognizer(
yolo_weights="models/age/yolov8x_person_face.pt",
mivolo_model_path="iitolstykh/mivolo_v2"
)
results = recognizer.run_age_recognize(img)
print(f" Found {len(results)} people with both custom models")
else:
print(f"Image not found: {image_path}")
def example_advanced_usage():
"""Example using the EnhancedAgeRecognizer class directly."""
print("\n=== Advanced Usage Example ===")
# Initialize recognizer with custom settings and model paths
recognizer = EnhancedAgeRecognizer(
yolo_weights="models/age/yolov8x_person_face.pt",
mivolo_model_path="iitolstykh/mivolo_v2",
device="cuda", # or "cpu" if no GPU available
verbose=True # Enable detailed logging
)
image_path = "mock.jpg"
if os.path.exists(image_path):
img = Image.open(image_path)
# Get basic results
results = recognizer.run_age_recognize(img)
print(f"\nBasic results: {len(results)} people detected")
# Get detailed results with bounding boxes
detailed = recognizer.run_age_recognize_with_details(img)
print("\nDetailed results:")
print(f" Faces detected: {detailed['detection_info']['n_faces']}")
print(f" Persons detected: {detailed['detection_info']['n_persons']}")
print(f" Face crops extracted: {detailed['crop_info']['n_face_crops']}")
print(f" Body crops extracted: {detailed['crop_info']['n_body_crops']}")
# Print bounding boxes
if detailed['detection_info']['face_boxes']:
print(" Face bounding boxes (x1, y1, x2, y2):")
for i, box in enumerate(detailed['detection_info']['face_boxes']):
print(f" Face {i+1}: {box}")
if detailed['detection_info']['person_boxes']:
print(" Person bounding boxes (x1, y1, x2, y2):")
for i, box in enumerate(detailed['detection_info']['person_boxes']):
print(f" Person {i+1}: {box}")
else:
print(f"Image not found: {image_path}")
def example_local_model():
"""Example using local MiVOLO model path."""
print("\n=== Local Model Usage Example ===")
# Check if local model exists
local_model_path = "./models/mivolo_v2_local"
if not os.path.exists(local_model_path):
print(f"Local model not found at '{local_model_path}'")
print("To download the model locally, run:")
print(f" python cache.py --model iitolstykh/mivolo_v2 --save-path {local_model_path}")
print("Skipping local model example...")
return
# Initialize recognizer with local model path
from age_recognize.inference import TransformersInferenceEngine
try:
recognizer = EnhancedAgeRecognizer(
device="cuda",
verbose=True
)
# Override the inference engine with local model
recognizer.inference_engine = TransformersInferenceEngine(
model_path=local_model_path,
device="cuda"
)
image_path = "mock.jpg"
if os.path.exists(image_path):
img = Image.open(image_path)
results = recognizer.run_age_recognize(img)
print(f"Found {len(results)} people with local model:")
for i, result in enumerate(results):
print(f" Person {i+1}: Age={result['age']:.1f}, "
f"Gender={result['gender']} ({result['gender_probability']:.2f})")
else:
print(f"Image not found: {image_path}")
except Exception as e:
print(f"Error loading local model: {e}")
print("Make sure the model was downloaded correctly with cache.py")
def example_huggingface_model():
"""Example using Hugging Face model (default)."""
print("\n=== Hugging Face Model Usage Example ===")
# Initialize recognizer with HuggingFace hub model (default)
from age_recognize.inference import TransformersInferenceEngine
recognizer = EnhancedAgeRecognizer(
device="cuda",
verbose=True
)
# Override the inference engine with specific HF model
recognizer.inference_engine = TransformersInferenceEngine(
model_path="iitolstykh/mivolo_v2", # HuggingFace hub model
device="cuda"
)
image_path = "mock.jpg"
if os.path.exists(image_path):
img = Image.open(image_path)
results = recognizer.run_age_recognize(img)
print(f"Found {len(results)} people with HuggingFace model:")
for i, result in enumerate(results):
print(f" Person {i+1}: Age={result['age']:.1f}, "
f"Gender={result['gender']} ({result['gender_probability']:.2f})")
else:
print(f"Image not found: {image_path}")
def main():
"""Run all examples."""
print("Age Recognition API Usage Examples - PIL Image")
print("=" * 50)
example_basic_usage()
example_custom_model_paths()
example_advanced_usage()
example_local_model()
example_huggingface_model()
print("\n" + "=" * 50)
print("Examples completed!")
if __name__ == "__main__":
main()