-
Notifications
You must be signed in to change notification settings - Fork 0
/
recognize.py
257 lines (180 loc) · 7.06 KB
/
recognize.py
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
import threading
import time
import cv2
import numpy as np
import torch
import yaml
from torchvision import transforms
from face_alignment.alignment import norm_crop
from face_detection.scrfd.detector import SCRFD
from face_detection.yolov5_face.detector import Yolov5Face
from face_recognition.arcface.model import iresnet_inference
from face_recognition.arcface.utils import compare_encodings, read_features
from face_tracking.tracker.byte_tracker import BYTETracker
from face_tracking.tracker.visualize import plot_tracking
import threading
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
#detector = SCRFD(model_file="face_detection/scrfd/weights/scrfd_2.5g_bnkps.onnx")
detector = Yolov5Face(model_file="face_detection/yolov5_face/weights/yolov5n-face.pt")
recognizer = iresnet_inference(
model_name="r100", path="face_recognition/arcface/weights/arcface_r100.pth", device=device
)
images_names, images_embs = read_features(feature_path="./datasets/face_features/feature")
id_face_mapping = {}
id_face_mapping_lock = threading.Lock()
data_mapping = {
"raw_image": [],
"tracking_ids": [],
"detection_bboxes": [],
"detection_landmarks": [],
"tracking_bboxes": [],
}
def load_config(file_name):
with open(file_name, "r") as stream:
try:
return yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
def process_tracking(frame, detector, tracker, args, frame_id, fps):
outputs, img_info, bboxes, landmarks = detector.detect_tracking(image=frame)
tracking_tlwhs = []
tracking_ids = []
tracking_scores = []
tracking_bboxes = []
if outputs is not None:
online_targets = tracker.update(
outputs, [img_info["height"], img_info["width"]], (128, 128)
)
for i in range(len(online_targets)):
t = online_targets[i]
tlwh = t.tlwh
tid = t.track_id
vertical = tlwh[2] / tlwh[3] > args["aspect_ratio_thresh"]
if tlwh[2] * tlwh[3] > args["min_box_area"] and not vertical:
x1, y1, w, h = tlwh
tracking_bboxes.append([x1, y1, x1 + w, y1 + h])
tracking_tlwhs.append(tlwh)
tracking_ids.append(tid)
tracking_scores.append(t.score)
tracking_image = plot_tracking(
img_info["raw_img"],
tracking_tlwhs,
tracking_ids,
names=id_face_mapping,
frame_id=frame_id + 1,
fps=fps,
)
else:
tracking_image = img_info["raw_img"]
data_mapping["raw_image"] = img_info["raw_img"]
data_mapping["detection_bboxes"] = bboxes
data_mapping["detection_landmarks"] = landmarks
data_mapping["tracking_ids"] = tracking_ids
data_mapping["tracking_bboxes"] = tracking_bboxes
return tracking_image
@torch.no_grad()
def get_feature(face_image):
face_preprocess = transforms.Compose(
[
transforms.ToTensor(),
transforms.Resize((112, 112)),
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
]
)
face_image = cv2.cvtColor(face_image, cv2.COLOR_BGR2RGB)
face_image = face_preprocess(face_image).unsqueeze(0).to(device)
emb_img_face = recognizer(face_image).cpu().numpy()
images_emb = emb_img_face / np.linalg.norm(emb_img_face)
return images_emb
def recognition(face_image):
query_emb = get_feature(face_image)
score, id_min = compare_encodings(query_emb, images_embs)
name = images_names[id_min]
score = score[0]
return score, name
def mapping_bbox(box1, box2):
x_min_inter = max(box1[0], box2[0])
y_min_inter = max(box1[1], box2[1])
x_max_inter = min(box1[2], box2[2])
y_max_inter = min(box1[3], box2[3])
intersection_area = max(0, x_max_inter - x_min_inter + 1) * max(
0, y_max_inter - y_min_inter + 1
)
area_box1 = (box1[2] - box1[0] + 1) * (box1[3] - box1[1] + 1)
area_box2 = (box2[2] - box2[0] + 1) * (box2[3] - box2[1] + 1)
union_area = area_box1 + area_box2 - intersection_area
iou = intersection_area / union_area
return iou
def tracking(detector, args):
start_time = time.time_ns()
frame_count = 0
fps = -1
tracker = BYTETracker(args=args, frame_rate=30)
frame_id = 0
cap = cv2.VideoCapture(0)
while True:
_, img = cap.read()
tracking_image = process_tracking(img, detector, tracker, args, frame_id, fps)
frame_count += 1
if frame_count >= 30:
fps = 1e9 * frame_count / (time.time_ns() - start_time)
frame_count = 0
start_time = time.time_ns()
cv2.imshow("Face Recognition", tracking_image)
ch = cv2.waitKey(1)
if ch == 27 or ch == ord("q") or ch == ord("Q"):
break
import requests
def recognize():
while True:
raw_image = data_mapping["raw_image"]
detection_landmarks = data_mapping["detection_landmarks"]
detection_bboxes = data_mapping["detection_bboxes"]
tracking_ids = data_mapping["tracking_ids"]
tracking_bboxes = data_mapping["tracking_bboxes"]
for i in range(len(tracking_bboxes)):
for j in range(len(detection_bboxes)):
mapping_score = mapping_bbox(box1=tracking_bboxes[i], box2=detection_bboxes[j])
if mapping_score > 0.:
face_alignment = norm_crop(img=raw_image, landmark=detection_landmarks[j])
score, name = recognition(face_image=face_alignment)
if name is not None:
if score < 0.25:
caption = "UNKNOWN"
else:
caption = f"{name}"
with id_face_mapping_lock:
id_face_mapping[tracking_ids[i]] = caption
detection_bboxes = np.delete(detection_bboxes, j, axis=0)
detection_landmarks = np.delete(detection_landmarks, j, axis=0)
url = 'api'
data = {
'mobile_id' : '',
'Employee_code' : name,
'check_in_time' : '',
'check_in_type' : '',
'device_id' : '',
}
response = requests.post(url, data =data)
if response.status_code == 200:
print('data sent successfully')
else:
print('data sent failed')
break
if tracking_bboxes == []:
print("Face is detected {name}")
def main():
file_name = "./face_tracking/config/config_tracking.yaml"
config_tracking = load_config(file_name)
thread_track = threading.Thread(
target=tracking,
args=(
detector,
config_tracking,
),
)
thread_track.start()
thread_recognize = threading.Thread(target=recognize)
thread_recognize.start()
if __name__ == "__main__":
main()