-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdual_camera_recognizer.py
More file actions
550 lines (461 loc) · 22.1 KB
/
dual_camera_recognizer.py
File metadata and controls
550 lines (461 loc) · 22.1 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
import cv2
import numpy as np
import json
import os
import time
from datetime import datetime
import pickle
try:
from ultralytics import YOLO
YOLO_AVAILABLE = True
except ImportError:
YOLO_AVAILABLE = False
print("YOLO not available. Install with: pip install ultralytics")
class DualCameraRecognizer:
def __init__(self):
self.memory_file = "recognition_memory.json"
self.face_encodings_file = "face_encodings.pkl"
self.memory = self.load_memory()
self.face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
self.yolo_model = None
self.cameras = []
self.session_detections = []
# Recognition settings
self.confidence_threshold = 0.5
self.face_recognition_enabled = False
# Try to import face_recognition for advanced face features
try:
import face_recognition
self.face_recognition = face_recognition
self.face_recognition_enabled = True
self.known_face_encodings = []
self.known_face_names = []
self.load_face_encodings()
print("Advanced face recognition enabled!")
except ImportError:
print("face_recognition not available. Using basic face detection only.")
print("Install with: pip install face_recognition")
self.load_yolo_model()
self.initialize_cameras()
def load_memory(self):
"""Load recognition memory from file."""
if os.path.exists(self.memory_file):
try:
with open(self.memory_file, 'r') as f:
return json.load(f)
except (json.JSONDecodeError, ValueError) as e:
print(f"Warning: Corrupted memory file detected: {e}")
# Backup the corrupted file
backup_file = f"{self.memory_file}.backup_{int(time.time())}"
os.rename(self.memory_file, backup_file)
print(f"Corrupted file backed up as: {backup_file}")
print("Creating fresh memory database...")
return {
"people": {},
"objects": {},
"sessions": [],
"statistics": {
"total_people_seen": 0,
"total_objects_detected": 0,
"sessions_count": 0
}
}
def save_memory(self):
"""Save recognition memory to file."""
with open(self.memory_file, 'w') as f:
json.dump(self.memory, f, indent=2)
def load_face_encodings(self):
"""Load known face encodings."""
if os.path.exists(self.face_encodings_file):
with open(self.face_encodings_file, 'rb') as f:
data = pickle.load(f)
self.known_face_encodings = data.get('encodings', [])
self.known_face_names = data.get('names', [])
# Load basic face database
self.load_basic_face_database()
def save_face_encodings(self):
"""Save face encodings to file."""
data = {
'encodings': self.known_face_encodings,
'names': self.known_face_names
}
with open(self.face_encodings_file, 'wb') as f:
pickle.dump(data, f)
def load_basic_face_database(self):
"""Load basic face database."""
basic_db_file = "basic_face_database.json"
if os.path.exists(basic_db_file):
with open(basic_db_file, 'r') as f:
self.basic_face_database = json.load(f)
else:
self.basic_face_database = []
def save_basic_face_database(self):
"""Save basic face database."""
basic_db_file = "basic_face_database.json"
with open(basic_db_file, 'w') as f:
json.dump(self.basic_face_database, f, indent=2)
def load_yolo_model(self):
"""Initialize YOLO model."""
if YOLO_AVAILABLE:
try:
print("Loading YOLO model...")
self.yolo_model = YOLO('yolov8n.pt')
print("YOLO model loaded successfully!")
except Exception as e:
print(f"Failed to load YOLO model: {e}")
def initialize_cameras(self):
"""Initialize dual cameras."""
backends = [cv2.CAP_DSHOW, cv2.CAP_MSMF, cv2.CAP_ANY]
for camera_index in range(2):
cap = None
for backend in backends:
cap = cv2.VideoCapture(camera_index, backend)
if cap.isOpened():
ret, test_frame = cap.read()
if ret:
self.cameras.append({
'cap': cap,
'index': camera_index,
'backend': backend
})
print(f"Camera {camera_index} initialized successfully")
break
else:
cap.release()
cap = None
else:
if cap:
cap.release()
cap = None
if cap is None:
break
if len(self.cameras) < 2:
print(f"Warning: Only {len(self.cameras)} camera(s) available")
def get_object_color(self, class_name):
"""Get color for object class."""
colors = {
'person': (0, 255, 0), # Green
'car': (255, 0, 0), # Blue
'truck': (255, 0, 255), # Magenta
'bus': (0, 255, 255), # Yellow
'bicycle': (255, 255, 0), # Cyan
'motorcycle': (128, 0, 128), # Purple
'chair': (255, 165, 0), # Orange
'couch': (255, 192, 203), # Pink
'tv': (128, 128, 128), # Gray
'laptop': (0, 128, 255), # Light Blue
'cell phone': (255, 20, 147), # Deep Pink
'book': (139, 69, 19), # Brown
'bottle': (0, 100, 0), # Dark Green
'cup': (255, 215, 0), # Gold
}
return colors.get(class_name, (255, 255, 255))
def detect_and_recognize_faces(self, frame, camera_index):
"""Detect and recognize faces in frame."""
detections = []
# Basic face detection
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = self.face_cascade.detectMultiScale(gray, 1.1, 4)
for (x, y, w, h) in faces:
face_id = "unknown"
confidence = 0.0
# Advanced face recognition if available
if self.face_recognition_enabled and len(self.known_face_encodings) > 0:
# Extract face encoding
face_locations = [(y, x+w, y+h, x)]
face_encodings = self.face_recognition.face_encodings(frame, face_locations)
if face_encodings:
face_encoding = face_encodings[0]
matches = self.face_recognition.compare_faces(self.known_face_encodings, face_encoding)
face_distances = self.face_recognition.face_distance(self.known_face_encodings, face_encoding)
if matches and min(face_distances) < 0.6:
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
face_id = self.known_face_names[best_match_index]
confidence = 1.0 - face_distances[best_match_index]
# Basic face recognition using stored characteristics
elif hasattr(self, 'basic_face_database') and len(self.basic_face_database) > 0:
current_ratio = w / h
current_area = w * h
best_match = None
best_score = 0
for face_data in self.basic_face_database:
# Simple matching based on face characteristics
ratio_diff = abs(current_ratio - face_data['bbox_ratio'])
area_diff = abs(current_area - face_data['face_area']) / max(current_area, face_data['face_area'])
# Score based on similarity (lower differences = higher score)
score = 1.0 - (ratio_diff * 0.3 + area_diff * 0.7)
if score > 0.6 and score > best_score: # Threshold for basic matching
best_score = score
best_match = face_data
if best_match:
face_id = best_match['name']
confidence = best_score
# Draw face rectangle
color = (0, 255, 0) if face_id != "unknown" else (0, 0, 255)
cv2.rectangle(frame, (x, y), (x+w, y+h), color, 2)
# Add label
label = f"{face_id}: {confidence:.2f}" if face_id != "unknown" else "Unknown Face"
cv2.putText(frame, label, (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
# Record detection
detection = {
'type': 'face',
'id': face_id,
'confidence': confidence,
'bbox': (x, y, w, h),
'camera': camera_index,
'timestamp': datetime.now().isoformat()
}
detections.append(detection)
# Update memory
self.update_face_memory(face_id, detection)
return frame, detections
def detect_objects(self, frame, camera_index):
"""Detect objects using YOLO."""
detections = []
if not YOLO_AVAILABLE or self.yolo_model is None:
return frame, detections
try:
results = self.yolo_model(frame, verbose=False)
for result in results:
boxes = result.boxes
if boxes is not None:
for box in boxes:
x1, y1, x2, y2 = box.xyxy[0].cpu().numpy()
confidence = box.conf[0].cpu().numpy()
class_id = int(box.cls[0].cpu().numpy())
class_name = self.yolo_model.names[class_id]
if confidence > self.confidence_threshold:
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
color = self.get_object_color(class_name)
# Draw bounding box
cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
# Add label
label = f"{class_name}: {confidence:.2f}"
cv2.putText(frame, label, (x1, y1-10),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
# Record detection
detection = {
'type': 'object',
'class': class_name,
'confidence': float(confidence),
'bbox': (x1, y1, x2-x1, y2-y1),
'camera': camera_index,
'timestamp': datetime.now().isoformat()
}
detections.append(detection)
# Update memory
self.update_object_memory(class_name, detection)
except Exception as e:
print(f"Object detection error: {e}")
return frame, detections
def update_face_memory(self, face_id, detection):
"""Update face memory with new detection."""
if face_id not in self.memory["people"]:
self.memory["people"][face_id] = {
"first_seen": detection['timestamp'],
"last_seen": detection['timestamp'],
"total_detections": 0,
"cameras_seen": [],
"confidence_history": []
}
self.memory["statistics"]["total_people_seen"] += 1
person_data = self.memory["people"][face_id]
person_data["last_seen"] = detection['timestamp']
person_data["total_detections"] += 1
# Ensure cameras_seen is a list and add camera if not already present
if isinstance(person_data["cameras_seen"], set):
person_data["cameras_seen"] = list(person_data["cameras_seen"])
if detection['camera'] not in person_data["cameras_seen"]:
person_data["cameras_seen"].append(detection['camera'])
person_data["confidence_history"].append(detection['confidence'])
def update_object_memory(self, class_name, detection):
"""Update object memory with new detection."""
if class_name not in self.memory["objects"]:
self.memory["objects"][class_name] = {
"first_seen": detection['timestamp'],
"last_seen": detection['timestamp'],
"total_detections": 0,
"cameras_seen": [],
"confidence_history": []
}
self.memory["statistics"]["total_objects_detected"] += 1
object_data = self.memory["objects"][class_name]
object_data["last_seen"] = detection['timestamp']
object_data["total_detections"] += 1
# Ensure cameras_seen is a list and add camera if not already present
if isinstance(object_data["cameras_seen"], set):
object_data["cameras_seen"] = list(object_data["cameras_seen"])
if detection['camera'] not in object_data["cameras_seen"]:
object_data["cameras_seen"].append(detection['camera'])
object_data["confidence_history"].append(detection['confidence'])
def label_new_face(self, frame, face_bbox):
"""Label a new face interactively."""
x, y, w, h = face_bbox
face_image = frame[y:y+h, x:x+w]
# Show the face image for labeling
cv2.imshow('Face to Label', face_image)
cv2.waitKey(100) # Brief pause to show the face
# Get name from user
name = input("Enter name for this person (or 'skip' to ignore): ").strip()
if name.lower() != 'skip' and name:
if self.face_recognition_enabled:
# Advanced face recognition
face_locations = [(0, w, h, 0)]
face_encodings = self.face_recognition.face_encodings(face_image, face_locations)
if face_encodings:
self.known_face_encodings.append(face_encodings[0])
self.known_face_names.append(name)
self.save_face_encodings()
print(f"Added {name} to advanced face database!")
else:
# Basic face labeling using characteristics
face_data = {
'name': name,
'bbox_ratio': w / h, # Width to height ratio
'face_area': w * h,
'sample_image': face_image.tolist() if face_image.size < 10000 else None # Store small faces only
}
# Add to basic face database
if not hasattr(self, 'basic_face_database'):
self.basic_face_database = []
self.basic_face_database.append(face_data)
self.save_basic_face_database()
print(f"Added {name} to basic face database!")
# Also add to memory
detection = {
'type': 'face',
'id': name,
'confidence': 1.0,
'bbox': face_bbox,
'camera': 0, # Default camera
'timestamp': datetime.now().isoformat()
}
self.update_face_memory(name, detection)
cv2.destroyWindow('Face to Label')
return name
cv2.destroyWindow('Face to Label')
return None
def display_statistics(self, frame):
"""Display recognition statistics on frame."""
stats = self.memory["statistics"]
y_offset = 30
cv2.putText(frame, f"People Known: {len(self.memory['people'])}",
(10, y_offset), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
cv2.putText(frame, f"Objects Seen: {len(self.memory['objects'])}",
(10, y_offset + 25), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
cv2.putText(frame, f"Session Detections: {len(self.session_detections)}",
(10, y_offset + 50), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
def run(self):
"""Main recognition loop."""
if len(self.cameras) == 0:
print("No cameras available!")
return
print("Dual Camera Recognition System Started!")
print("Controls:")
print(" 'q' - Quit")
print(" 'l' - Label unknown face (when face is detected)")
print(" 's' - Save current session")
print(" 'r' - Show recognition report")
print(" 'c' - Clear session detections")
session_start = datetime.now()
show_stats = True
while True:
all_detections = []
# Process each camera
for camera in self.cameras:
ret, frame = camera['cap'].read()
if not ret:
continue
camera_index = camera['index']
# Detect faces and objects
frame, face_detections = self.detect_and_recognize_faces(frame, camera_index)
frame, object_detections = self.detect_objects(frame, camera_index)
# Combine detections
current_detections = face_detections + object_detections
all_detections.extend(current_detections)
# Add to session detections
self.session_detections.extend(current_detections)
# Display statistics
if show_stats:
self.display_statistics(frame)
# Add camera label
cv2.putText(frame, f"Camera {camera_index}", (10, frame.shape[0] - 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
# Show frame
cv2.imshow(f'Recognition Camera {camera_index}', frame)
# Handle keyboard input
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
elif key == ord('l'):
# Label unknown faces
for camera in self.cameras:
ret, frame = camera['cap'].read()
if ret:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = self.face_cascade.detectMultiScale(gray, 1.1, 4)
for face_bbox in faces:
self.label_new_face(frame, face_bbox)
break
elif key == ord('s'):
self.save_session(session_start)
print("Session saved!")
elif key == ord('r'):
self.show_recognition_report()
elif key == ord('c'):
self.session_detections = []
print("Session detections cleared!")
# Save final session
self.save_session(session_start)
self.cleanup()
def save_session(self, session_start):
"""Save current session data."""
session_data = {
"start_time": session_start.isoformat(),
"end_time": datetime.now().isoformat(),
"detections": self.session_detections,
"summary": {
"total_detections": len(self.session_detections),
"unique_people": len(set(d.get('id', 'unknown') for d in self.session_detections if d['type'] == 'face')),
"unique_objects": len(set(d.get('class', '') for d in self.session_detections if d['type'] == 'object'))
}
}
self.memory["sessions"].append(session_data)
self.memory["statistics"]["sessions_count"] += 1
self.save_memory()
def show_recognition_report(self):
"""Display recognition statistics report."""
print("\n" + "="*50)
print("RECOGNITION REPORT")
print("="*50)
print(f"Total People Known: {len(self.memory['people'])}")
print(f"Total Object Types Seen: {len(self.memory['objects'])}")
print(f"Total Sessions: {self.memory['statistics']['sessions_count']}")
print("\nMost Frequently Seen People:")
people_by_frequency = sorted(
self.memory['people'].items(),
key=lambda x: x[1]['total_detections'],
reverse=True
)
for name, data in people_by_frequency[:5]:
print(f" {name}: {data['total_detections']} detections")
print("\nMost Common Objects:")
objects_by_frequency = sorted(
self.memory['objects'].items(),
key=lambda x: x[1]['total_detections'],
reverse=True
)
for obj_class, data in objects_by_frequency[:5]:
print(f" {obj_class}: {data['total_detections']} detections")
print(f"\nCurrent Session Detections: {len(self.session_detections)}")
print("="*50 + "\n")
def cleanup(self):
"""Clean up resources."""
for camera in self.cameras:
camera['cap'].release()
cv2.destroyAllWindows()
print("Dual Camera Recognition System closed.")
if __name__ == "__main__":
recognizer = DualCameraRecognizer()
recognizer.run()