Spaces:
Running
Running
Update demo.py
Browse files
demo.py
CHANGED
|
@@ -3,123 +3,108 @@ import requests
|
|
| 3 |
import datadog_api_client
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
def
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
"</table>".format(compare_result=compare_result, compare_similarity=compare_similarity))
|
| 32 |
-
|
| 33 |
try:
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
face1 = None
|
| 38 |
-
face2 = None
|
| 39 |
-
|
| 40 |
-
if r.json().get('face1') is not None:
|
| 41 |
-
face = r.json().get('face1')
|
| 42 |
-
x1 = face.get('x1')
|
| 43 |
-
y1 = face.get('y1')
|
| 44 |
-
x2 = face.get('x2')
|
| 45 |
-
y2 = face.get('y2')
|
| 46 |
-
|
| 47 |
-
if x1 < 0:
|
| 48 |
-
x1 = 0
|
| 49 |
-
if y1 < 0:
|
| 50 |
-
y1 = 0
|
| 51 |
-
if x2 >= image1.width:
|
| 52 |
-
x2 = image1.width - 1
|
| 53 |
-
if y2 >= image1.height:
|
| 54 |
-
y2 = image1.height - 1
|
| 55 |
-
|
| 56 |
-
face1 = image1.crop((x1, y1, x2, y2))
|
| 57 |
-
face_image_ratio = face1.width / float(face1.height)
|
| 58 |
-
resized_w = int(face_image_ratio * 150)
|
| 59 |
-
resized_h = 150
|
| 60 |
-
|
| 61 |
-
face1 = face1.resize((int(resized_w), int(resized_h)))
|
| 62 |
-
|
| 63 |
-
if r.json().get('face2') is not None:
|
| 64 |
-
face = r.json().get('face2')
|
| 65 |
-
x1 = face.get('x1')
|
| 66 |
-
y1 = face.get('y1')
|
| 67 |
-
x2 = face.get('x2')
|
| 68 |
-
y2 = face.get('y2')
|
| 69 |
-
|
| 70 |
-
if x1 < 0:
|
| 71 |
-
x1 = 0
|
| 72 |
-
if y1 < 0:
|
| 73 |
-
y1 = 0
|
| 74 |
-
if x2 >= image2.width:
|
| 75 |
-
x2 = image2.width - 1
|
| 76 |
-
if y2 >= image2.height:
|
| 77 |
-
y2 = image2.height - 1
|
| 78 |
-
|
| 79 |
-
face2 = image2.crop((x1, y1, x2, y2))
|
| 80 |
-
face_image_ratio = face2.width / float(face2.height)
|
| 81 |
-
resized_w = int(face_image_ratio * 150)
|
| 82 |
-
resized_h = 150
|
| 83 |
-
|
| 84 |
-
face2 = face2.resize((int(resized_w), int(resized_h)))
|
| 85 |
-
|
| 86 |
-
if face1 is not None and face2 is not None:
|
| 87 |
-
new_image = Image.new('RGB',(face1.width + face2.width + 10, 150), (80,80,80))
|
| 88 |
-
|
| 89 |
-
new_image.paste(face1,(0,0))
|
| 90 |
-
new_image.paste(face2,(face1.width + 10, 0))
|
| 91 |
-
faces = new_image.copy()
|
| 92 |
-
elif face1 is not None and face2 is None:
|
| 93 |
-
new_image = Image.new('RGB',(face1.width + face1.width + 10, 150), (80,80,80))
|
| 94 |
-
|
| 95 |
-
new_image.paste(face1,(0,0))
|
| 96 |
-
faces = new_image.copy()
|
| 97 |
-
elif face1 is None and face2 is not None:
|
| 98 |
-
new_image = Image.new('RGB',(face2.width + face2.width + 10, 150), (80,80,80))
|
| 99 |
-
|
| 100 |
-
new_image.paste(face2,(face2.width + 10, 0))
|
| 101 |
-
faces = new_image.copy()
|
| 102 |
-
|
| 103 |
except:
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
return [faces, html]
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
with gr.Blocks(css=".gradio-container {background-color: #F4E5E0}") as demo:
|
| 109 |
with gr.Row():
|
| 110 |
-
with gr.Column():
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
| 124 |
|
| 125 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 3 |
import datadog_api_client
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
def face_crop(image, face_rect):
|
| 7 |
+
x = face_rect.get('x')
|
| 8 |
+
y = face_rect.get('y')
|
| 9 |
+
width = face_rect.get('width')
|
| 10 |
+
height = face_rect.get('height')
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
if x < 0:
|
| 14 |
+
x = 0
|
| 15 |
+
if y < 0:
|
| 16 |
+
y = 0
|
| 17 |
+
if x + width >= image.width:
|
| 18 |
+
width = image.width - x
|
| 19 |
+
if y + height >= image.height:
|
| 20 |
+
height = image.height - y
|
| 21 |
+
|
| 22 |
+
face_image = image.crop((x, y, x + width - 1, y + height - 1))
|
| 23 |
+
face_image_ratio = face_image.width / float(face_image.height)
|
| 24 |
+
resized_w = int(face_image_ratio * 150)
|
| 25 |
+
resized_h = 150
|
| 26 |
+
|
| 27 |
+
face_image = face_image.resize((int(resized_w), int(resized_h)))
|
| 28 |
+
return face_image
|
| 29 |
+
|
| 30 |
+
def compare_face(image1, image2):
|
|
|
|
|
|
|
| 31 |
try:
|
| 32 |
+
img_bytes1 = io.BytesIO()
|
| 33 |
+
image1.save(img_bytes1, format="JPEG")
|
| 34 |
+
img_bytes1.seek(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
except:
|
| 36 |
+
return ["Failed to open image1", {"resultCode": "Failed to open image1"}]
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
try:
|
| 39 |
+
img_bytes2 = io.BytesIO()
|
| 40 |
+
image2.save(img_bytes2, format="JPEG")
|
| 41 |
+
img_bytes2.seek(0)
|
| 42 |
+
except:
|
| 43 |
+
return ["Failed to open image2", {"resultCode": "Failed to open image2"}]
|
| 44 |
+
|
| 45 |
+
url = "http://127.0.0.1:8080/compare_face"
|
| 46 |
+
files = {'image1': img_bytes1, 'image2': img_bytes2}
|
| 47 |
+
result = requests.post(url=url, files=files)
|
| 48 |
+
if result.ok:
|
| 49 |
+
json_result = result.json()
|
| 50 |
+
if json_result.get("resultCode") != "Ok":
|
| 51 |
+
return [json_result.get("resultCode"), json_result]
|
| 52 |
+
|
| 53 |
+
html = ""
|
| 54 |
+
faces1 = json_result.get("faces1", {})
|
| 55 |
+
faces2 = json_result.get("faces2", {})
|
| 56 |
+
results = json_result.get("results", {})
|
| 57 |
+
|
| 58 |
+
for result in results:
|
| 59 |
+
score = result.get('score')
|
| 60 |
+
face1_idx = result.get('face1')
|
| 61 |
+
face2_idx = result.get('face2')
|
| 62 |
+
|
| 63 |
+
face_image1 = face_crop(image1, faces1[face1_idx])
|
| 64 |
+
face_value1 = ('<img src="data:image/png;base64,{base64_image}" style="width: 100px; height: auto; object-fit: contain;"/>').format(base64_image=pil_image_to_base64(face_image1, format="PNG"))
|
| 65 |
+
|
| 66 |
+
face_image2 = face_crop(image2, faces2[face2_idx])
|
| 67 |
+
face_value2 = ('<img src="data:image/png;base64,{base64_image}" style="width: 100px; height: auto; object-fit: contain;"/>').format(base64_image=pil_image_to_base64(face_image2, format="PNG"))
|
| 68 |
+
|
| 69 |
+
match_icon = '<svg fill="red" width="19" height="32" viewBox="0 0 19 32"><path d="M0 13.92V10.2H19V13.92H0ZM0 21.64V17.92H19V21.64H0Z"></path><path d="M14.08 0H18.08L5.08 32H1.08L14.08 0Z"></path></svg>'
|
| 70 |
+
if score > 0.7:
|
| 71 |
+
match_icon = '<svg fill="green" width="19" height="32" viewBox="0 0 19 32"><path d="M0 13.9202V10.2002H19V13.9202H0ZM0 21.6402V17.9202H19V21.6402H0Z"></path></svg>'
|
| 72 |
+
|
| 73 |
+
item_value = ('<div style="align-items: center; gap: 10px; display: flex; flex-direction: column;">'
|
| 74 |
+
'<div style="display: flex; align-items: center; gap: 20px;">'
|
| 75 |
+
'{face_value1}'
|
| 76 |
+
'{match_icon}'
|
| 77 |
+
'{face_value2}'
|
| 78 |
+
'</div>'
|
| 79 |
+
'<div style="text-align: center; margin-top: 10px;">'
|
| 80 |
+
'Score: {score}'
|
| 81 |
+
'</div>'
|
| 82 |
+
'</div>'
|
| 83 |
+
).format(face_value1=face_value1, face_value2=face_value2, match_icon=match_icon, score=f"{score:.2f}")
|
| 84 |
+
html += item_value
|
| 85 |
+
html += '<hr style="border: 1px solid #C0C0C0; margin: 10px 0;"/>'
|
| 86 |
+
|
| 87 |
+
return html
|
| 88 |
+
else:
|
| 89 |
+
return result.text
|
| 90 |
+
|
| 91 |
with gr.Blocks(css=".gradio-container {background-color: #F4E5E0}") as demo:
|
| 92 |
with gr.Row():
|
| 93 |
+
with gr.Column(scale=7):
|
| 94 |
+
with gr.Row():
|
| 95 |
+
with gr.Column():
|
| 96 |
+
image_input1 = gr.Image(type='pil')
|
| 97 |
+
gr.Examples(['examples/1.webp', 'examples/2.webp', 'examples/3.webp', 'examples/4.webp'],
|
| 98 |
+
inputs=image_input1)
|
| 99 |
+
with gr.Column():
|
| 100 |
+
image_input2 = gr.Image(type='pil')
|
| 101 |
+
gr.Examples(['examples/5.webp', 'examples/6.webp', 'examples/7.webp', 'examples/8.webp'],
|
| 102 |
+
inputs=image_input2)
|
| 103 |
+
verifyThreshold = gr.Slider(minimum=0, maximum=1, value=0.67, label="Verify Threshold")
|
| 104 |
+
face_recog_button = gr.Button("Face Recognition")
|
| 105 |
+
with gr.Column(scale=3):
|
| 106 |
+
recog_html_output = gr.HTML()
|
| 107 |
+
|
| 108 |
+
face_recog_button.click(compare_face, inputs=[image_input1, image_input2, verifyThreshold], outputs=recog_html_output)
|
| 109 |
|
| 110 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|