-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathface_recognition_module.py
More file actions
218 lines (178 loc) · 7.94 KB
/
face_recognition_module.py
File metadata and controls
218 lines (178 loc) · 7.94 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
# import os
# import cv2
# import face_recognition
# import pickle
# import numpy as np
# from multiprocessing import shared_memory
# encodings_file = "encodings.pickle"
# if os.path.exists(encodings_file):
# with open(encodings_file, "rb") as f:
# data = pickle.load(f)
# known_encodings = data["encodings"]
# known_names = data["names"]
# else:
# known_encodings = []
# known_names = []
# def face_recognition_process(shm_name, shape, output_queue, cam_id):
# shared_mem = shared_memory.SharedMemory(name=shm_name)
# frame_buffer = np.ndarray(shape, dtype=np.uint8, buffer=shared_mem.buf)
# while True:
# frame = frame_buffer.copy()
# rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# boxes = face_recognition.face_locations(rgb_frame)
# encodings = face_recognition.face_encodings(rgb_frame, boxes)
# for encoding, (top, right, bottom, left) in zip(encodings, boxes):
# matches = face_recognition.compare_faces(known_encodings, encoding, tolerance=0.5)
# name = "Unknown"
# if True in matches:
# matched_idxs = [i for (i, b) in enumerate(matches) if b]
# counts = {}
# for i in matched_idxs:
# recognized_name = known_names[i]
# counts[recognized_name] = counts.get(recognized_name, 0) + 1
# name = max(counts, key=counts.get)
# output_queue.put({
# "cam_id": cam_id,
# "name": name,
# "bbox": (left, top, right, bottom),
# "detection_type": "face"
# })
# if __name__ == "__main__":
# print("Run main.py to start the system.")
# import os
# import cv2
# import face_recognition
# import pickle
# import numpy as np
# from multiprocessing import shared_memory
# # Load known face encodings
# encodings_file = "encodings.pickle"
# if os.path.exists(encodings_file):
# with open(encodings_file, "rb") as f:
# data = pickle.load(f)
# known_encodings = data["encodings"]
# known_names = data["names"]
# else:
# known_encodings = []
# known_names = []
# def face_recognition_process(shm_name, shape, output_queue, cam_id):
# shared_mem = shared_memory.SharedMemory(name=shm_name)
# frame_buffer = np.ndarray(shape, dtype=np.uint8, buffer=shared_mem.buf)
# print(f"[INFO] Face recognition started for Camera {cam_id}...")
# try:
# while True:
# if frame_buffer is None or frame_buffer.size == 0:
# continue # Skip if frame is empty
# # ✅ Resize image to speed up processing (Reduce resolution by half)
# small_frame = cv2.resize(frame_buffer, (shape[1] // 2, shape[0] // 2))
# # ✅ Ensure correct data type
# if small_frame.dtype != np.uint8:
# small_frame = small_frame.astype(np.uint8)
# # ✅ Convert frame to RGB
# rgb_frame = cv2.cvtColor(small_frame, cv2.COLOR_BGR2RGB)
# # Face detection
# boxes = face_recognition.face_locations(rgb_frame)
# encodings = face_recognition.face_encodings(rgb_frame, boxes)
# detected_faces = []
# for encoding, (top, right, bottom, left) in zip(encodings, boxes):
# matches = face_recognition.compare_faces(known_encodings, encoding, tolerance=0.5)
# name = "Unknown"
# if True in matches:
# matched_idxs = [i for (i, b) in enumerate(matches) if b]
# counts = {}
# for i in matched_idxs:
# recognized_name = known_names[i]
# counts[recognized_name] = counts.get(recognized_name, 0) + 1
# name = max(counts, key=counts.get)
# detected_faces.append({
# "cam_id": cam_id,
# "name": name,
# "bbox": (left, top, right, bottom),
# "detection_type": "face"
# })
# if detected_faces:
# output_queue.put(detected_faces)
# except Exception as e:
# print(f"[ERROR] Face recognition process encountered an issue: {str(e)}")
# finally:
# print(f"[INFO] Face recognition shutting down for Camera {cam_id}...")
# shared_mem.close()
import os
import cv2
import face_recognition
import pickle
import numpy as np
import time
from multiprocessing import shared_memory
# Load known face encodings
encodings_file = "encodings.pickle"
if os.path.exists(encodings_file):
with open(encodings_file, "rb") as f:
data = pickle.load(f)
known_encodings = data["encodings"]
known_names = data["names"]
else:
known_encodings = []
known_names = []
def face_recognition_process(shm_name, shape, output_queue, cam_id):
shared_mem = shared_memory.SharedMemory(name=shm_name)
frame_buffer = np.ndarray(shape, dtype=np.uint8, buffer=shared_mem.buf)
print(f"[INFO] Face recognition started for Camera {cam_id}...")
try:
while True:
frame = frame_buffer.copy()
if frame is None or frame.size == 0:
continue
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
small_frame = cv2.resize(rgb_frame, (shape[1] // 2, shape[0] // 2))
boxes = face_recognition.face_locations(small_frame)
encodings = face_recognition.face_encodings(small_frame, boxes)
detected_faces = []
for encoding, (top, right, bottom, left) in zip(encodings, boxes):
matches = face_recognition.compare_faces(known_encodings, encoding, tolerance=0.5)
name = "Unknown"
if True in matches:
matched_idxs = [i for (i, b) in enumerate(matches) if b]
counts = {}
for i in matched_idxs:
recognized_name = known_names[i]
counts[recognized_name] = counts.get(recognized_name, 0) + 1
name = max(counts, key=counts.get)
# Scale box back to original size
scale_x, scale_y = shape[1] / (shape[1] // 2), shape[0] / (shape[0] // 2)
left = int(left * scale_x)
top = int(top * scale_y)
right = int(right * scale_x)
bottom = int(bottom * scale_y)
# Draw box and label
cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
cv2.putText(frame, name, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
detected_faces.append({
"cam_id": cam_id,
"name": name,
"bbox": (left, top, right, bottom),
"detection_type": "face"
})
if detected_faces:
image_path = save_face_frame(frame, cam_id, name)
for face in detected_faces:
face["image_path"] = image_path
output_queue.put(detected_faces)
except Exception as e:
print(f"[ERROR] Face recognition process encountered an issue: {str(e)}")
finally:
print(f"[INFO] Face recognition shutting down for Camera {cam_id}...")
shared_mem.close()
# 🔹 Save Face Detection Image
def save_face_frame(frame, cam_id, label):
os.makedirs("face_alerts", exist_ok=True)
timestamp = time.strftime("%Y%m%d_%H%M%S")
filename = f"face_cam{cam_id}_{label}_{timestamp}.jpg"
image_path = os.path.join("face_alerts", filename)
cv2.imwrite(image_path, frame)
if os.path.exists(image_path) and os.path.getsize(image_path) > 0:
print(f"[INFO] Face detection frame saved: {image_path}")
return image_path
else:
print(f"[ERROR] Failed to save face image for Camera {cam_id}.")
return None