Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from deepface import DeepFace
|
3 |
+
import numpy as np
|
4 |
+
import PIL
|
5 |
+
from PIL import Image, ImageDraw, ImageFont
|
6 |
+
import time
|
7 |
+
|
8 |
+
dbackends = [
|
9 |
+
'opencv',
|
10 |
+
'ssd',
|
11 |
+
'dlib',
|
12 |
+
'mtcnn',
|
13 |
+
'retinaface',
|
14 |
+
'mediapipe',
|
15 |
+
'yolov8',
|
16 |
+
'yunet',
|
17 |
+
'fastmtcnn',
|
18 |
+
]
|
19 |
+
|
20 |
+
annotated_image = gr.AnnotatedImage()
|
21 |
+
jsontext = gr.Text(label= "deepface extract_faces results",)
|
22 |
+
|
23 |
+
def findFaces(imgfile,dbackend):
|
24 |
+
start_time = time.time()
|
25 |
+
print(start_time)
|
26 |
+
|
27 |
+
face_objs = DeepFace.extract_faces(img_path = imgfile, enforce_detection = False, detector_backend = dbackend)
|
28 |
+
numberoffaces = len(face_objs)
|
29 |
+
jsontext = ''
|
30 |
+
faceannotations = []
|
31 |
+
for face_obj in face_objs:
|
32 |
+
face_coordinates = (face_obj["facial_area"]["x"],face_obj["facial_area"]["y"], (face_obj["facial_area"]["x"] + face_obj["facial_area"]["w"]),(face_obj["facial_area"]["y"] + face_obj["facial_area"]["h"]))
|
33 |
+
face_confidence = "{:.0%}".format(face_obj["confidence"])
|
34 |
+
face_result=[face_coordinates,face_confidence]
|
35 |
+
faceannotations.append(face_result)
|
36 |
+
jsontext=faceannotations
|
37 |
+
run_time = str(round((time.time() - start_time),2))
|
38 |
+
results = gr.AnnotatedImage(
|
39 |
+
label= "Detected " + str(numberoffaces) + " faces via " + dbackend + ' in ' + run_time + ' seconds.',
|
40 |
+
value=(imgfile, faceannotations)
|
41 |
+
)
|
42 |
+
|
43 |
+
print(run_time)
|
44 |
+
return(results,jsontext,numberoffaces,run_time)
|
45 |
+
|
46 |
+
dbackendchoice = gr.Radio(choices=dbackends,label='Detector Backend:')
|
47 |
+
demo = gr.Interface(
|
48 |
+
allow_flagging = "never",
|
49 |
+
fn=findFaces,
|
50 |
+
inputs=[gr.Image(value="8428_26_SM.jpg"), dbackendchoice],
|
51 |
+
outputs=[annotated_image,jsontext],
|
52 |
+
|
53 |
+
)
|
54 |
+
|
55 |
+
demo.launch(show_error=True)
|
56 |
+
|