Spaces:
Sleeping
Sleeping
Update gradio/app.py
Browse files- gradio/app.py +291 -271
gradio/app.py
CHANGED
|
@@ -1,271 +1,291 @@
|
|
| 1 |
-
import sys
|
| 2 |
-
sys.path.append('../')
|
| 3 |
-
|
| 4 |
-
import os
|
| 5 |
-
import gradio as gr
|
| 6 |
-
import cv2
|
| 7 |
-
import time
|
| 8 |
-
import numpy as np
|
| 9 |
-
from PIL import Image
|
| 10 |
-
|
| 11 |
-
from engine.header import *
|
| 12 |
-
|
| 13 |
-
file_path = os.path.abspath(__file__)
|
| 14 |
-
gradio_path = os.path.dirname(file_path)
|
| 15 |
-
root_path = os.path.dirname(gradio_path)
|
| 16 |
-
|
| 17 |
-
version = get_version().decode('utf-8')
|
| 18 |
-
print_info('\t <Recognito Face Recognition> \t version {}'.format(version))
|
| 19 |
-
|
| 20 |
-
device_id = get_deviceid().decode('utf-8')
|
| 21 |
-
print_info('\t <Hardware ID> \t\t {}'.format(device_id))
|
| 22 |
-
|
| 23 |
-
g_activation_result = -1
|
| 24 |
-
MATCH_THRESHOLD = 0.67
|
| 25 |
-
|
| 26 |
-
css = """
|
| 27 |
-
.example-image img{
|
| 28 |
-
display: flex; /* Use flexbox to align items */
|
| 29 |
-
justify-content: center; /* Center the image horizontally */
|
| 30 |
-
align-items: center; /* Center the image vertically */
|
| 31 |
-
height: 300px; /* Set the height of the container */
|
| 32 |
-
object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */
|
| 33 |
-
}
|
| 34 |
-
|
| 35 |
-
.example-image{
|
| 36 |
-
display: flex; /* Use flexbox to align items */
|
| 37 |
-
justify-content: center; /* Center the image horizontally */
|
| 38 |
-
align-items: center; /* Center the image vertically */
|
| 39 |
-
height: 350px; /* Set the height of the container */
|
| 40 |
-
object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */
|
| 41 |
-
}
|
| 42 |
-
|
| 43 |
-
.face-row {
|
| 44 |
-
display: flex;
|
| 45 |
-
justify-content: space-around; /* Distribute space evenly between elements */
|
| 46 |
-
align-items: center; /* Align items vertically */
|
| 47 |
-
width: 100%; /* Set the width of the row to 100% */
|
| 48 |
-
}
|
| 49 |
-
|
| 50 |
-
.face-image{
|
| 51 |
-
justify-content: center; /* Center the image horizontally */
|
| 52 |
-
align-items: center; /* Center the image vertically */
|
| 53 |
-
height: 160px; /* Set the height of the container */
|
| 54 |
-
width: 160px;
|
| 55 |
-
object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */
|
| 56 |
-
}
|
| 57 |
-
|
| 58 |
-
.face-image img{
|
| 59 |
-
justify-content: center; /* Center the image horizontally */
|
| 60 |
-
align-items: center; /* Center the image vertically */
|
| 61 |
-
height: 160px; /* Set the height of the container */
|
| 62 |
-
object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */
|
| 63 |
-
}
|
| 64 |
-
|
| 65 |
-
.markdown-success-container {
|
| 66 |
-
background-color: #F6FFED;
|
| 67 |
-
padding: 20px;
|
| 68 |
-
margin: 20px;
|
| 69 |
-
border-radius: 1px;
|
| 70 |
-
border: 2px solid green;
|
| 71 |
-
text-align: center;
|
| 72 |
-
}
|
| 73 |
-
|
| 74 |
-
.markdown-fail-container {
|
| 75 |
-
background-color: #FFF1F0;
|
| 76 |
-
padding: 20px;
|
| 77 |
-
margin: 20px;
|
| 78 |
-
border-radius: 1px;
|
| 79 |
-
border: 2px solid red;
|
| 80 |
-
text-align: center;
|
| 81 |
-
}
|
| 82 |
-
|
| 83 |
-
.block-background {
|
| 84 |
-
# background-color: #202020; /* Set your desired background color */
|
| 85 |
-
border-radius: 5px;
|
| 86 |
-
}
|
| 87 |
-
|
| 88 |
-
"""
|
| 89 |
-
|
| 90 |
-
def activate_sdk():
|
| 91 |
-
online_key = os.environ.get("FR_LICENSE_KEY")
|
| 92 |
-
offline_key_path = os.path.join(root_path, "license.txt")
|
| 93 |
-
dict_path = os.path.join(root_path, "engine/bin")
|
| 94 |
-
|
| 95 |
-
ret = -1
|
| 96 |
-
if online_key is None:
|
| 97 |
-
print_warning("Recognition online license key not found!")
|
| 98 |
-
else:
|
| 99 |
-
print_info(f"FR_LICENSE_KEY: {online_key}")
|
| 100 |
-
ret = init_sdk(dict_path.encode('utf-8'), online_key.encode('utf-8'))
|
| 101 |
-
|
| 102 |
-
if ret == 0:
|
| 103 |
-
print_log("Successfully online init SDK!")
|
| 104 |
-
else:
|
| 105 |
-
print_error(f"Failed to online init SDK, Error code {ret}\n Trying offline init SDK...");
|
| 106 |
-
if os.path.exists(offline_key_path) is False:
|
| 107 |
-
print_warning("Recognition offline license key file not found!")
|
| 108 |
-
print_error(f"Falied to offline init SDK, Error code {ret}")
|
| 109 |
-
return ret
|
| 110 |
-
else:
|
| 111 |
-
ret = init_sdk_offline(dict_path.encode('utf-8'), offline_key_path.encode('utf-8'))
|
| 112 |
-
if ret == 0:
|
| 113 |
-
print_log("Successfully offline init SDK!")
|
| 114 |
-
else:
|
| 115 |
-
print_error(f"Falied to offline init SDK, Error code {ret}")
|
| 116 |
-
return ret
|
| 117 |
-
|
| 118 |
-
return ret
|
| 119 |
-
|
| 120 |
-
def compare_face_clicked(frame1, frame2, threshold):
|
| 121 |
-
global g_activation_result
|
| 122 |
-
if g_activation_result != 0:
|
| 123 |
-
gr.Warning("SDK Activation Failed!")
|
| 124 |
-
return None, None, None, None, None, None, None, None, None
|
| 125 |
-
|
| 126 |
-
try:
|
| 127 |
-
image1 = open(frame1, 'rb')
|
| 128 |
-
image2 = open(frame2, 'rb')
|
| 129 |
-
except:
|
| 130 |
-
raise gr.Error("Please select images files!")
|
| 131 |
-
|
| 132 |
-
image_mat1 = cv2.imdecode(np.frombuffer(image1.read(), np.uint8), cv2.IMREAD_COLOR)
|
| 133 |
-
image_mat2 = cv2.imdecode(np.frombuffer(image2.read(), np.uint8), cv2.IMREAD_COLOR)
|
| 134 |
-
start_time = time.time()
|
| 135 |
-
result, score, face_bboxes, face_features = compare_face(image_mat1, image_mat2, float(threshold))
|
| 136 |
-
end_time = time.time()
|
| 137 |
-
process_time = (end_time - start_time) * 1000
|
| 138 |
-
|
| 139 |
-
try:
|
| 140 |
-
image1 = Image.open(frame1)
|
| 141 |
-
image2 = Image.open(frame2)
|
| 142 |
-
images = [image1, image2]
|
| 143 |
-
|
| 144 |
-
face1 = Image.new('RGBA',(150, 150), (80,80,80,0))
|
| 145 |
-
face2 = Image.new('RGBA',(150, 150), (80,80,80,0))
|
| 146 |
-
faces = [face1, face2]
|
| 147 |
-
|
| 148 |
-
face_bboxes_result = []
|
| 149 |
-
if face_bboxes is not None:
|
| 150 |
-
for i, bbox in enumerate(face_bboxes):
|
| 151 |
-
x1 = bbox[0]
|
| 152 |
-
y1 = bbox[1]
|
| 153 |
-
x2 = bbox[2]
|
| 154 |
-
y2 = bbox[3]
|
| 155 |
-
if x1 < 0:
|
| 156 |
-
x1 = 0
|
| 157 |
-
if y1 < 0:
|
| 158 |
-
y1 = 0
|
| 159 |
-
if x2 >= images[i].width:
|
| 160 |
-
x2 = images[i].width - 1
|
| 161 |
-
if y2 >= images[i].height:
|
| 162 |
-
y2 = images[i].height - 1
|
| 163 |
-
|
| 164 |
-
face_bbox_str = f"x1: {x1}, y1: {y1}, x2: {x2}, y2: {y2}"
|
| 165 |
-
face_bboxes_result.append(face_bbox_str)
|
| 166 |
-
|
| 167 |
-
faces[i] = images[i].crop((x1, y1, x2, y2))
|
| 168 |
-
face_image_ratio = faces[i].width / float(faces[i].height)
|
| 169 |
-
resized_w = int(face_image_ratio * 150)
|
| 170 |
-
resized_h = 150
|
| 171 |
-
|
| 172 |
-
faces[i] = faces[i].resize((int(resized_w), int(resized_h)))
|
| 173 |
-
except:
|
| 174 |
-
pass
|
| 175 |
-
|
| 176 |
-
matching_result = Image.open(os.path.join(gradio_path, "icons/blank.png"))
|
| 177 |
-
similarity_score = ""
|
| 178 |
-
if faces[0] is not None and faces[1] is not None:
|
| 179 |
-
if score is not None:
|
| 180 |
-
str_score = str("{:.4f}".format(score))
|
| 181 |
-
if result == "SAME PERSON":
|
| 182 |
-
matching_result = Image.open(os.path.join(gradio_path, "icons/same.png"))
|
| 183 |
-
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>"""
|
| 184 |
-
else:
|
| 185 |
-
matching_result = Image.open(os.path.join(gradio_path, "icons/different.png"))
|
| 186 |
-
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>"""
|
| 187 |
-
|
| 188 |
-
return faces[0], faces[1], matching_result, similarity_score, face_bboxes_result[0], face_bboxes_result[1], face_features[0], face_features[1], str(process_time)
|
| 189 |
-
|
| 190 |
-
def launch_demo(activate_result):
|
| 191 |
-
with gr.Blocks(css=css) as demo:
|
| 192 |
-
gr.Markdown(
|
| 193 |
-
f"""
|
| 194 |
-
<a href="https://recognito.vision" style="display: flex; align-items: center;">
|
| 195 |
-
<img src="https://recognito.vision/wp-content/uploads/2024/03/Recognito-modified.png" style="width: 3%; margin-right: 15px;"/>
|
| 196 |
-
</a>
|
| 197 |
-
<div style="display: flex; align-items: center;justify-content: center;">
|
| 198 |
-
<p style="font-size: 36px; font-weight: bold;">Face Recognition {version}</p>
|
| 199 |
-
</div>
|
| 200 |
-
<p style="font-size: 20px; font-weight: bold;">🤝 Contact us for our on-premise Face Recognition, Liveness Detection SDKs deployment</p>
|
| 201 |
-
</div>
|
| 202 |
-
<div style="display: flex; align-items: center;">
|
| 203 |
-
  <a target="_blank" href="mailto:hello@recognito.vision"><img src="https://img.shields.io/badge/email-hello@recognito.vision-blue.svg?logo=gmail " alt="www.recognito.vision"></a>
|
| 204 |
-
<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>
|
| 205 |
-
<a target="_blank" href="https://t.me/recognito_vision"><img src="https://img.shields.io/badge/telegram-@recognito-blue.svg?logo=telegram " alt="www.recognito.vision"></a>
|
| 206 |
-
<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>
|
| 207 |
-
</div>
|
| 208 |
-
<br/>
|
| 209 |
-
<div style="display: flex; align-items: center;">
|
| 210 |
-
  <a href="https://recognito.vision" style="display: flex; align-items: center;"><img src="https://recognito.vision/wp-content/uploads/2024/03/recognito_64.png" style="width: 24px; margin-right: 5px;"/></a>
|
| 211 |
-
<a href="https://www.linkedin.com/company/recognito-vision" style="display: flex; align-items: center;"><img src="https://recognito.vision/wp-content/uploads/2024/03/linkedin64.png" style="width: 24px; margin-right: 5px;"/></a>
|
| 212 |
-
<a href="https://huggingface.co/Recognito" style="display: flex; align-items: center;"><img src="https://recognito.vision/wp-content/uploads/2024/03/hf1_64.png" style="width: 24px; margin-right: 5px;"/></a>
|
| 213 |
-
<a href="https://github.com/Recognito-Vision" style="display: flex; align-items: center;"><img src="https://recognito.vision/wp-content/uploads/2024/03/github64.png" style="width: 24px; margin-right: 5px;"/></a>
|
| 214 |
-
<a href="https://hub.docker.com/u/recognito" style="display: flex; align-items: center;"><img src="https://recognito.vision/wp-content/uploads/2024/03/docker64.png" style="width: 24px; margin-right: 5px;"/></a>
|
| 215 |
-
</div>
|
| 216 |
-
<br/>
|
| 217 |
-
"""
|
| 218 |
-
)
|
| 219 |
-
|
| 220 |
-
with gr.Group():
|
| 221 |
-
if activate_result == 0:
|
| 222 |
-
gr.Markdown("""<p style="text-align: left; font-size: 20px; color: green;"> Activation Success!</p>""")
|
| 223 |
-
else:
|
| 224 |
-
gr.Markdown("""<p style="text-align: left; font-size: 20px; color: red;"> Activation Failed!</p>""")
|
| 225 |
-
|
| 226 |
-
gr.Textbox(device_id, label="Hardware ID")
|
| 227 |
-
|
| 228 |
-
with gr.Row():
|
| 229 |
-
with gr.Column(scale=2):
|
| 230 |
-
with gr.Row():
|
| 231 |
-
with gr.Column(scale=1):
|
| 232 |
-
compare_face_input1 = gr.Image(label="Image1", type='filepath', elem_classes="example-image")
|
| 233 |
-
gr.Examples([os.path.join(root_path,'examples/1.jpg'),
|
| 234 |
-
os.path.join(root_path,'examples/2.jpg'),
|
| 235 |
-
os.path.join(root_path,'examples/3.jpg'),
|
| 236 |
-
os.path.join(root_path,'examples/4.jpg')],
|
| 237 |
-
inputs=compare_face_input1)
|
| 238 |
-
with gr.Column(scale=1):
|
| 239 |
-
compare_face_input2 = gr.Image(label="Image2", type='filepath', elem_classes="example-image")
|
| 240 |
-
gr.Examples([os.path.join(root_path,'examples/5.jpg'),
|
| 241 |
-
os.path.join(root_path,'examples/6.jpg'),
|
| 242 |
-
os.path.join(root_path,'examples/7.jpg'),
|
| 243 |
-
os.path.join(root_path,'examples/8.jpg')],
|
| 244 |
-
inputs=compare_face_input2)
|
| 245 |
-
|
| 246 |
-
with gr.Blocks():
|
| 247 |
-
with gr.Column(scale=1, min_width=400, elem_classes="block-background"):
|
| 248 |
-
txt_threshold = gr.Textbox(f"{MATCH_THRESHOLD}", label="Matching Threshold", interactive=True)
|
| 249 |
-
compare_face_button = gr.Button("Compare Face", variant="primary", size="lg")
|
| 250 |
-
with gr.Row(elem_classes="face-row"):
|
| 251 |
-
face_output1 = gr.Image(value=os.path.join(gradio_path,'icons/face.jpg'), label="Face 1", scale=0, elem_classes="face-image")
|
| 252 |
-
compare_result = gr.Image(value=os.path.join(gradio_path,'icons/blank.png'), min_width=30, scale=0, show_download_button=False, show_label=False)
|
| 253 |
-
face_output2 = gr.Image(value=os.path.join(gradio_path,'icons/face.jpg'), label="Face 2", scale=0, elem_classes="face-image")
|
| 254 |
-
similarity_markdown = gr.Markdown("")
|
| 255 |
-
txt_speed = gr.Textbox(f"", label="Processing Time (ms)", interactive=False, visible=False)
|
| 256 |
-
with gr.Group():
|
| 257 |
-
gr.Markdown(""" face1""")
|
| 258 |
-
txt_bbox1 = gr.Textbox(f"", label="Rect", interactive=False)
|
| 259 |
-
txt_feature1 = gr.Textbox(f"", label="Feature", interactive=False, max_lines=5)
|
| 260 |
-
with gr.Group():
|
| 261 |
-
gr.Markdown(""" face2""")
|
| 262 |
-
txt_bbox2 = gr.Textbox(f"", label="Rect", interactive=False)
|
| 263 |
-
txt_feature2 = gr.Textbox(f"", label="Feature", interactive=False, max_lines=5)
|
| 264 |
-
|
| 265 |
-
compare_face_button.click(compare_face_clicked, inputs=[compare_face_input1, compare_face_input2, txt_threshold], outputs=[face_output1, face_output2, compare_result, similarity_markdown, txt_bbox1, txt_bbox2, txt_feature1, txt_feature2, txt_speed])
|
| 266 |
-
|
| 267 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False)
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
sys.path.append('../')
|
| 3 |
+
|
| 4 |
+
import os
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import cv2
|
| 7 |
+
import time
|
| 8 |
+
import numpy as np
|
| 9 |
+
from PIL import Image
|
| 10 |
+
|
| 11 |
+
from engine.header import *
|
| 12 |
+
|
| 13 |
+
file_path = os.path.abspath(__file__)
|
| 14 |
+
gradio_path = os.path.dirname(file_path)
|
| 15 |
+
root_path = os.path.dirname(gradio_path)
|
| 16 |
+
|
| 17 |
+
version = get_version().decode('utf-8')
|
| 18 |
+
print_info('\t <Recognito Face Recognition> \t version {}'.format(version))
|
| 19 |
+
|
| 20 |
+
device_id = get_deviceid().decode('utf-8')
|
| 21 |
+
print_info('\t <Hardware ID> \t\t {}'.format(device_id))
|
| 22 |
+
|
| 23 |
+
g_activation_result = -1
|
| 24 |
+
MATCH_THRESHOLD = 0.67
|
| 25 |
+
|
| 26 |
+
css = """
|
| 27 |
+
.example-image img{
|
| 28 |
+
display: flex; /* Use flexbox to align items */
|
| 29 |
+
justify-content: center; /* Center the image horizontally */
|
| 30 |
+
align-items: center; /* Center the image vertically */
|
| 31 |
+
height: 300px; /* Set the height of the container */
|
| 32 |
+
object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
.example-image{
|
| 36 |
+
display: flex; /* Use flexbox to align items */
|
| 37 |
+
justify-content: center; /* Center the image horizontally */
|
| 38 |
+
align-items: center; /* Center the image vertically */
|
| 39 |
+
height: 350px; /* Set the height of the container */
|
| 40 |
+
object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
.face-row {
|
| 44 |
+
display: flex;
|
| 45 |
+
justify-content: space-around; /* Distribute space evenly between elements */
|
| 46 |
+
align-items: center; /* Align items vertically */
|
| 47 |
+
width: 100%; /* Set the width of the row to 100% */
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
.face-image{
|
| 51 |
+
justify-content: center; /* Center the image horizontally */
|
| 52 |
+
align-items: center; /* Center the image vertically */
|
| 53 |
+
height: 160px; /* Set the height of the container */
|
| 54 |
+
width: 160px;
|
| 55 |
+
object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
.face-image img{
|
| 59 |
+
justify-content: center; /* Center the image horizontally */
|
| 60 |
+
align-items: center; /* Center the image vertically */
|
| 61 |
+
height: 160px; /* Set the height of the container */
|
| 62 |
+
object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
.markdown-success-container {
|
| 66 |
+
background-color: #F6FFED;
|
| 67 |
+
padding: 20px;
|
| 68 |
+
margin: 20px;
|
| 69 |
+
border-radius: 1px;
|
| 70 |
+
border: 2px solid green;
|
| 71 |
+
text-align: center;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
.markdown-fail-container {
|
| 75 |
+
background-color: #FFF1F0;
|
| 76 |
+
padding: 20px;
|
| 77 |
+
margin: 20px;
|
| 78 |
+
border-radius: 1px;
|
| 79 |
+
border: 2px solid red;
|
| 80 |
+
text-align: center;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
.block-background {
|
| 84 |
+
# background-color: #202020; /* Set your desired background color */
|
| 85 |
+
border-radius: 5px;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
"""
|
| 89 |
+
|
| 90 |
+
def activate_sdk():
|
| 91 |
+
online_key = os.environ.get("FR_LICENSE_KEY")
|
| 92 |
+
offline_key_path = os.path.join(root_path, "license.txt")
|
| 93 |
+
dict_path = os.path.join(root_path, "engine/bin")
|
| 94 |
+
|
| 95 |
+
ret = -1
|
| 96 |
+
if online_key is None:
|
| 97 |
+
print_warning("Recognition online license key not found!")
|
| 98 |
+
else:
|
| 99 |
+
print_info(f"FR_LICENSE_KEY: {online_key}")
|
| 100 |
+
ret = init_sdk(dict_path.encode('utf-8'), online_key.encode('utf-8'))
|
| 101 |
+
|
| 102 |
+
if ret == 0:
|
| 103 |
+
print_log("Successfully online init SDK!")
|
| 104 |
+
else:
|
| 105 |
+
print_error(f"Failed to online init SDK, Error code {ret}\n Trying offline init SDK...");
|
| 106 |
+
if os.path.exists(offline_key_path) is False:
|
| 107 |
+
print_warning("Recognition offline license key file not found!")
|
| 108 |
+
print_error(f"Falied to offline init SDK, Error code {ret}")
|
| 109 |
+
return ret
|
| 110 |
+
else:
|
| 111 |
+
ret = init_sdk_offline(dict_path.encode('utf-8'), offline_key_path.encode('utf-8'))
|
| 112 |
+
if ret == 0:
|
| 113 |
+
print_log("Successfully offline init SDK!")
|
| 114 |
+
else:
|
| 115 |
+
print_error(f"Falied to offline init SDK, Error code {ret}")
|
| 116 |
+
return ret
|
| 117 |
+
|
| 118 |
+
return ret
|
| 119 |
+
|
| 120 |
+
def compare_face_clicked(frame1, frame2, threshold):
|
| 121 |
+
global g_activation_result
|
| 122 |
+
if g_activation_result != 0:
|
| 123 |
+
gr.Warning("SDK Activation Failed!")
|
| 124 |
+
return None, None, None, None, None, None, None, None, None
|
| 125 |
+
|
| 126 |
+
try:
|
| 127 |
+
image1 = open(frame1, 'rb')
|
| 128 |
+
image2 = open(frame2, 'rb')
|
| 129 |
+
except:
|
| 130 |
+
raise gr.Error("Please select images files!")
|
| 131 |
+
|
| 132 |
+
image_mat1 = cv2.imdecode(np.frombuffer(image1.read(), np.uint8), cv2.IMREAD_COLOR)
|
| 133 |
+
image_mat2 = cv2.imdecode(np.frombuffer(image2.read(), np.uint8), cv2.IMREAD_COLOR)
|
| 134 |
+
start_time = time.time()
|
| 135 |
+
result, score, face_bboxes, face_features = compare_face(image_mat1, image_mat2, float(threshold))
|
| 136 |
+
end_time = time.time()
|
| 137 |
+
process_time = (end_time - start_time) * 1000
|
| 138 |
+
|
| 139 |
+
try:
|
| 140 |
+
image1 = Image.open(frame1)
|
| 141 |
+
image2 = Image.open(frame2)
|
| 142 |
+
images = [image1, image2]
|
| 143 |
+
|
| 144 |
+
face1 = Image.new('RGBA',(150, 150), (80,80,80,0))
|
| 145 |
+
face2 = Image.new('RGBA',(150, 150), (80,80,80,0))
|
| 146 |
+
faces = [face1, face2]
|
| 147 |
+
|
| 148 |
+
face_bboxes_result = []
|
| 149 |
+
if face_bboxes is not None:
|
| 150 |
+
for i, bbox in enumerate(face_bboxes):
|
| 151 |
+
x1 = bbox[0]
|
| 152 |
+
y1 = bbox[1]
|
| 153 |
+
x2 = bbox[2]
|
| 154 |
+
y2 = bbox[3]
|
| 155 |
+
if x1 < 0:
|
| 156 |
+
x1 = 0
|
| 157 |
+
if y1 < 0:
|
| 158 |
+
y1 = 0
|
| 159 |
+
if x2 >= images[i].width:
|
| 160 |
+
x2 = images[i].width - 1
|
| 161 |
+
if y2 >= images[i].height:
|
| 162 |
+
y2 = images[i].height - 1
|
| 163 |
+
|
| 164 |
+
face_bbox_str = f"x1: {x1}, y1: {y1}, x2: {x2}, y2: {y2}"
|
| 165 |
+
face_bboxes_result.append(face_bbox_str)
|
| 166 |
+
|
| 167 |
+
faces[i] = images[i].crop((x1, y1, x2, y2))
|
| 168 |
+
face_image_ratio = faces[i].width / float(faces[i].height)
|
| 169 |
+
resized_w = int(face_image_ratio * 150)
|
| 170 |
+
resized_h = 150
|
| 171 |
+
|
| 172 |
+
faces[i] = faces[i].resize((int(resized_w), int(resized_h)))
|
| 173 |
+
except:
|
| 174 |
+
pass
|
| 175 |
+
|
| 176 |
+
matching_result = Image.open(os.path.join(gradio_path, "icons/blank.png"))
|
| 177 |
+
similarity_score = ""
|
| 178 |
+
if faces[0] is not None and faces[1] is not None:
|
| 179 |
+
if score is not None:
|
| 180 |
+
str_score = str("{:.4f}".format(score))
|
| 181 |
+
if result == "SAME PERSON":
|
| 182 |
+
matching_result = Image.open(os.path.join(gradio_path, "icons/same.png"))
|
| 183 |
+
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>"""
|
| 184 |
+
else:
|
| 185 |
+
matching_result = Image.open(os.path.join(gradio_path, "icons/different.png"))
|
| 186 |
+
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>"""
|
| 187 |
+
|
| 188 |
+
return faces[0], faces[1], matching_result, similarity_score, face_bboxes_result[0], face_bboxes_result[1], face_features[0], face_features[1], str(process_time)
|
| 189 |
+
|
| 190 |
+
def launch_demo(activate_result):
|
| 191 |
+
with gr.Blocks(css=css) as demo:
|
| 192 |
+
gr.Markdown(
|
| 193 |
+
f"""
|
| 194 |
+
<a href="https://recognito.vision" style="display: flex; align-items: center;">
|
| 195 |
+
<img src="https://recognito.vision/wp-content/uploads/2024/03/Recognito-modified.png" style="width: 3%; margin-right: 15px;"/>
|
| 196 |
+
</a>
|
| 197 |
+
<div style="display: flex; align-items: center;justify-content: center;">
|
| 198 |
+
<p style="font-size: 36px; font-weight: bold;">Face Recognition {version}</p>
|
| 199 |
+
</div>
|
| 200 |
+
<p style="font-size: 20px; font-weight: bold;">🤝 Contact us for our on-premise Face Recognition, Liveness Detection SDKs deployment</p>
|
| 201 |
+
</div>
|
| 202 |
+
<div style="display: flex; align-items: center;">
|
| 203 |
+
  <a target="_blank" href="mailto:hello@recognito.vision"><img src="https://img.shields.io/badge/email-hello@recognito.vision-blue.svg?logo=gmail " alt="www.recognito.vision"></a>
|
| 204 |
+
<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>
|
| 205 |
+
<a target="_blank" href="https://t.me/recognito_vision"><img src="https://img.shields.io/badge/telegram-@recognito-blue.svg?logo=telegram " alt="www.recognito.vision"></a>
|
| 206 |
+
<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>
|
| 207 |
+
</div>
|
| 208 |
+
<br/>
|
| 209 |
+
<div style="display: flex; align-items: center;">
|
| 210 |
+
  <a href="https://recognito.vision" style="display: flex; align-items: center;"><img src="https://recognito.vision/wp-content/uploads/2024/03/recognito_64.png" style="width: 24px; margin-right: 5px;"/></a>
|
| 211 |
+
<a href="https://www.linkedin.com/company/recognito-vision" style="display: flex; align-items: center;"><img src="https://recognito.vision/wp-content/uploads/2024/03/linkedin64.png" style="width: 24px; margin-right: 5px;"/></a>
|
| 212 |
+
<a href="https://huggingface.co/Recognito" style="display: flex; align-items: center;"><img src="https://recognito.vision/wp-content/uploads/2024/03/hf1_64.png" style="width: 24px; margin-right: 5px;"/></a>
|
| 213 |
+
<a href="https://github.com/Recognito-Vision" style="display: flex; align-items: center;"><img src="https://recognito.vision/wp-content/uploads/2024/03/github64.png" style="width: 24px; margin-right: 5px;"/></a>
|
| 214 |
+
<a href="https://hub.docker.com/u/recognito" style="display: flex; align-items: center;"><img src="https://recognito.vision/wp-content/uploads/2024/03/docker64.png" style="width: 24px; margin-right: 5px;"/></a>
|
| 215 |
+
</div>
|
| 216 |
+
<br/>
|
| 217 |
+
"""
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
with gr.Group():
|
| 221 |
+
if activate_result == 0:
|
| 222 |
+
gr.Markdown("""<p style="text-align: left; font-size: 20px; color: green;"> Activation Success!</p>""")
|
| 223 |
+
else:
|
| 224 |
+
gr.Markdown("""<p style="text-align: left; font-size: 20px; color: red;"> Activation Failed!</p>""")
|
| 225 |
+
|
| 226 |
+
gr.Textbox(device_id, label="Hardware ID")
|
| 227 |
+
|
| 228 |
+
with gr.Row():
|
| 229 |
+
with gr.Column(scale=2):
|
| 230 |
+
with gr.Row():
|
| 231 |
+
with gr.Column(scale=1):
|
| 232 |
+
compare_face_input1 = gr.Image(label="Image1", type='filepath', elem_classes="example-image")
|
| 233 |
+
gr.Examples([os.path.join(root_path,'examples/1.jpg'),
|
| 234 |
+
os.path.join(root_path,'examples/2.jpg'),
|
| 235 |
+
os.path.join(root_path,'examples/3.jpg'),
|
| 236 |
+
os.path.join(root_path,'examples/4.jpg')],
|
| 237 |
+
inputs=compare_face_input1)
|
| 238 |
+
with gr.Column(scale=1):
|
| 239 |
+
compare_face_input2 = gr.Image(label="Image2", type='filepath', elem_classes="example-image")
|
| 240 |
+
gr.Examples([os.path.join(root_path,'examples/5.jpg'),
|
| 241 |
+
os.path.join(root_path,'examples/6.jpg'),
|
| 242 |
+
os.path.join(root_path,'examples/7.jpg'),
|
| 243 |
+
os.path.join(root_path,'examples/8.jpg')],
|
| 244 |
+
inputs=compare_face_input2)
|
| 245 |
+
|
| 246 |
+
with gr.Blocks():
|
| 247 |
+
with gr.Column(scale=1, min_width=400, elem_classes="block-background"):
|
| 248 |
+
txt_threshold = gr.Textbox(f"{MATCH_THRESHOLD}", label="Matching Threshold", interactive=True)
|
| 249 |
+
compare_face_button = gr.Button("Compare Face", variant="primary", size="lg")
|
| 250 |
+
with gr.Row(elem_classes="face-row"):
|
| 251 |
+
face_output1 = gr.Image(value=os.path.join(gradio_path,'icons/face.jpg'), label="Face 1", scale=0, elem_classes="face-image")
|
| 252 |
+
compare_result = gr.Image(value=os.path.join(gradio_path,'icons/blank.png'), min_width=30, scale=0, show_download_button=False, show_label=False)
|
| 253 |
+
face_output2 = gr.Image(value=os.path.join(gradio_path,'icons/face.jpg'), label="Face 2", scale=0, elem_classes="face-image")
|
| 254 |
+
similarity_markdown = gr.Markdown("")
|
| 255 |
+
txt_speed = gr.Textbox(f"", label="Processing Time (ms)", interactive=False, visible=False)
|
| 256 |
+
with gr.Group():
|
| 257 |
+
gr.Markdown(""" face1""")
|
| 258 |
+
txt_bbox1 = gr.Textbox(f"", label="Rect", interactive=False)
|
| 259 |
+
txt_feature1 = gr.Textbox(f"", label="Feature", interactive=False, max_lines=5)
|
| 260 |
+
with gr.Group():
|
| 261 |
+
gr.Markdown(""" face2""")
|
| 262 |
+
txt_bbox2 = gr.Textbox(f"", label="Rect", interactive=False)
|
| 263 |
+
txt_feature2 = gr.Textbox(f"", label="Feature", interactive=False, max_lines=5)
|
| 264 |
+
|
| 265 |
+
compare_face_button.click(compare_face_clicked, inputs=[compare_face_input1, compare_face_input2, txt_threshold], outputs=[face_output1, face_output2, compare_result, similarity_markdown, txt_bbox1, txt_bbox2, txt_feature1, txt_feature2, txt_speed])
|
| 266 |
+
|
| 267 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False)
|
| 268 |
+
|
| 269 |
+
def compare_faces(img1: Image.Image, img2: Image.Image) -> str:
|
| 270 |
+
# Example placeholder logic — replace with your actual face comparison code
|
| 271 |
+
# You can use your FaceSDK logic here
|
| 272 |
+
if img1.size == img2.size:
|
| 273 |
+
return "Match"
|
| 274 |
+
else:
|
| 275 |
+
return "No match"
|
| 276 |
+
|
| 277 |
+
if __name__ == '__main__':
|
| 278 |
+
g_activation_result = activate_sdk()
|
| 279 |
+
# launch_demo(g_activation_result)
|
| 280 |
+
|
| 281 |
+
# Create Gradio interface
|
| 282 |
+
iface = gr.Interface(
|
| 283 |
+
fn=compare_faces,
|
| 284 |
+
inputs=["image", "image"],
|
| 285 |
+
outputs="text",
|
| 286 |
+
title="Face Comparison API",
|
| 287 |
+
description="Upload two images to compare faces."
|
| 288 |
+
)
|
| 289 |
+
|
| 290 |
+
iface.launch()
|
| 291 |
+
|