Spaces:
Runtime error
Runtime error
ParisNeo
commited on
Commit
·
25b5d1e
1
Parent(s):
71a1779
bugsfix
Browse files
app.py
CHANGED
@@ -22,10 +22,7 @@ from deepface import DeepFace
|
|
22 |
nb_images=50
|
23 |
|
24 |
|
25 |
-
|
26 |
-
faces_path = Path(__file__).parent/"faces"
|
27 |
-
if not faces_path.exists():
|
28 |
-
faces_path.mkdir(parents=True, exist_ok=True)
|
29 |
|
30 |
|
31 |
# Build face analyzer while specifying that we want to extract just a single face
|
@@ -46,6 +43,10 @@ import gradio as gr
|
|
46 |
import numpy as np
|
47 |
class UI():
|
48 |
def __init__(self) -> None:
|
|
|
|
|
|
|
|
|
49 |
self.i=0
|
50 |
self.embeddings_cloud = []
|
51 |
self.is_recording=False
|
@@ -113,8 +114,8 @@ class UI():
|
|
113 |
self.status = gr.Label(label="Status")
|
114 |
|
115 |
self.gallery = gr.Gallery(
|
116 |
-
label="Uploaded Images", show_label=True, height=300, elem_id="gallery"
|
117 |
-
).style(grid=[
|
118 |
self.btn_clear = gr.Button("Clear Gallery")
|
119 |
self.btn_start.click(self.record_from_files, inputs=[self.gallery, self.txtFace_name2], outputs=self.status, show_progress=True)
|
120 |
self.btn_clear.click(self.clear_galery,[],[self.gallery, self.add_file])
|
@@ -123,19 +124,15 @@ class UI():
|
|
123 |
with gr.Blocks():
|
124 |
with gr.Row():
|
125 |
with gr.Column():
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
headers=["Face Name"],
|
136 |
-
datatype=["str"],
|
137 |
-
label="Faces"
|
138 |
-
)
|
139 |
with gr.Row():
|
140 |
with gr.Accordion(label="Options", open=False):
|
141 |
self.sld_threshold = gr.Slider(1e-2,10,4e-1,step=1e-2,label="Recognition threshold")
|
@@ -154,8 +151,34 @@ class UI():
|
|
154 |
|
155 |
demo.queue().launch()
|
156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
def change_distance(self, type):
|
158 |
-
self.distance_type=type
|
159 |
|
160 |
def clear_galery(self):
|
161 |
return self.gallery.update(value=[]), self.add_file.update(value=[])
|
@@ -209,7 +232,7 @@ class UI():
|
|
209 |
print("Reloading faces")
|
210 |
self.known_faces=[]
|
211 |
self.known_faces_names=[]
|
212 |
-
face_files = [f for f in faces_path.iterdir() if f.name.endswith("pkl")]
|
213 |
for file in face_files:
|
214 |
with open(str(file),"rb") as f:
|
215 |
finger_print = pickle.load(f)
|
@@ -254,7 +277,7 @@ class UI():
|
|
254 |
# Now we save it.
|
255 |
# create a dialog box to ask for the subject name
|
256 |
name = self.face_name
|
257 |
-
with open(str(faces_path/f"{name}.pkl"),"wb") as f:
|
258 |
pickle.dump({"mean":embeddings_cloud_mean, "inv_cov":embeddings_cloud_inv_cov},f)
|
259 |
print(f"Saved {name}")
|
260 |
|
@@ -290,7 +313,7 @@ class UI():
|
|
290 |
# Now we save it.
|
291 |
# create a dialog box to ask for the subject name
|
292 |
name = self.face_name
|
293 |
-
with open(str(faces_path/f"{name}.pkl"),"wb") as f:
|
294 |
pickle.dump({"mean":embeddings_cloud_mean, "inv_cov":embeddings_cloud_inv_cov},f)
|
295 |
print(f"Saved {name} embeddings")
|
296 |
self.i=0
|
@@ -338,7 +361,7 @@ class UI():
|
|
338 |
embeddings_cloud_inv_cov = embeddings_cloud.std(axis=0)
|
339 |
# Now we save it.
|
340 |
# create a dialog box to ask for the subject name
|
341 |
-
with open(str(faces_path/f"{face_name}.pkl"),"wb") as f:
|
342 |
pickle.dump({"mean":embeddings_cloud_mean, "inv_cov":embeddings_cloud_inv_cov},f)
|
343 |
print(f"Saved {face_name} embeddings")
|
344 |
self.i=0
|
@@ -387,7 +410,8 @@ class UI():
|
|
387 |
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}"])
|
388 |
except Exception as ex:
|
389 |
pass
|
390 |
-
|
|
|
391 |
# Return the resulting frame
|
392 |
return image
|
393 |
|
@@ -430,7 +454,8 @@ class UI():
|
|
430 |
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}"])
|
431 |
except Exception as ex:
|
432 |
image=face_image
|
433 |
-
|
|
|
434 |
|
435 |
# Return the resulting frame
|
436 |
return image
|
|
|
22 |
nb_images=50
|
23 |
|
24 |
|
25 |
+
|
|
|
|
|
|
|
26 |
|
27 |
|
28 |
# Build face analyzer while specifying that we want to extract just a single face
|
|
|
43 |
import numpy as np
|
44 |
class UI():
|
45 |
def __init__(self) -> None:
|
46 |
+
# If faces path is empty then make it
|
47 |
+
self.faces_path = Path(__file__).parent/"faces"
|
48 |
+
if not self.faces_path.exists():
|
49 |
+
self.faces_path.mkdir(parents=True, exist_ok=True)
|
50 |
self.i=0
|
51 |
self.embeddings_cloud = []
|
52 |
self.is_recording=False
|
|
|
114 |
self.status = gr.Label(label="Status")
|
115 |
|
116 |
self.gallery = gr.Gallery(
|
117 |
+
label="Uploaded Images", show_label=True, height=300, elem_id="gallery", visible=False
|
118 |
+
).style(grid=[8], height="auto")
|
119 |
self.btn_clear = gr.Button("Clear Gallery")
|
120 |
self.btn_start.click(self.record_from_files, inputs=[self.gallery, self.txtFace_name2], outputs=self.status, show_progress=True)
|
121 |
self.btn_clear.click(self.clear_galery,[],[self.gallery, self.add_file])
|
|
|
124 |
with gr.Blocks():
|
125 |
with gr.Row():
|
126 |
with gr.Column():
|
127 |
+
self.faces_list = gr.Dataframe(
|
128 |
+
headers=["Face Name"],
|
129 |
+
datatype=["str"],
|
130 |
+
label="Faces",
|
131 |
+
value=[[n] for n in self.known_faces_names]
|
132 |
+
)
|
133 |
+
self.btn_reset_faces = gr.Button("clear faces list")
|
134 |
+
self.faces_list_status = gr.Label(label="Status")
|
135 |
+
self.btn_reset_faces.click(self.clear_faces,[],[self.faces_list_status])
|
|
|
|
|
|
|
|
|
136 |
with gr.Row():
|
137 |
with gr.Accordion(label="Options", open=False):
|
138 |
self.sld_threshold = gr.Slider(1e-2,10,4e-1,step=1e-2,label="Recognition threshold")
|
|
|
151 |
|
152 |
demo.queue().launch()
|
153 |
|
154 |
+
def clear_directory(self, directory_path):
|
155 |
+
"""
|
156 |
+
Recursively removes all files and subdirectories within the specified directory.
|
157 |
+
|
158 |
+
Args:
|
159 |
+
directory_path (str): The path to the directory to clear.
|
160 |
+
|
161 |
+
Returns:
|
162 |
+
None
|
163 |
+
"""
|
164 |
+
directory = Path(directory_path)
|
165 |
+
for item in directory.iterdir():
|
166 |
+
if item.is_file():
|
167 |
+
item.unlink()
|
168 |
+
elif item.is_dir():
|
169 |
+
self.clear_directory(item)
|
170 |
+
item.rmdir()
|
171 |
+
|
172 |
+
def clear_faces(self):
|
173 |
+
"""
|
174 |
+
clears faces
|
175 |
+
"""
|
176 |
+
self.clear_directory(self.faces_path)
|
177 |
+
self.upgrade_faces()
|
178 |
+
return "Faces removed"
|
179 |
+
|
180 |
def change_distance(self, type):
|
181 |
+
return self.distance_type.update(value=type)
|
182 |
|
183 |
def clear_galery(self):
|
184 |
return self.gallery.update(value=[]), self.add_file.update(value=[])
|
|
|
232 |
print("Reloading faces")
|
233 |
self.known_faces=[]
|
234 |
self.known_faces_names=[]
|
235 |
+
face_files = [f for f in self.faces_path.iterdir() if f.name.endswith("pkl")]
|
236 |
for file in face_files:
|
237 |
with open(str(file),"rb") as f:
|
238 |
finger_print = pickle.load(f)
|
|
|
277 |
# Now we save it.
|
278 |
# create a dialog box to ask for the subject name
|
279 |
name = self.face_name
|
280 |
+
with open(str(self.faces_path/f"{name}.pkl"),"wb") as f:
|
281 |
pickle.dump({"mean":embeddings_cloud_mean, "inv_cov":embeddings_cloud_inv_cov},f)
|
282 |
print(f"Saved {name}")
|
283 |
|
|
|
313 |
# Now we save it.
|
314 |
# create a dialog box to ask for the subject name
|
315 |
name = self.face_name
|
316 |
+
with open(str(self.faces_path/f"{name}.pkl"),"wb") as f:
|
317 |
pickle.dump({"mean":embeddings_cloud_mean, "inv_cov":embeddings_cloud_inv_cov},f)
|
318 |
print(f"Saved {name} embeddings")
|
319 |
self.i=0
|
|
|
361 |
embeddings_cloud_inv_cov = embeddings_cloud.std(axis=0)
|
362 |
# Now we save it.
|
363 |
# create a dialog box to ask for the subject name
|
364 |
+
with open(str(self.faces_path/f"{face_name}.pkl"),"wb") as f:
|
365 |
pickle.dump({"mean":embeddings_cloud_mean, "inv_cov":embeddings_cloud_inv_cov},f)
|
366 |
print(f"Saved {face_name} embeddings")
|
367 |
self.i=0
|
|
|
410 |
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}"])
|
411 |
except Exception as ex:
|
412 |
pass
|
413 |
+
if len(bboxes_and_names)>0:
|
414 |
+
image = fa.draw_names_on_bboxes(image,bboxes_and_names,upscale=2)
|
415 |
# Return the resulting frame
|
416 |
return image
|
417 |
|
|
|
454 |
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}"])
|
455 |
except Exception as ex:
|
456 |
image=face_image
|
457 |
+
if len(bboxes_and_names)>0:
|
458 |
+
image = fa.draw_names_on_bboxes(image,bboxes_and_names,upscale=2)
|
459 |
|
460 |
# Return the resulting frame
|
461 |
return image
|