Spaces:
Runtime error
Runtime error
ParisNeo
commited on
Commit
·
ae608e6
1
Parent(s):
9578e95
upgraded
Browse files
app.py
CHANGED
@@ -68,6 +68,7 @@ class UI():
|
|
68 |
|
69 |
with gr.Blocks() as demo:
|
70 |
self.faces = gr.State([])
|
|
|
71 |
gr.Markdown("## FaceAnalyzer face recognition test")
|
72 |
with gr.Tabs():
|
73 |
with gr.TabItem('Realtime Recognize'):
|
@@ -145,36 +146,16 @@ class UI():
|
|
145 |
self.cb_draw_landmarks.change(self.set_draw_landmarks, self.cb_draw_landmarks)
|
146 |
self.sld_nb_faces = gr.Slider(1,50,3,label="Maximum number of faces")
|
147 |
self.sld_nb_faces.change(self.set_nb_faces, self.sld_nb_faces)
|
|
|
|
|
|
|
|
|
148 |
|
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=[])
|
@@ -379,9 +360,9 @@ class UI():
|
|
379 |
|
380 |
if fa.nb_faces>0:
|
381 |
bboxes_and_names=[]
|
382 |
-
for
|
383 |
try:
|
384 |
-
face = fa.faces[
|
385 |
vertices = face.get_face_outer_vertices()
|
386 |
face_image = face.getFaceBox(image, vertices, margins=(40,40,40,40))
|
387 |
embedding = DeepFace.represent(face_image, enforce_detection=False)[0]["embedding"]
|
@@ -390,13 +371,15 @@ class UI():
|
|
390 |
nearest_distance = 1e100
|
391 |
nearest = 0
|
392 |
for i, known_face in enumerate(self.known_faces):
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
|
|
|
|
400 |
if distance<nearest_distance:
|
401 |
nearest_distance = distance
|
402 |
nearest = i
|
@@ -419,9 +402,9 @@ class UI():
|
|
419 |
fa.process(image)
|
420 |
|
421 |
if fa.nb_faces>0:
|
422 |
-
for
|
423 |
try:
|
424 |
-
face = fa.faces[
|
425 |
vertices = face.get_face_outer_vertices()
|
426 |
face_image = face.getFaceBox(image, vertices, margins=(40,40,40,40))
|
427 |
embedding = DeepFace.represent(face_image, enforce_detection=False)[0]["embedding"]
|
@@ -430,13 +413,15 @@ class UI():
|
|
430 |
nearest_distance = 1e100
|
431 |
nearest = 0
|
432 |
for i, known_face in enumerate(self.known_faces):
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
|
|
|
|
440 |
if distance<nearest_distance:
|
441 |
nearest_distance = distance
|
442 |
nearest = i
|
|
|
68 |
|
69 |
with gr.Blocks() as demo:
|
70 |
self.faces = gr.State([])
|
71 |
+
self.distance_type = gr.State("cosine")
|
72 |
gr.Markdown("## FaceAnalyzer face recognition test")
|
73 |
with gr.Tabs():
|
74 |
with gr.TabItem('Realtime Recognize'):
|
|
|
146 |
self.cb_draw_landmarks.change(self.set_draw_landmarks, self.cb_draw_landmarks)
|
147 |
self.sld_nb_faces = gr.Slider(1,50,3,label="Maximum number of faces")
|
148 |
self.sld_nb_faces.change(self.set_nb_faces, self.sld_nb_faces)
|
149 |
+
self.rd_distance_type = gr.Radio(
|
150 |
+
["cosine", "L1", "L2"], label="Distance", value="cosine"
|
151 |
+
)
|
152 |
+
self.rd_distance_type.change(self.change_distance, self.rd_distance_type, self.distance_type)
|
153 |
|
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=[])
|
|
|
360 |
|
361 |
if fa.nb_faces>0:
|
362 |
bboxes_and_names=[]
|
363 |
+
for j in range(fa.nb_faces):
|
364 |
try:
|
365 |
+
face = fa.faces[j]
|
366 |
vertices = face.get_face_outer_vertices()
|
367 |
face_image = face.getFaceBox(image, vertices, margins=(40,40,40,40))
|
368 |
embedding = DeepFace.represent(face_image, enforce_detection=False)[0]["embedding"]
|
|
|
371 |
nearest_distance = 1e100
|
372 |
nearest = 0
|
373 |
for i, known_face in enumerate(self.known_faces):
|
374 |
+
if self.distance_type == "cosine":
|
375 |
+
# Cosine distance
|
376 |
+
distance = self.cosine_distance(known_face["mean"], embedding)
|
377 |
+
elif self.distance_type =="L1":
|
378 |
+
# absolute distance
|
379 |
+
distance = np.abs(known_face["mean"]-embedding).sum()
|
380 |
+
elif self.distance_type == "L2":
|
381 |
+
# absolute distance
|
382 |
+
distance = np.sqrt(np.square(known_face["mean"]-embedding).sum())
|
383 |
if distance<nearest_distance:
|
384 |
nearest_distance = distance
|
385 |
nearest = i
|
|
|
402 |
fa.process(image)
|
403 |
|
404 |
if fa.nb_faces>0:
|
405 |
+
for j in range(fa.nb_faces):
|
406 |
try:
|
407 |
+
face = fa.faces[j]
|
408 |
vertices = face.get_face_outer_vertices()
|
409 |
face_image = face.getFaceBox(image, vertices, margins=(40,40,40,40))
|
410 |
embedding = DeepFace.represent(face_image, enforce_detection=False)[0]["embedding"]
|
|
|
413 |
nearest_distance = 1e100
|
414 |
nearest = 0
|
415 |
for i, known_face in enumerate(self.known_faces):
|
416 |
+
if self.distance_type == "cosine":
|
417 |
+
# Cosine distance
|
418 |
+
distance = self.cosine_distance(known_face["mean"], embedding)
|
419 |
+
elif self.distance_type =="L1":
|
420 |
+
# absolute distance
|
421 |
+
distance = np.abs(known_face["mean"]-embedding).sum()
|
422 |
+
elif self.distance_type == "L2":
|
423 |
+
# absolute distance
|
424 |
+
distance = np.sqrt(np.square(known_face["mean"]-embedding).sum())
|
425 |
if distance<nearest_distance:
|
426 |
nearest_distance = distance
|
427 |
nearest = i
|