Update app.py
Browse filesupgrader UI, API
app.py
CHANGED
@@ -4,50 +4,315 @@ import requests
|
|
4 |
import json
|
5 |
from PIL import Image
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
def get_attributes(json):
|
8 |
-
|
|
|
9 |
attr = json.get('attribute')
|
10 |
age = attr.get('age')
|
11 |
gender = attr.get('gender')
|
12 |
emotion = attr.get('emotion')
|
13 |
ethnicity = attr.get('ethnicity')
|
14 |
|
15 |
-
mask =
|
|
|
16 |
if attr.get('glasses') == 'USUAL':
|
17 |
-
|
18 |
if attr.get('glasses') == 'DARK':
|
19 |
-
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
26 |
facehair = attr.get('facial_hair')
|
27 |
haircolor = attr.get('hair_color')
|
28 |
hairtype = attr.get('hair_type')
|
29 |
headwear = attr.get('headwear')
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
if attr.get('
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
pitch = attr.get('pitch')
|
44 |
roll = attr.get('roll')
|
45 |
yaw = attr.get('yaw')
|
46 |
quality = attr.get('quality')
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
def compare_face(frame1, frame2):
|
50 |
-
url = "https://recognito.p.rapidapi.com/api/
|
51 |
try:
|
52 |
files = {'image1': open(frame1, 'rb'), 'image2': open(frame2, 'rb')}
|
53 |
headers = {"X-RapidAPI-Key": os.environ.get("API_KEY")}
|
@@ -65,8 +330,6 @@ def compare_face(frame1, frame2):
|
|
65 |
face1 = Image.new('RGBA',(150, 150), (80,80,80,0))
|
66 |
face2 = Image.new('RGBA',(150, 150), (80,80,80,0))
|
67 |
|
68 |
-
liveness1, age1, gender1, emotion1, ethnicity1, mask1, eye1, facehair1, haircolor1, hairtype1, headwear1, activity1, pitch1, roll1, yaw1, quality1 = [None] * 16
|
69 |
-
liveness2, age2, gender2, emotion2, ethnicity2, mask2, eye2, facehair2, haircolor2, hairtype2, headwear2, activity2, pitch2, roll2, yaw2, quality2 = [None] * 16
|
70 |
res1 = r.json().get('image1')
|
71 |
|
72 |
if res1 is not None and res1:
|
@@ -90,7 +353,6 @@ def compare_face(frame1, frame2):
|
|
90 |
resized_h = 150
|
91 |
|
92 |
face1 = face1.resize((int(resized_w), int(resized_h)))
|
93 |
-
liveness1, age1, gender1, emotion1, ethnicity1, mask1, eye1, facehair1, haircolor1, hairtype1, headwear1, activity1, pitch1, roll1, yaw1, quality1 = get_attributes(res1)
|
94 |
|
95 |
res2 = r.json().get('image2')
|
96 |
if res2 is not None and res2:
|
@@ -115,91 +377,96 @@ def compare_face(frame1, frame2):
|
|
115 |
resized_h = 150
|
116 |
|
117 |
face2 = face2.resize((int(resized_w), int(resized_h)))
|
118 |
-
liveness2, age2, gender2, emotion2, ethnicity2, mask2, eye2, facehair2, haircolor2, hairtype2, headwear2, activity2, pitch2, roll2, yaw2, quality2 = get_attributes(res2)
|
119 |
except:
|
120 |
pass
|
121 |
-
|
122 |
-
matching_result = ""
|
|
|
123 |
if face1 is not None and face2 is not None:
|
124 |
matching_score = r.json().get('matching_score')
|
125 |
if matching_score is not None:
|
126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
|
128 |
-
return [r.json(), [face1, face2], matching_result,
|
129 |
-
liveness1, age1, gender1, emotion1, ethnicity1, mask1, eye1, facehair1, haircolor1, hairtype1, headwear1, activity1, pitch1, roll1, yaw1, quality1,
|
130 |
-
liveness2, age2, gender2, emotion2, ethnicity2, mask2, eye2, facehair2, haircolor2, hairtype2, headwear2, activity2, pitch2, roll2, yaw2, quality2]
|
131 |
|
132 |
-
|
|
|
|
|
|
|
|
|
133 |
gr.Markdown(
|
134 |
"""
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
with gr.Column(scale=1):
|
143 |
-
compare_face_input1 = gr.Image(label="Image1", type='filepath', height=270)
|
144 |
-
gr.Examples(['examples/1.jpg', 'examples/2.jpg', 'examples/3.jpg', 'examples/4.jpg'],
|
145 |
-
inputs=compare_face_input1)
|
146 |
-
compare_face_input2 = gr.Image(label="Image2", type='filepath', height=270)
|
147 |
-
gr.Examples(['examples/5.jpg', 'examples/6.jpg', 'examples/7.jpg', 'examples/8.jpg'],
|
148 |
-
inputs=compare_face_input2)
|
149 |
-
compare_face_button = gr.Button("Face Analysis & Verification", variant="primary", size="lg")
|
150 |
-
|
151 |
-
with gr.Column(scale=2):
|
152 |
-
with gr.Row():
|
153 |
-
compare_face_output = gr.Gallery(label="Faces", height=230, columns=[2], rows=[1])
|
154 |
-
with gr.Column(variant="panel"):
|
155 |
-
compare_result = gr.Markdown("")
|
156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
with gr.Row():
|
158 |
-
with gr.Column(
|
159 |
-
gr.Markdown("<b>Image 1<b/>")
|
160 |
-
liveness1 = gr.CheckboxGroup(["GENUINE", "FAKE"], label="Liveness")
|
161 |
-
age1 = gr.Number(0, label="Age")
|
162 |
-
gender1 = gr.CheckboxGroup(["MALE", "FEMALE"], label="Gender")
|
163 |
-
emotion1 = gr.CheckboxGroup(["HAPPINESS", "ANGER", "FEAR", "NEUTRAL", "SADNESS", "SURPRISE"], label="Emotion")
|
164 |
-
ethnicity1 = gr.CheckboxGroup(["ASIAN", "BLACK", "CAUCASIAN", "EAST_INDIAN"], label="Ethnicity")
|
165 |
-
mask1 = gr.CheckboxGroup(["LOWER_FACE_MASK", "FULL_FACE_MASK", "OTHER_MASK", "GLASSES", "SUNGLASSES"], label="Mask & Glasses")
|
166 |
-
eye1 = gr.CheckboxGroup(["LEFT", "RIGHT"], label="Eye Open")
|
167 |
-
facehair1 = gr.CheckboxGroup(["BEARD", "BRISTLE", "MUSTACHE", "SHAVED"], label="Facial Hair")
|
168 |
-
haircolor1 = gr.CheckboxGroup(["BLACK", "BLOND", "BROWN"], label="Hair Color")
|
169 |
-
hairtype1 = gr.CheckboxGroup(["BALD", "SHORT", "MEDIUM", "LONG"], label="Hair Type")
|
170 |
-
headwear1 = gr.CheckboxGroup(["B_CAP", "CAP", "HAT", "HELMET", "HOOD"], label="Head Wear")
|
171 |
-
activity1 = gr.CheckboxGroup(["EATING", "PHONE_RECORDING", "PHONE_USE", "SMOKING", "SEATBELT"], label="Activity")
|
172 |
-
with gr.Row():
|
173 |
-
pitch1 = gr.Number(0, label="Pitch")
|
174 |
-
roll1 = gr.Number(0, label="Roll")
|
175 |
-
yaw1 = gr.Number(0, label="Yaw")
|
176 |
-
quality1 = gr.Number(0, label="Quality")
|
177 |
-
with gr.Column(variant="panel"):
|
178 |
-
gr.Markdown("<b>Image 2<b/>")
|
179 |
-
liveness2 = gr.CheckboxGroup(["GENUINE", "FAKE"], label="Liveness")
|
180 |
-
age2 = gr.Number(0, label="Age")
|
181 |
-
gender2 = gr.CheckboxGroup(["MALE", "FEMALE"], label="Gender")
|
182 |
-
emotion2 = gr.CheckboxGroup(["HAPPINESS", "ANGER", "FEAR", "NEUTRAL", "SADNESS", "SURPRISE"], label="Emotion")
|
183 |
-
ethnicity2 = gr.CheckboxGroup(["ASIAN", "BLACK", "CAUCASIAN", "EAST_INDIAN"], label="Ethnicity")
|
184 |
-
mask2 = gr.CheckboxGroup(["LOWER_FACE_MASK", "FULL_FACE_MASK", "OTHER_MASK", "GLASSES", "SUNGLASSES"], label="Mask & Glasses")
|
185 |
-
eye2 = gr.CheckboxGroup(["LEFT", "RIGHT"], label="Eye Open")
|
186 |
-
facehair2 = gr.CheckboxGroup(["BEARD", "BRISTLE", "MUSTACHE", "SHAVED"], label="Facial Hair")
|
187 |
-
haircolor2 = gr.CheckboxGroup(["BLACK", "BLOND", "BROWN"], label="Hair Color")
|
188 |
-
hairtype2 = gr.CheckboxGroup(["BALD", "SHORT", "MEDIUM", "LONG"], label="Hair Type")
|
189 |
-
headwear2 = gr.CheckboxGroup(["B_CAP", "CAP", "HAT", "HELMET", "HOOD"], label="Head Wear")
|
190 |
-
activity2 = gr.CheckboxGroup(["EATING", "PHONE_RECORDING", "PHONE_USE", "SMOKING", "SEATBELT"], label="Activity")
|
191 |
with gr.Row():
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
|
197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
|
199 |
-
|
200 |
-
|
201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
202 |
|
203 |
gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FRecognito%2FFaceAnalysis"><img src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FRecognito%2FFaceAnalysis&countColor=%2337d67a&style=flat&labelStyle=upper" /></a>')
|
204 |
-
|
205 |
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False)
|
|
|
4 |
import json
|
5 |
from PIL import Image
|
6 |
|
7 |
+
css = """
|
8 |
+
.example-image img{
|
9 |
+
display: flex; /* Use flexbox to align items */
|
10 |
+
justify-content: center; /* Center the image horizontally */
|
11 |
+
align-items: center; /* Center the image vertically */
|
12 |
+
height: 300px; /* Set the height of the container */
|
13 |
+
object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */
|
14 |
+
}
|
15 |
+
|
16 |
+
.example-image{
|
17 |
+
display: flex; /* Use flexbox to align items */
|
18 |
+
justify-content: center; /* Center the image horizontally */
|
19 |
+
align-items: center; /* Center the image vertically */
|
20 |
+
height: 350px; /* Set the height of the container */
|
21 |
+
object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */
|
22 |
+
}
|
23 |
+
|
24 |
+
.face-row {
|
25 |
+
display: flex;
|
26 |
+
justify-content: space-around; /* Distribute space evenly between elements */
|
27 |
+
align-items: center; /* Align items vertically */
|
28 |
+
width: 100%; /* Set the width of the row to 100% */
|
29 |
+
}
|
30 |
+
|
31 |
+
.face-image{
|
32 |
+
justify-content: center; /* Center the image horizontally */
|
33 |
+
align-items: center; /* Center the image vertically */
|
34 |
+
height: 160px; /* Set the height of the container */
|
35 |
+
width: 160px;
|
36 |
+
object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */
|
37 |
+
}
|
38 |
+
|
39 |
+
.face-image img{
|
40 |
+
justify-content: center; /* Center the image horizontally */
|
41 |
+
align-items: center; /* Center the image vertically */
|
42 |
+
height: 160px; /* Set the height of the container */
|
43 |
+
object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */
|
44 |
+
}
|
45 |
+
|
46 |
+
.markdown-success-container {
|
47 |
+
background-color: #F6FFED;
|
48 |
+
padding: 20px;
|
49 |
+
margin: 20px;
|
50 |
+
border-radius: 1px;
|
51 |
+
border: 2px solid green;
|
52 |
+
text-align: center;
|
53 |
+
}
|
54 |
+
|
55 |
+
.markdown-fail-container {
|
56 |
+
background-color: #FFF1F0;
|
57 |
+
padding: 20px;
|
58 |
+
margin: 20px;
|
59 |
+
border-radius: 1px;
|
60 |
+
border: 2px solid red;
|
61 |
+
text-align: center;
|
62 |
+
}
|
63 |
+
|
64 |
+
.markdown-attribute-container {
|
65 |
+
display: flex;
|
66 |
+
justify-content: space-around; /* Distribute space evenly between elements */
|
67 |
+
align-items: center; /* Align items vertically */
|
68 |
+
padding: 10px;
|
69 |
+
margin: 10px;
|
70 |
+
}
|
71 |
+
|
72 |
+
.block-background {
|
73 |
+
# background-color: #202020; /* Set your desired background color */
|
74 |
+
border-radius: 5px;
|
75 |
+
}
|
76 |
+
|
77 |
+
"""
|
78 |
+
|
79 |
+
def convert_fun(input_str):
|
80 |
+
# Remove line breaks and extra whitespaces
|
81 |
+
return ' '.join(input_str.split())
|
82 |
+
|
83 |
def get_attributes(json):
|
84 |
+
liveness_thr = 0.5
|
85 |
+
liveness = "GENUINE" if json.get('liveness') >= liveness_thr else "FAKE"
|
86 |
attr = json.get('attribute')
|
87 |
age = attr.get('age')
|
88 |
gender = attr.get('gender')
|
89 |
emotion = attr.get('emotion')
|
90 |
ethnicity = attr.get('ethnicity')
|
91 |
|
92 |
+
mask = attr.get('face_mask')
|
93 |
+
glass = 'No Glasses'
|
94 |
if attr.get('glasses') == 'USUAL':
|
95 |
+
glass = 'Glasses'
|
96 |
if attr.get('glasses') == 'DARK':
|
97 |
+
glass = 'Sunglasses'
|
98 |
|
99 |
+
open_eye_thr = 0.3
|
100 |
+
left_eye = 'Close'
|
101 |
+
if attr.get('eye_left') >= open_eye_thr:
|
102 |
+
left_eye = 'Open'
|
103 |
+
|
104 |
+
right_eye = 'Close'
|
105 |
+
if attr.get('eye_right') >= open_eye_thr:
|
106 |
+
right_eye = 'Open'
|
107 |
+
|
108 |
facehair = attr.get('facial_hair')
|
109 |
haircolor = attr.get('hair_color')
|
110 |
hairtype = attr.get('hair_type')
|
111 |
headwear = attr.get('headwear')
|
112 |
|
113 |
+
eating = 'No'
|
114 |
+
eat_thr = 0.5
|
115 |
+
if attr.get('food_consumption') >= eat_thr:
|
116 |
+
eating = 'Yes'
|
117 |
+
|
118 |
+
phone_record_thr = 0.5
|
119 |
+
phone_recording = 'No'
|
120 |
+
if attr.get('phone_recording') >= phone_record_thr:
|
121 |
+
phone_recording = 'Yes'
|
122 |
+
|
123 |
+
phone_use_thr = 0.5
|
124 |
+
phone_use = 'No'
|
125 |
+
if attr.get('phone_use') >= phone_use_thr:
|
126 |
+
phone_use = 'Yes'
|
127 |
+
|
128 |
+
seatbelt = 'No'
|
129 |
+
seatbelt_thr = 0.5
|
130 |
+
if attr.get('seatbelt') >= seatbelt_thr:
|
131 |
+
seatbelt = 'Yes'
|
132 |
+
|
133 |
+
smoking = 'No'
|
134 |
+
smoking_thr = 0.5
|
135 |
+
if attr.get('smoking') >= smoking_thr:
|
136 |
+
smoking = 'Yes'
|
137 |
|
138 |
pitch = attr.get('pitch')
|
139 |
roll = attr.get('roll')
|
140 |
yaw = attr.get('yaw')
|
141 |
quality = attr.get('quality')
|
142 |
+
|
143 |
+
attribute = f"""
|
144 |
+
<br/>
|
145 |
+
<div class="markdown-attribute-container">
|
146 |
+
<table>
|
147 |
+
<tr>
|
148 |
+
<th style="text-align: center;">Attribute</th>
|
149 |
+
<th style="text-align: center;">Result</th>
|
150 |
+
<th style="text-align: center;">Score</th>
|
151 |
+
<th style="text-align: center;">Threshold</th>
|
152 |
+
</tr>
|
153 |
+
<tr>
|
154 |
+
<td>Liveness</td>
|
155 |
+
<td>{liveness}</td>
|
156 |
+
<td>{"{:.4f}".format(json.get('liveness'))}</td>
|
157 |
+
<td>{liveness_thr}</td>
|
158 |
+
</tr>
|
159 |
+
<tr>
|
160 |
+
<td>Gender</td>
|
161 |
+
<td>{gender}</td>
|
162 |
+
<td></td><td></td>
|
163 |
+
</tr>
|
164 |
+
<tr>
|
165 |
+
<td>Left Eye</td>
|
166 |
+
<td>{left_eye}</td>
|
167 |
+
<td>{"{:.4f}".format(attr.get('eye_left'))}</td>
|
168 |
+
<td>{open_eye_thr}</td>
|
169 |
+
</tr>
|
170 |
+
<tr>
|
171 |
+
<td>Right Eye</td>
|
172 |
+
<td>{right_eye}</td>
|
173 |
+
<td>{"{:.4f}".format(attr.get('eye_right'))}</td>
|
174 |
+
<td>{open_eye_thr}</td>
|
175 |
+
</tr>
|
176 |
+
<tr>
|
177 |
+
<td>Age</td>
|
178 |
+
<td>{int(age)}</td>
|
179 |
+
<td></td><td></td>
|
180 |
+
</tr>
|
181 |
+
<tr>
|
182 |
+
<td>Pitch</td>
|
183 |
+
<td>{"{:.4f}".format(pitch)}</td>
|
184 |
+
<td></td><td></td>
|
185 |
+
</tr>
|
186 |
+
<tr>
|
187 |
+
<td>Yaw</td>
|
188 |
+
<td>{"{:.4f}".format(yaw)}</td>
|
189 |
+
<td></td><td></td>
|
190 |
+
</tr>
|
191 |
+
<tr>
|
192 |
+
<td>Roll</td>
|
193 |
+
<td>{"{:.4f}".format(roll)}</td>
|
194 |
+
<td></td><td></td>
|
195 |
+
</tr>
|
196 |
+
<tr>
|
197 |
+
<td>Emotion</td>
|
198 |
+
<td>{emotion}</td>
|
199 |
+
<td></td><td></td>
|
200 |
+
</tr>
|
201 |
+
<tr>
|
202 |
+
<td>Mask</td>
|
203 |
+
<td>{mask}</td>
|
204 |
+
<td></td><td></td>
|
205 |
+
</tr>
|
206 |
+
<tr>
|
207 |
+
<td>Glass</td>
|
208 |
+
<td>{glass}</td>
|
209 |
+
<td></td><td></td>
|
210 |
+
</tr>
|
211 |
+
<tr>
|
212 |
+
<td>FaceHair</td>
|
213 |
+
<td>{facehair}</td>
|
214 |
+
<td></td><td></td>
|
215 |
+
</tr>
|
216 |
+
<tr>
|
217 |
+
<td>HairColor</td>
|
218 |
+
<td>{haircolor}</td>
|
219 |
+
<td></td><td></td>
|
220 |
+
</tr>
|
221 |
+
<tr>
|
222 |
+
<td>HairType</td>
|
223 |
+
<td>{hairtype}</td>
|
224 |
+
<td></td><td></td>
|
225 |
+
</tr>
|
226 |
+
<tr>
|
227 |
+
<td>HeadWear</td>
|
228 |
+
<td>{headwear}</td>
|
229 |
+
<td></td><td></td>
|
230 |
+
</tr>
|
231 |
+
<tr>
|
232 |
+
<td>Eating</td>
|
233 |
+
<td>{eating}</td>
|
234 |
+
<td>{"{:.4f}".format(attr.get('food_consumption'))}</td>
|
235 |
+
<td>{eat_thr}</td>
|
236 |
+
</tr>
|
237 |
+
<tr>
|
238 |
+
<td>Phone Use</td>
|
239 |
+
<td>{phone_use}</td>
|
240 |
+
<td>{"{:.4f}".format(attr.get('phone_use'))}</td>
|
241 |
+
<td>{phone_use_thr}</td>
|
242 |
+
</tr>
|
243 |
+
<tr>
|
244 |
+
<td>Smoking</td>
|
245 |
+
<td>{smoking}</td>
|
246 |
+
<td>{"{:.4f}".format(attr.get('smoking'))}</td>
|
247 |
+
<td>{smoking_thr}</td>
|
248 |
+
</tr>
|
249 |
+
<tr>
|
250 |
+
<td>Image Quality</td>
|
251 |
+
<td>{"{:.4f}".format(quality)}</td>
|
252 |
+
<td></td><td></td>
|
253 |
+
</tr>
|
254 |
+
</table>
|
255 |
+
</div>
|
256 |
+
"""
|
257 |
+
one_line_attribute = convert_fun(attribute)
|
258 |
+
if liveness == 'GENUINE':
|
259 |
+
liveness_result = f"""<br/><div class="markdown-success-container"><p style="text-align: center; font-size: 20px; color: green;">Liveness Check: GENUINE</p></div>"""
|
260 |
+
else:
|
261 |
+
liveness_result = f"""<br/><div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check: FAKE</p></div>"""
|
262 |
+
|
263 |
+
return liveness_result, one_line_attribute
|
264 |
+
|
265 |
+
def analyze_face(frame):
|
266 |
+
url = "https://recognito.p.rapidapi.com/api/analyze_face"
|
267 |
+
try:
|
268 |
+
files = {'image': open(frame, 'rb')}
|
269 |
+
headers = {"X-RapidAPI-Key": os.environ.get("API_KEY")}
|
270 |
+
|
271 |
+
r = requests.post(url=url, files=files, headers=headers)
|
272 |
+
except:
|
273 |
+
raise gr.Error("Please select images files!")
|
274 |
+
|
275 |
+
faces = None
|
276 |
+
|
277 |
+
try:
|
278 |
+
image = Image.open(frame)
|
279 |
+
|
280 |
+
face = Image.new('RGBA',(150, 150), (80,80,80,0))
|
281 |
+
|
282 |
+
liveness, age, gender, emotion, ethnicity, mask, eye, facehair, haircolor, hairtype, headwear, activity, pitch, roll, yaw, quality = [None] * 16
|
283 |
+
|
284 |
+
res = r.json().get('image')
|
285 |
+
if res is not None and res:
|
286 |
+
face = res.get('detection')
|
287 |
+
x1 = face.get('x')
|
288 |
+
y1 = face.get('y')
|
289 |
+
x2 = x1 + face.get('w')
|
290 |
+
y2 = y1 + face.get('h')
|
291 |
+
|
292 |
+
if x1 < 0:
|
293 |
+
x1 = 0
|
294 |
+
if y1 < 0:
|
295 |
+
y1 = 0
|
296 |
+
if x2 >= image.width:
|
297 |
+
x2 = image.width - 1
|
298 |
+
if y2 >= image.height:
|
299 |
+
y2 = image.height - 1
|
300 |
+
|
301 |
+
face_crop = image.crop((x1, y1, x2, y2))
|
302 |
+
face_image_ratio = face_crop.width / float(face_crop.height)
|
303 |
+
resized_w = int(face_image_ratio * 150)
|
304 |
+
resized_h = 150
|
305 |
+
|
306 |
+
face_crop = face_crop.resize((int(resized_w), int(resized_h)))
|
307 |
+
liveness, attribute = get_attributes(res)
|
308 |
+
except:
|
309 |
+
pass
|
310 |
+
|
311 |
+
return [face_crop, liveness, attribute]
|
312 |
+
|
313 |
|
314 |
def compare_face(frame1, frame2):
|
315 |
+
url = "https://recognito.p.rapidapi.com/api/compare_face"
|
316 |
try:
|
317 |
files = {'image1': open(frame1, 'rb'), 'image2': open(frame2, 'rb')}
|
318 |
headers = {"X-RapidAPI-Key": os.environ.get("API_KEY")}
|
|
|
330 |
face1 = Image.new('RGBA',(150, 150), (80,80,80,0))
|
331 |
face2 = Image.new('RGBA',(150, 150), (80,80,80,0))
|
332 |
|
|
|
|
|
333 |
res1 = r.json().get('image1')
|
334 |
|
335 |
if res1 is not None and res1:
|
|
|
353 |
resized_h = 150
|
354 |
|
355 |
face1 = face1.resize((int(resized_w), int(resized_h)))
|
|
|
356 |
|
357 |
res2 = r.json().get('image2')
|
358 |
if res2 is not None and res2:
|
|
|
377 |
resized_h = 150
|
378 |
|
379 |
face2 = face2.resize((int(resized_w), int(resized_h)))
|
|
|
380 |
except:
|
381 |
pass
|
382 |
+
|
383 |
+
matching_result = Image.open("icons/blank.png")
|
384 |
+
similarity_score = ""
|
385 |
if face1 is not None and face2 is not None:
|
386 |
matching_score = r.json().get('matching_score')
|
387 |
if matching_score is not None:
|
388 |
+
str_score = str("{:.4f}".format(matching_score))
|
389 |
+
if matching_score >= 0.7:
|
390 |
+
matching_result = Image.open("icons/same.png")
|
391 |
+
similarity_score = f"""<br/><div class="markdown-success-container"><p style="text-align: center; font-size: 20px; color: green;">Similarity score: {str_score}</p></div>"""
|
392 |
+
else:
|
393 |
+
matching_result = Image.open("icons/different.png")
|
394 |
+
similarity_score = f"""<br/><div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Similarity score: {str_score}</p></div>"""
|
395 |
+
|
396 |
+
return [face1, face2, matching_result, similarity_score]
|
397 |
|
|
|
|
|
|
|
398 |
|
399 |
+
def image_change_callback(image_data):
|
400 |
+
# This function will be called whenever a new image is set for the gr.Image component
|
401 |
+
print("New image set:", image_data)
|
402 |
+
|
403 |
+
with gr.Blocks(css=css) as demo:
|
404 |
gr.Markdown(
|
405 |
"""
|
406 |
+
<a href="https://recognito.vision" style="display: flex; align-items: center;">
|
407 |
+
<img src="https://recognito.vision/wp-content/uploads/2024/03/Recognito-modified.png" style="width: 8%; margin-right: 15px;"/>
|
408 |
+
<div>
|
409 |
+
<p style="font-size: 32px; font-weight: bold; margin: 0;">Recognito</p>
|
410 |
+
<p style="font-size: 18px; margin: 0;">www.recognito.vision</p>
|
411 |
+
</div>
|
412 |
+
</a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
413 |
|
414 |
+
<p style="font-size: 20px; font-weight: bold;">β¨ NIST FRVT Top #1 Face Recognition Algorithm Developer</p>
|
415 |
+
<div style="display: flex; align-items: center;">
|
416 |
+
  <a href="https://pages.nist.gov/frvt/html/frvt11.html"> <p style="font-size: 14px;">ππ» Latest NIST FRVT Report</p></a>
|
417 |
+
</div>
|
418 |
+
<p style="font-size: 20px; font-weight: bold;">π€ Contact us for our on-premise SDKs deployment</p>
|
419 |
+
</div><div style="display: flex; align-items: center;">
|
420 |
+
  <a target="_blank" href="mailto:[email protected]"><img src="https://img.shields.io/badge/[email protected]?logo=gmail " alt="www.recognito.vision"></a>
|
421 |
+
<a target="_blank" href="https://wa.me/+14158003112"><img src="https://img.shields.io/badge/whatsapp-recognito-blue.svg?logo=whatsapp " alt="www.recognito.vision"></a>
|
422 |
+
<a target="_blank" href="https://t.me/recognito_vision"><img src="https://img.shields.io/badge/[email protected]?logo=telegram " alt="www.recognito.vision"></a>
|
423 |
+
<a target="_blank" href="https://join.slack.com/t/recognito-workspace/shared_invite/zt-2d4kscqgn-"><img src="https://img.shields.io/badge/slack-recognito-blue.svg?logo=slack " alt="www.recognito.vision"></a>
|
424 |
+
</div/
|
425 |
+
"""
|
426 |
+
)
|
427 |
+
|
428 |
+
with gr.Tabs():
|
429 |
+
with gr.Tab("Face Recognition"):
|
430 |
with gr.Row():
|
431 |
+
with gr.Column(scale=2):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
432 |
with gr.Row():
|
433 |
+
with gr.Column(scale=1):
|
434 |
+
compare_face_input1 = gr.Image(label="Image1", type='filepath', elem_classes="example-image")
|
435 |
+
gr.Examples(['examples/1.jpg', 'examples/2.jpg', 'examples/3.jpg', 'examples/4.jpg'],
|
436 |
+
inputs=compare_face_input1)
|
437 |
+
with gr.Column(scale=1):
|
438 |
+
compare_face_input2 = gr.Image(label="Image2", type='filepath', elem_classes="example-image")
|
439 |
+
gr.Examples(['examples/5.jpg', 'examples/6.jpg', 'examples/7.jpg', 'examples/8.jpg'],
|
440 |
+
inputs=compare_face_input2)
|
441 |
+
|
442 |
+
with gr.Blocks():
|
443 |
+
with gr.Column(scale=1, min_width=400, elem_classes="block-background"):
|
444 |
+
compare_face_button = gr.Button("Compare Face", variant="primary", size="lg")
|
445 |
+
with gr.Row(elem_classes="face-row"):
|
446 |
+
face_output1 = gr.Image(value="icons/face.jpg", label="Face 1", scale=0, elem_classes="face-image")
|
447 |
+
compare_result = gr.Image(value="icons/blank.png", min_width=30, scale=0, show_download_button=False, show_label=False)
|
448 |
+
face_output2 = gr.Image(value="icons/face.jpg", label="Face 2", scale=0, elem_classes="face-image")
|
449 |
+
similarity_markdown = gr.Markdown("")
|
450 |
|
451 |
+
compare_face_button.click(compare_face, inputs=[compare_face_input1, compare_face_input2], outputs=[face_output1, face_output2, compare_result, similarity_markdown])
|
452 |
+
|
453 |
+
with gr.Tab("Face Analysis"):
|
454 |
+
with gr.Row():
|
455 |
+
with gr.Column(scale=1):
|
456 |
+
face_input = gr.Image(label="Image", type='filepath', elem_classes="example-image")
|
457 |
+
gr.Examples(['examples/1.jpg', 'examples/2.jpg', 'examples/3.jpg', 'examples/4.jpg', 'examples/5.jpg', 'examples/6.jpg', 'examples/7.jpg', 'examples/8.jpg'],
|
458 |
+
inputs=face_input)
|
459 |
|
460 |
+
with gr.Blocks():
|
461 |
+
with gr.Column(scale=1, elem_classes="block-background"):
|
462 |
+
analyze_face_button = gr.Button("Analyze Face", variant="primary", size="lg")
|
463 |
+
with gr.Row(elem_classes="face-row"):
|
464 |
+
face_output = gr.Image(value="icons/face.jpg", label="Face", scale=0, elem_classes="face-image")
|
465 |
+
|
466 |
+
liveness_result = gr.Markdown("")
|
467 |
+
attribute_result = gr.Markdown("")
|
468 |
+
|
469 |
+
analyze_face_button.click(analyze_face, inputs=face_input, outputs=[face_output, liveness_result, attribute_result])
|
470 |
|
471 |
gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FRecognito%2FFaceAnalysis"><img src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FRecognito%2FFaceAnalysis&countColor=%2337d67a&style=flat&labelStyle=upper" /></a>')
|
|
|
472 |
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False)
|