Spaces:
Runtime error
Runtime error
talha
commited on
Commit
•
a9d5e18
1
Parent(s):
611a23f
added app file
Browse files- gradio_app.py +222 -0
- requirements.txt +12 -0
gradio_app.py
ADDED
@@ -0,0 +1,222 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from email.headerregistry import Group
|
2 |
+
import numpy as np
|
3 |
+
from psutil import getloadavg
|
4 |
+
import tensorflow as tf
|
5 |
+
import logging
|
6 |
+
from rich.logging import RichHandler
|
7 |
+
from PIL import Image
|
8 |
+
import numpy as np
|
9 |
+
from config import get_config
|
10 |
+
from deepface import DeepFace
|
11 |
+
import matplotlib.pyplot as plt
|
12 |
+
from imutils import url_to_image
|
13 |
+
import cv2
|
14 |
+
import gradio as gr
|
15 |
+
|
16 |
+
# # --------------------------------------------------------------------------
|
17 |
+
# # global variables
|
18 |
+
# # --------------------------------------------------------------------------
|
19 |
+
|
20 |
+
REFERENCE = None
|
21 |
+
QUERY = None
|
22 |
+
interpreter = tf.lite.Interpreter(model_path="IR/model_float16_quant.tflite")
|
23 |
+
interpreter.allocate_tensors()
|
24 |
+
input_details = interpreter.get_input_details()
|
25 |
+
output_details = interpreter.get_output_details()
|
26 |
+
ref = None
|
27 |
+
query = None
|
28 |
+
face_query_ = None
|
29 |
+
face_ref_ = None
|
30 |
+
|
31 |
+
# demo = gr.Interface(
|
32 |
+
# fn=image_classifier,
|
33 |
+
# inputs=[
|
34 |
+
# gr.Image(shape=(224, 224),
|
35 |
+
# image_mode='RGB',
|
36 |
+
# source='webcam',
|
37 |
+
# type="numpy",
|
38 |
+
# label="Reference Image",
|
39 |
+
# streaming=True,
|
40 |
+
# mirror_webcam=True),
|
41 |
+
|
42 |
+
# gr.Image(shape=(224, 224),
|
43 |
+
# image_mode='RGB',
|
44 |
+
# source='webcam',
|
45 |
+
# type="numpy",
|
46 |
+
# label="Query Image",
|
47 |
+
# streaming=True,
|
48 |
+
# mirror_webcam=True)
|
49 |
+
# ],
|
50 |
+
# outputs=[
|
51 |
+
# gr.Number(label="Cosine Similarity",
|
52 |
+
# precision=5),
|
53 |
+
# gr.Plot(label="Reference Embedding Histogram",
|
54 |
+
# ),
|
55 |
+
# gr.Plot(label="Query Embedding Histogram",
|
56 |
+
# )
|
57 |
+
# ],
|
58 |
+
# live=False,
|
59 |
+
# title="Face Recognition",
|
60 |
+
# # description='''
|
61 |
+
# # | feature | description |
|
62 |
+
# # | :-----:| :------------: |
|
63 |
+
# # | model | mobile-facenet |
|
64 |
+
# # | precision | fp16 |
|
65 |
+
# # |type | tflite|
|
66 |
+
# # ''',
|
67 |
+
# # article="""
|
68 |
+
|
69 |
+
# # - detects face in input image
|
70 |
+
# # - resizes face to 112x112
|
71 |
+
# # - aligns the face using **deepface MTCNN**
|
72 |
+
# # - runs inference on the aligned face
|
73 |
+
|
74 |
+
# # """,
|
75 |
+
# allow_flagging="auto",
|
76 |
+
# analytics_enabled=True,
|
77 |
+
# )
|
78 |
+
|
79 |
+
|
80 |
+
# demo.launch(inbrowser=True, auth=("talha", "123"))
|
81 |
+
|
82 |
+
def plot_images():
|
83 |
+
global face_query_, face_ref_
|
84 |
+
return face_ref_, face_query_
|
85 |
+
|
86 |
+
|
87 |
+
def predict(interpreter, input_details, input_data, output_details):
|
88 |
+
interpreter.set_tensor(input_details[0]['index'], input_data)
|
89 |
+
|
90 |
+
interpreter.invoke()
|
91 |
+
|
92 |
+
output_data = interpreter.get_tensor(output_details[0]['index'])
|
93 |
+
|
94 |
+
return output_data
|
95 |
+
|
96 |
+
|
97 |
+
def get_ref_vector(image):
|
98 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
99 |
+
cv2.imwrite("ref.jpg", image)
|
100 |
+
global face_ref_
|
101 |
+
face_ref_ = DeepFace.detectFace("ref.jpg", detector_backend="opencv",
|
102 |
+
align=True, target_size=(112, 112))
|
103 |
+
|
104 |
+
face_ref = face_ref_.copy()
|
105 |
+
# face_ is [0, 1] fp32 , needs to be changed to [0, 255] uint8
|
106 |
+
face_ref = cv2.normalize(face_ref, None, 0, 255,
|
107 |
+
cv2.NORM_MINMAX, cv2.CV_32FC3)
|
108 |
+
print(
|
109 |
+
f"dtype {face_ref.dtype} || max {np.max(face_ref)} || min {np.min(face_ref)}")
|
110 |
+
|
111 |
+
# calculate embeddings
|
112 |
+
face_ref = face_ref[np.newaxis, ...] # [1, 112, 112, 3]
|
113 |
+
output_data_ref = predict(interpreter, input_details,
|
114 |
+
face_ref, output_details)
|
115 |
+
|
116 |
+
global ref
|
117 |
+
ref = output_data_ref
|
118 |
+
|
119 |
+
return str(f"shape ---> {face_ref_.shape} dtype ---> {face_ref_.dtype} max {face_ref_.max()} min {face_ref_.min()}"), ref
|
120 |
+
|
121 |
+
|
122 |
+
def get_query_vector(image1):
|
123 |
+
image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)
|
124 |
+
cv2.imwrite("query.jpg", image1)
|
125 |
+
global face_query_
|
126 |
+
|
127 |
+
face_query_ = DeepFace.detectFace("query.jpg", detector_backend="opencv",
|
128 |
+
align=True, target_size=(112, 112))
|
129 |
+
|
130 |
+
face_query = face_query_.copy()
|
131 |
+
# face_ is [0, 1] fp32 , needs to be changed to [0, 255] uint8
|
132 |
+
face_query = cv2.normalize(
|
133 |
+
face_query, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_32FC3)
|
134 |
+
print(
|
135 |
+
f"dtype {face_query.dtype} || max {np.max(face_query)} || min {np.min(face_query)}")
|
136 |
+
|
137 |
+
# calculate embeddings
|
138 |
+
face_query = face_query[np.newaxis, ...] # [1, 112, 112, 3]
|
139 |
+
output_data_query = predict(interpreter, input_details,
|
140 |
+
face_query, output_details)
|
141 |
+
global query
|
142 |
+
query = output_data_query
|
143 |
+
return str(f"shape ---> {face_query_.shape} dtype ---> {face_query_.dtype} max {face_query_.max()} min {face_query_.min()}"), query
|
144 |
+
|
145 |
+
|
146 |
+
def get_metrics():
|
147 |
+
global ref, query
|
148 |
+
return float(np.dot(np.squeeze(ref), np.squeeze(query))), float(np.linalg.norm(np.squeeze(ref) - np.squeeze(query)))
|
149 |
+
|
150 |
+
|
151 |
+
with gr.Blocks(analytics_enabled=True, title="Face Recognition") as demo:
|
152 |
+
|
153 |
+
# draw a box around children
|
154 |
+
with gr.Box():
|
155 |
+
gr.Markdown(
|
156 |
+
"# First provide the *reference* image and then the *query* image. The **cosine similarity** will be displayed as output.")
|
157 |
+
# put both cameras under separate groups
|
158 |
+
with gr.Group():
|
159 |
+
# components under this scope will have no padding or margin between them
|
160 |
+
with gr.Row():
|
161 |
+
# reference image
|
162 |
+
with gr.Column():
|
163 |
+
inp_ref = gr.Image(shape=(224, 224),
|
164 |
+
image_mode='RGB',
|
165 |
+
source='webcam',
|
166 |
+
type="numpy",
|
167 |
+
label="Reference Image",
|
168 |
+
streaming=True,
|
169 |
+
mirror_webcam=True),
|
170 |
+
|
171 |
+
out_ref = [gr.Textbox(label="Face capture details"),
|
172 |
+
gr.Dataframe(label="Embedding",
|
173 |
+
type="pandas", max_cols=512,
|
174 |
+
headers=None),
|
175 |
+
]
|
176 |
+
# make button on left column
|
177 |
+
btn_ref = gr.Button("reference_image")
|
178 |
+
btn_ref.click(fn=get_ref_vector,
|
179 |
+
inputs=inp_ref, outputs=out_ref)
|
180 |
+
with gr.Column():
|
181 |
+
inp_query = gr.Image(shape=(224, 224),
|
182 |
+
image_mode='RGB',
|
183 |
+
source='webcam',
|
184 |
+
type="numpy",
|
185 |
+
label="Query Image",
|
186 |
+
streaming=True,
|
187 |
+
mirror_webcam=True),
|
188 |
+
|
189 |
+
out_query = [gr.Textbox(label="Face capture details"),
|
190 |
+
gr.Dataframe(label="Embedding",
|
191 |
+
type="pandas", max_cols=512,
|
192 |
+
headers=None),
|
193 |
+
]
|
194 |
+
# make button on right column
|
195 |
+
btn_query = gr.Button("query_image")
|
196 |
+
btn_query.click(fn=get_query_vector,
|
197 |
+
inputs=inp_query, outputs=out_query)
|
198 |
+
with gr.Box():
|
199 |
+
gr.Markdown("# Metrics")
|
200 |
+
with gr.Group():
|
201 |
+
gr.Markdown(
|
202 |
+
"The **cosine similarity** and **l2 norm of diff.** will be displayed as output here")
|
203 |
+
with gr.Row():
|
204 |
+
out_sim = gr.Number(label="Cosine Similarity", precision=5)
|
205 |
+
out_d = gr.Number(label="L2 norm distance", precision=5)
|
206 |
+
# make button in center, outside row
|
207 |
+
btn_sim = gr.Button("Calculate Metrics")
|
208 |
+
btn_sim.click(fn=get_metrics, inputs=[], outputs=[out_sim, out_d])
|
209 |
+
with gr.Box():
|
210 |
+
with gr.Group():
|
211 |
+
gr.Markdown("# detected face results are shown below")
|
212 |
+
with gr.Row():
|
213 |
+
out_faces = [
|
214 |
+
gr.Image(shape=(60, 60), label="Detected Face Reference"),
|
215 |
+
gr.Image(shape=(112, 112), label="Detected Face Query")
|
216 |
+
]
|
217 |
+
# make button inside row
|
218 |
+
# make button outside (below) row
|
219 |
+
button_show = gr.Button("Show detected faces")
|
220 |
+
button_show.click(fn=plot_images, inputs=[], outputs=out_faces)
|
221 |
+
|
222 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
config==0.5.1
|
2 |
+
deepface==0.0.75
|
3 |
+
gradio==3.1.1
|
4 |
+
imutils==0.5.4
|
5 |
+
matplotlib==3.5.2
|
6 |
+
numpy==1.22.4
|
7 |
+
opencv_contrib_python==4.6.0.66
|
8 |
+
opencv-python==4.6.0.66
|
9 |
+
Pillow==9.2.0
|
10 |
+
psutil==5.9.1
|
11 |
+
rich==12.5.1
|
12 |
+
tensorflow==2.8.0
|