Spaces:
Runtime error
Runtime error
ParisNeo
commited on
Commit
·
9578e95
1
Parent(s):
19ef1c2
upgraded
Browse files
app.py
CHANGED
@@ -52,6 +52,7 @@ class UI():
|
|
52 |
self.face_name=None
|
53 |
self.nb_images = 20
|
54 |
self.nb_faces = 3
|
|
|
55 |
# Important to set. If higher than this distance, the face is considered unknown
|
56 |
self.threshold = 4e-1
|
57 |
self.faces_db_preprocessed_path = Path(__file__).parent/"faces_db_preprocessed"
|
@@ -104,19 +105,19 @@ class UI():
|
|
104 |
with gr.Blocks():
|
105 |
with gr.Row():
|
106 |
with gr.Column():
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
self.gallery = gr.Gallery(
|
108 |
label="Uploaded Images", show_label=True, height=300, elem_id="gallery"
|
109 |
).style(grid=[2], height="auto")
|
110 |
self.btn_clear = gr.Button("Clear Gallery")
|
111 |
-
|
112 |
-
self.add_file = gr.Files(label="Files",file_types=["image"])
|
113 |
-
self.add_file.change(self.add_files, self.add_file, [self.gallery, self.faces])
|
114 |
-
self.txtFace_name2 = gr.Textbox(label="face_name")
|
115 |
-
self.btn_start = gr.Button("Build face embeddings")
|
116 |
-
self.status = gr.Label(label="Status")
|
117 |
-
self.txtFace_name2.change(self.set_face_name, inputs=self.txtFace_name2, outputs=self.status, show_progress=False)
|
118 |
-
self.btn_start.click(self.record_from_files, inputs=self.gallery, outputs=self.status, show_progress=True)
|
119 |
self.btn_clear.click(self.clear_galery,[],[self.gallery, self.add_file])
|
|
|
120 |
with gr.TabItem('Known Faces List'):
|
121 |
with gr.Blocks():
|
122 |
with gr.Row():
|
@@ -148,6 +149,33 @@ class UI():
|
|
148 |
|
149 |
demo.queue().launch()
|
150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
def clear_galery(self):
|
152 |
return self.gallery.update(value=[]), self.add_file.update(value=[])
|
153 |
|
@@ -250,7 +278,7 @@ class UI():
|
|
250 |
print(f"Saved {name}")
|
251 |
|
252 |
def record_from_webcam(self, image):
|
253 |
-
if self.face_name is None:
|
254 |
self.embeddings_cloud=[]
|
255 |
self.is_recording=False
|
256 |
return "Please input a face name"
|
@@ -293,8 +321,8 @@ class UI():
|
|
293 |
else:
|
294 |
return "Waiting"
|
295 |
|
296 |
-
def record_from_files(self, images):
|
297 |
-
if
|
298 |
self.embeddings_cloud=[]
|
299 |
self.is_recording=False
|
300 |
return "Please input a face name"
|
@@ -329,16 +357,15 @@ class UI():
|
|
329 |
embeddings_cloud_inv_cov = embeddings_cloud.std(axis=0)
|
330 |
# Now we save it.
|
331 |
# create a dialog box to ask for the subject name
|
332 |
-
|
333 |
-
with open(str(faces_path/f"{name}.pkl"),"wb") as f:
|
334 |
pickle.dump({"mean":embeddings_cloud_mean, "inv_cov":embeddings_cloud_inv_cov},f)
|
335 |
-
print(f"Saved {
|
336 |
self.i=0
|
337 |
self.embeddings_cloud=[]
|
338 |
self.is_recording=False
|
339 |
self.upgrade_faces()
|
340 |
|
341 |
-
return f"Saved {
|
342 |
else:
|
343 |
return "Waiting"
|
344 |
|
@@ -351,6 +378,7 @@ class UI():
|
|
351 |
fa.process(image)
|
352 |
|
353 |
if fa.nb_faces>0:
|
|
|
354 |
for i in range(fa.nb_faces):
|
355 |
try:
|
356 |
face = fa.faces[i]
|
@@ -373,13 +401,10 @@ class UI():
|
|
373 |
nearest_distance = distance
|
374 |
nearest = i
|
375 |
|
376 |
-
if nearest_distance>self.threshold:
|
377 |
-
face.draw_bounding_box(image, thickness=1,text=f"Unknown:{nearest_distance:.3e}")
|
378 |
-
else:
|
379 |
-
face.draw_bounding_box(image, thickness=1,text=f"{self.known_faces_names[nearest]}:{nearest_distance:.3e}")
|
380 |
except Exception as ex:
|
381 |
pass
|
382 |
-
|
383 |
# Return the resulting frame
|
384 |
return image
|
385 |
|
|
|
52 |
self.face_name=None
|
53 |
self.nb_images = 20
|
54 |
self.nb_faces = 3
|
55 |
+
|
56 |
# Important to set. If higher than this distance, the face is considered unknown
|
57 |
self.threshold = 4e-1
|
58 |
self.faces_db_preprocessed_path = Path(__file__).parent/"faces_db_preprocessed"
|
|
|
105 |
with gr.Blocks():
|
106 |
with gr.Row():
|
107 |
with gr.Column():
|
108 |
+
self.add_file = gr.Files(label="Files",file_types=["image"])
|
109 |
+
with gr.Row():
|
110 |
+
self.txtFace_name2 = gr.Textbox(label="face_name")
|
111 |
+
self.btn_start = gr.Button("Build face embeddings")
|
112 |
+
self.status = gr.Label(label="Status")
|
113 |
+
|
114 |
self.gallery = gr.Gallery(
|
115 |
label="Uploaded Images", show_label=True, height=300, elem_id="gallery"
|
116 |
).style(grid=[2], height="auto")
|
117 |
self.btn_clear = gr.Button("Clear Gallery")
|
118 |
+
self.btn_start.click(self.record_from_files, inputs=[self.gallery, self.txtFace_name2], outputs=self.status, show_progress=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
self.btn_clear.click(self.clear_galery,[],[self.gallery, self.add_file])
|
120 |
+
self.add_file.change(self.add_files, self.add_file, [self.gallery, self.faces])
|
121 |
with gr.TabItem('Known Faces List'):
|
122 |
with gr.Blocks():
|
123 |
with gr.Row():
|
|
|
149 |
|
150 |
demo.queue().launch()
|
151 |
|
152 |
+
|
153 |
+
|
154 |
+
def draw_name_on_bbox(self, image, bbox, name, font=cv2.FONT_HERSHEY_SIMPLEX,
|
155 |
+
font_scale=1, thickness=2, color=(0, 0, 255)):
|
156 |
+
# Upscale the image to avoid pixelization of the text
|
157 |
+
height, width = image.shape[:2]
|
158 |
+
scale = max(1, int(max(height, width) / 800))
|
159 |
+
new_height, new_width = int(height * scale), int(width * scale)
|
160 |
+
resized_image = cv2.resize(image, (new_width, new_height))
|
161 |
+
|
162 |
+
# Upscale the bounding box
|
163 |
+
bbox = [int(val * scale) for val in bbox]
|
164 |
+
|
165 |
+
# Draw the bounding box and the name
|
166 |
+
x1, y1, x2, y2 = bbox
|
167 |
+
cv2.rectangle(resized_image, (x1, y1), (x2, y2), color, thickness)
|
168 |
+
text_size, _ = cv2.getTextSize(name, font, font_scale, thickness)
|
169 |
+
text_x = x1 + (x2 - x1 - text_size[0]) // 2
|
170 |
+
text_y = y1 - text_size[1] - 5
|
171 |
+
cv2.putText(resized_image, name, (text_x, text_y), font, font_scale, color, thickness)
|
172 |
+
|
173 |
+
# Downscale the image back to its original size
|
174 |
+
final_image = cv2.resize(resized_image, (width, height))
|
175 |
+
|
176 |
+
return final_image
|
177 |
+
|
178 |
+
|
179 |
def clear_galery(self):
|
180 |
return self.gallery.update(value=[]), self.add_file.update(value=[])
|
181 |
|
|
|
278 |
print(f"Saved {name}")
|
279 |
|
280 |
def record_from_webcam(self, image):
|
281 |
+
if self.face_name is None or self.face_name=="":
|
282 |
self.embeddings_cloud=[]
|
283 |
self.is_recording=False
|
284 |
return "Please input a face name"
|
|
|
321 |
else:
|
322 |
return "Waiting"
|
323 |
|
324 |
+
def record_from_files(self, images, face_name):
|
325 |
+
if face_name is None or face_name=="":
|
326 |
self.embeddings_cloud=[]
|
327 |
self.is_recording=False
|
328 |
return "Please input a face name"
|
|
|
357 |
embeddings_cloud_inv_cov = embeddings_cloud.std(axis=0)
|
358 |
# Now we save it.
|
359 |
# create a dialog box to ask for the subject name
|
360 |
+
with open(str(faces_path/f"{face_name}.pkl"),"wb") as f:
|
|
|
361 |
pickle.dump({"mean":embeddings_cloud_mean, "inv_cov":embeddings_cloud_inv_cov},f)
|
362 |
+
print(f"Saved {face_name} embeddings")
|
363 |
self.i=0
|
364 |
self.embeddings_cloud=[]
|
365 |
self.is_recording=False
|
366 |
self.upgrade_faces()
|
367 |
|
368 |
+
return f"Saved {face_name} embeddings"
|
369 |
else:
|
370 |
return "Waiting"
|
371 |
|
|
|
378 |
fa.process(image)
|
379 |
|
380 |
if fa.nb_faces>0:
|
381 |
+
bboxes_and_names=[]
|
382 |
for i in range(fa.nb_faces):
|
383 |
try:
|
384 |
face = fa.faces[i]
|
|
|
401 |
nearest_distance = distance
|
402 |
nearest = i
|
403 |
|
404 |
+
bboxes_and_names.append([face.bounding_box, f"Unknown:{nearest_distance:.2e}" if nearest_distance>self.threshold else f"{self.known_faces_names[nearest]}:{nearest_distance:.2e}"])
|
|
|
|
|
|
|
405 |
except Exception as ex:
|
406 |
pass
|
407 |
+
image = fa.draw_names_on_bboxes(image,bboxes_and_names,upscale=2)
|
408 |
# Return the resulting frame
|
409 |
return image
|
410 |
|