-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeed_optimization.py
More file actions
177 lines (144 loc) Β· 5.99 KB
/
speed_optimization.py
File metadata and controls
177 lines (144 loc) Β· 5.99 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
# Speed Optimization for Face Recognition System
import cv2
import face_recognition
import numpy as np
import time
from face_recognition_system import LocalFaceRecognition
class OptimizedFaceRecognition(LocalFaceRecognition):
def __init__(self, model_type='small'): # Default to small for speed
super().__init__(model_type)
self.face_detection_model = "hog" # Faster than CNN
def extract_face_encoding_fast(self, image_path, max_size=800):
"""
Optimized face encoding extraction
"""
# Load and resize image for speed
image = face_recognition.load_image_file(image_path)
# Resize if image is too large
height, width = image.shape[:2]
if max(height, width) > max_size:
scale = max_size / max(height, width)
new_width = int(width * scale)
new_height = int(height * scale)
image = cv2.resize(image, (new_width, new_height))
# Use HOG model for speed (instead of CNN)
face_locations = face_recognition.face_locations(
image,
model="hog", # Much faster than CNN
number_of_times_to_upsample=1 # Reduce upsampling for speed
)
if not face_locations:
return None
# Extract face encodings with small model
face_encodings = face_recognition.face_encodings(
image,
face_locations,
model="small" # Faster model
)
if face_encodings:
return face_encodings[0]
return None
def recognize_face_fast(self, image_path, tolerance=0.4, max_size=800):
"""
Optimized face recognition
"""
unknown_encoding = self.extract_face_encoding_fast(image_path, max_size)
if unknown_encoding is None:
return None, 0.0
if not self.known_encodings:
return "Unknown", 0.0
# Use optimized distance calculation
distances = face_recognition.face_distance(self.known_encodings, unknown_encoding)
best_match_index = np.argmin(distances)
if distances[best_match_index] <= tolerance:
confidence = 1 - distances[best_match_index]
return self.known_names[best_match_index], confidence
else:
return "Unknown", 1 - distances[best_match_index]
def speed_comparison_test():
"""
Compare speed of original vs optimized system
"""
print("πββοΈ Speed Optimization Test")
print("=" * 40)
# Load your existing model
original_system = LocalFaceRecognition(model_type='large')
optimized_system = OptimizedFaceRecognition(model_type='small')
# Load training data into both systems
training_folder = "training_images"
print("Loading training data...")
for person_name in ["ElonMusk", "SamAltman"]: # Adjust if different
person_folder = f"{training_folder}/{person_name}"
if os.path.exists(person_folder):
image_paths = []
for img_file in os.listdir(person_folder):
if img_file.lower().endswith(('.jpg', '.jpeg', '.png')):
image_paths.append(f"{person_folder}/{img_file}")
if image_paths:
original_system.add_person(person_name, image_paths)
optimized_system.add_person(person_name, image_paths)
# Find a test image
test_image = None
test_folder = "test_images"
for person_folder in os.listdir(test_folder):
person_path = f"{test_folder}/{person_folder}"
if os.path.isdir(person_path):
for image_file in os.listdir(person_path):
if image_file.lower().endswith(('.jpg', '.jpeg', '.png')):
test_image = f"{person_path}/{image_file}"
break
if test_image:
break
if not test_image:
print("β No test image found")
return
print(f"Testing with: {test_image}")
print()
# Test original system
print("π Testing Original System (Large Model + CNN)...")
original_times = []
for i in range(3):
start_time = time.time()
name, confidence = original_system.recognize_face(test_image)
end_time = time.time()
duration = (end_time - start_time) * 1000
original_times.append(duration)
print(f" Run {i+1}: {duration:.0f}ms - {name} ({confidence:.3f})")
original_avg = np.mean(original_times)
print()
print("π Testing Optimized System (Small Model + HOG)...")
optimized_times = []
for i in range(3):
start_time = time.time()
name, confidence = optimized_system.recognize_face_fast(test_image)
end_time = time.time()
duration = (end_time - start_time) * 1000
optimized_times.append(duration)
print(f" Run {i+1}: {duration:.0f}ms - {name} ({confidence:.3f})")
optimized_avg = np.mean(optimized_times)
# Results
speedup = original_avg / optimized_avg
print()
print("π SPEED COMPARISON RESULTS:")
print("=" * 40)
print(f"Original System: {original_avg:.0f}ms average")
print(f"Optimized System: {optimized_avg:.0f}ms average")
print(f"Speed Improvement: {speedup:.1f}x faster")
print(f"Time Saved: {original_avg - optimized_avg:.0f}ms per image")
def create_production_system():
"""
Create an optimized production-ready system
"""
print("\nπ Creating Production-Ready System...")
# Use optimized settings
system = OptimizedFaceRecognition(model_type='small')
# Load your trained model if it exists
if os.path.exists("my_face_model.pkl"):
print("π¦ Loading existing model...")
return system
if __name__ == "__main__":
import os
# Run speed comparison
results = speed_comparison_test()
# Create optimized system
production_system = create_production_system()