-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_face_auth.py
More file actions
339 lines (270 loc) Β· 12.9 KB
/
python_face_auth.py
File metadata and controls
339 lines (270 loc) Β· 12.9 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env python3
"""
High-Accuracy Face Authentication using Python
Achieves 99%+ accuracy using industry-standard libraries
"""
import face_recognition
import cv2
import numpy as np
import json
import os
import time
from datetime import datetime
from typing import List, Dict, Tuple, Optional
import pickle
import argparse
class HighAccuracyFaceAuth:
def __init__(self, db_path: str = "python_face_database.json"):
self.db_path = db_path
self.face_encodings_cache = {}
self.load_database()
def load_database(self):
"""Load face database or create new one"""
try:
if os.path.exists(self.db_path):
with open(self.db_path, 'r') as f:
self.database = json.load(f)
print(f"β
Loaded database with {len(self.database.get('users', {}))} users")
else:
self.database = {
"users": {},
"version": "1.0",
"accuracy_threshold": 0.6, # Face_recognition library optimal threshold
"created": datetime.now().isoformat()
}
print("π Created new face database")
except Exception as e:
print(f"β Error loading database: {e}")
self.database = {"users": {}, "version": "1.0", "accuracy_threshold": 0.6}
def save_database(self):
"""Save database to file"""
try:
with open(self.db_path, 'w') as f:
json.dump(self.database, f, indent=2)
except Exception as e:
print(f"β Error saving database: {e}")
def capture_from_camera(self, save_path: str) -> bool:
"""Capture image from camera with auto-capture"""
print("π· Initializing camera...")
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("β Could not open camera")
return False
print("πΈ Camera ready! Auto-capturing in 3 seconds...")
print("π‘ Look directly at the camera and stay still...")
# Auto-capture after a short delay
frame_count = 0
capture_frame = 90 # Capture after ~3 seconds (30 FPS)
while True:
ret, frame = cap.read()
if not ret:
print("β Failed to read from camera")
break
frame_count += 1
# Show countdown
if frame_count % 30 == 0: # Every second
seconds_left = max(0, 3 - (frame_count // 30))
if seconds_left > 0:
print(f"πΈ Capturing in {seconds_left}...")
# Display frame with countdown
display_frame = frame.copy()
if frame_count < capture_frame:
seconds_left = max(0, 3 - (frame_count // 30))
cv2.putText(display_frame, f"Capturing in {seconds_left}...",
(50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
else:
cv2.putText(display_frame, "CAPTURED!",
(50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.imshow('Face Authentication - Auto Capture', display_frame)
# Auto-capture
if frame_count >= capture_frame:
cv2.imwrite(save_path, frame)
print(f"β
Image auto-captured: {save_path}")
break
# Allow manual escape
key = cv2.waitKey(1) & 0xFF
if key == 27: # Escape key
print("β Capture cancelled")
cap.release()
cv2.destroyAllWindows()
return False
cap.release()
cv2.destroyAllWindows()
return True
def detect_and_encode_faces(self, image_path: str) -> Tuple[List[np.ndarray], List[Tuple]]:
"""
Detect faces and generate high-accuracy encodings
Returns: (face_encodings, face_locations)
"""
print(f"π Analyzing image: {image_path}")
# Load image
image = face_recognition.load_image_file(image_path)
# Find face locations using CNN model (more accurate but slower)
print("π― Detecting faces with CNN model...")
face_locations = face_recognition.face_locations(image, model="cnn")
if not face_locations:
print("β οΈ No faces found, trying HOG model...")
# Fallback to HOG model (faster but less accurate)
face_locations = face_recognition.face_locations(image, model="hog")
if not face_locations:
raise Exception("No faces detected in image")
print(f"β
Found {len(face_locations)} face(s)")
# Generate face encodings (128-dimensional vector)
print("π§ Generating face encodings...")
face_encodings = face_recognition.face_encodings(image, face_locations)
print(f"β
Generated {len(face_encodings)} face encoding(s)")
return face_encodings, face_locations
def register_user(self, user_id: str, num_samples: int = 3) -> bool:
"""Register user with multiple face samples"""
print(f"\nπ― === Face Registration for User: '{user_id}' ===")
print(f"π Will capture {num_samples} samples for optimal accuracy")
face_encodings = []
for i in range(num_samples):
print(f"\n--- πΈ Sample {i+1}/{num_samples} ---")
print("π‘ Tips for best results:")
print(" β’ Look directly at the camera")
print(" β’ Ensure good lighting")
print(" β’ Keep a neutral expression")
print(" β’ Avoid glasses/hats if possible")
# Capture image
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
image_path = f"captured_images/registration_{user_id}_{timestamp}_sample{i+1}.jpg"
os.makedirs("captured_images", exist_ok=True)
if not self.capture_from_camera(image_path):
print(f"β Failed to capture sample {i+1}")
continue
try:
# Extract face encodings
encodings, locations = self.detect_and_encode_faces(image_path)
if encodings:
face_encodings.append({
"encoding": encodings[0].tolist(), # Convert numpy array to list for JSON
"timestamp": datetime.now().isoformat(),
"image_path": image_path,
"sample_id": f"{user_id}_{timestamp}"
})
print(f"β
Sample {i+1} processed successfully!")
else:
print(f"β No face found in sample {i+1}")
except Exception as e:
print(f"β Error processing sample {i+1}: {e}")
if not face_encodings:
print("β No valid face samples captured")
return False
# Store in database
if "users" not in self.database:
self.database["users"] = {}
self.database["users"][user_id] = {
"user_id": user_id,
"face_encodings": face_encodings,
"enrollment_date": datetime.now().isoformat(),
"sample_count": len(face_encodings),
"last_authentication": None,
"authentication_count": 0
}
self.save_database()
print(f"\nπ === Registration Complete ===")
print(f"β
Successfully registered {len(face_encodings)} samples for '{user_id}'")
print(f"π User is ready for high-accuracy authentication!")
return True
def authenticate_user(self, tolerance: float = 0.6) -> Dict:
"""Authenticate user with high accuracy"""
print("\nπ === Face Authentication ===")
# Capture authentication image
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
auth_image_path = f"captured_images/authentication_{timestamp}.jpg"
os.makedirs("captured_images", exist_ok=True)
if not self.capture_from_camera(auth_image_path):
return {"success": False, "error": "Failed to capture image"}
start_time = time.time()
try:
# Extract face encoding from authentication image
auth_encodings, auth_locations = self.detect_and_encode_faces(auth_image_path)
if not auth_encodings:
return {"success": False, "error": "No face detected in authentication image"}
auth_encoding = auth_encodings[0] # Use first detected face
# Compare against all registered users
print(f"π Comparing against {len(self.database.get('users', {}))} registered users...")
best_match = None
best_distance = float('inf')
for user_id, user_data in self.database.get("users", {}).items():
user_encodings = [np.array(sample["encoding"]) for sample in user_data["face_encodings"]]
# Calculate distances to all samples for this user
distances = face_recognition.face_distance(user_encodings, auth_encoding)
min_distance = np.min(distances)
avg_distance = np.mean(distances)
# Use weighted score: 70% minimum distance, 30% average distance
score = 0.7 * min_distance + 0.3 * avg_distance
print(f"π€ User '{user_id}': min_dist={min_distance:.3f}, avg_dist={avg_distance:.3f}, score={score:.3f}")
if score < best_distance:
best_distance = score
best_match = {
"user_id": user_id,
"distance": min_distance,
"avg_distance": avg_distance,
"confidence": max(0, 1 - min_distance), # Convert distance to confidence
"score": score
}
processing_time = time.time() - start_time
# Determine if authentication is successful
is_match = best_match and best_distance <= tolerance
result = {
"success": True,
"is_match": is_match,
"matched_user": best_match["user_id"] if best_match else None,
"confidence": best_match["confidence"] if best_match else 0,
"distance": best_match["distance"] if best_match else float('inf'),
"threshold": tolerance,
"processing_time_ms": int(processing_time * 1000),
"image_path": auth_image_path
}
if is_match:
# Update authentication stats
user_data = self.database["users"][best_match["user_id"]]
user_data["last_authentication"] = datetime.now().isoformat()
user_data["authentication_count"] = user_data.get("authentication_count", 0) + 1
self.save_database()
print(f"\nβ
Authentication Successful!")
print(f"π€ User: {best_match['user_id']}")
print(f"π― Confidence: {result['confidence']:.1%}")
print(f"π Distance: {result['distance']:.3f}")
print(f"β‘ Processing: {result['processing_time_ms']}ms")
else:
print(f"\nβ Authentication Failed!")
if best_match:
print(f"π€ Closest match: {best_match['user_id']}")
print(f"π― Confidence: {result['confidence']:.1%}")
print(f"π Distance: {result['distance']:.3f} (threshold: {tolerance:.3f})")
print(f"β‘ Processing: {result['processing_time_ms']}ms")
return result
except Exception as e:
return {"success": False, "error": str(e)}
def main():
parser = argparse.ArgumentParser(description="High-Accuracy Face Authentication")
parser.add_argument("--mode", choices=["register", "auth"], required=True,
help="Mode: register or authenticate")
parser.add_argument("--user", type=str, help="User ID for registration")
parser.add_argument("--samples", type=int, default=3, help="Number of samples for registration")
parser.add_argument("--tolerance", type=float, default=0.6, help="Authentication tolerance")
args = parser.parse_args()
# Initialize face authentication system
face_auth = HighAccuracyFaceAuth()
if args.mode == "register":
if not args.user:
print("β User ID required for registration")
return
success = face_auth.register_user(args.user, args.samples)
if success:
print("π Registration completed successfully!")
else:
print("β Registration failed!")
elif args.mode == "auth":
result = face_auth.authenticate_user(args.tolerance)
if result["success"] and result["is_match"]:
print("π Access Granted!")
exit(0)
else:
print("π Access Denied!")
exit(1)
if __name__ == "__main__":
main()