Spaces:
Running
Running
Update demo.py
Browse files
demo.py
CHANGED
|
@@ -5,114 +5,124 @@ from gradio.components import Image
|
|
| 5 |
from PIL import Image as PILImage, ImageDraw, ImageFont # This import may be needed if you're processing images
|
| 6 |
from PIL import Image
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def compare_face(frame1, frame2):
|
| 9 |
url = "http://127.0.0.1:8080/compare_face"
|
| 10 |
files = {'file1': open(frame1, 'rb'), 'file2': open(frame2, 'rb')}
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
html = None
|
| 15 |
-
faces = None
|
| 16 |
-
|
| 17 |
-
compare_result = r.json().get('compare_result')
|
| 18 |
-
compare_similarity = r.json().get('compare_similarity')
|
| 19 |
-
|
| 20 |
-
html = ("<table>"
|
| 21 |
-
"<tr>"
|
| 22 |
-
"<th>Compare Result</th>"
|
| 23 |
-
"<th>Value</th>"
|
| 24 |
-
"</tr>"
|
| 25 |
-
"<tr>"
|
| 26 |
-
"<td>Result</td>"
|
| 27 |
-
"<td>{compare_result}</td>"
|
| 28 |
-
"</tr>"
|
| 29 |
-
"<tr>"
|
| 30 |
-
"<td>Similarity</td>"
|
| 31 |
-
"<td>{compare_similarity}</td>"
|
| 32 |
-
"</tr>"
|
| 33 |
-
"</table>".format(compare_result=compare_result, compare_similarity=compare_similarity))
|
| 34 |
-
|
| 35 |
try:
|
| 36 |
-
|
| 37 |
-
image2 = Image.open(frame2)
|
| 38 |
-
|
| 39 |
-
face1 = None
|
| 40 |
-
face2 = None
|
| 41 |
-
|
| 42 |
-
if r.json().get('face1') is not None:
|
| 43 |
-
face = r.json().get('face1')
|
| 44 |
-
x1 = face.get('x1')
|
| 45 |
-
y1 = face.get('y1')
|
| 46 |
-
x2 = face.get('x2')
|
| 47 |
-
y2 = face.get('y2')
|
| 48 |
-
|
| 49 |
-
if x1 < 0:
|
| 50 |
-
x1 = 0
|
| 51 |
-
if y1 < 0:
|
| 52 |
-
y1 = 0
|
| 53 |
-
if x2 >= image1.width:
|
| 54 |
-
x2 = image1.width - 1
|
| 55 |
-
if y2 >= image1.height:
|
| 56 |
-
y2 = image1.height - 1
|
| 57 |
-
|
| 58 |
-
face1 = image1.crop((x1, y1, x2, y2))
|
| 59 |
-
face_image_ratio = face1.width / float(face1.height)
|
| 60 |
-
resized_w = int(face_image_ratio * 150)
|
| 61 |
-
resized_h = 150
|
| 62 |
-
|
| 63 |
-
face1 = face1.resize((int(resized_w), int(resized_h)))
|
| 64 |
-
|
| 65 |
-
if r.json().get('face2') is not None:
|
| 66 |
-
face = r.json().get('face2')
|
| 67 |
-
x1 = face.get('x1')
|
| 68 |
-
y1 = face.get('y1')
|
| 69 |
-
x2 = face.get('x2')
|
| 70 |
-
y2 = face.get('y2')
|
| 71 |
-
|
| 72 |
-
if x1 < 0:
|
| 73 |
-
x1 = 0
|
| 74 |
-
if y1 < 0:
|
| 75 |
-
y1 = 0
|
| 76 |
-
if x2 >= image2.width:
|
| 77 |
-
x2 = image2.width - 1
|
| 78 |
-
if y2 >= image2.height:
|
| 79 |
-
y2 = image2.height - 1
|
| 80 |
-
|
| 81 |
-
face2 = image2.crop((x1, y1, x2, y2))
|
| 82 |
-
face_image_ratio = face2.width / float(face2.height)
|
| 83 |
-
resized_w = int(face_image_ratio * 150)
|
| 84 |
-
resized_h = 150
|
| 85 |
-
|
| 86 |
-
face2 = face2.resize((int(resized_w), int(resized_h)))
|
| 87 |
-
|
| 88 |
-
if face1 is not None and face2 is not None:
|
| 89 |
-
new_image = Image.new('RGB',(face1.width + face2.width + 10, 150), (80,80,80))
|
| 90 |
-
|
| 91 |
-
new_image.paste(face1,(0,0))
|
| 92 |
-
new_image.paste(face2,(face1.width + 10, 0))
|
| 93 |
-
faces = new_image.copy()
|
| 94 |
-
elif face1 is not None and face2 is None:
|
| 95 |
-
new_image = Image.new('RGB',(face1.width + face1.width + 10, 150), (80,80,80))
|
| 96 |
-
|
| 97 |
-
new_image.paste(face1,(0,0))
|
| 98 |
-
faces = new_image.copy()
|
| 99 |
-
elif face1 is None and face2 is not None:
|
| 100 |
-
new_image = Image.new('RGB',(face2.width + face2.width + 10, 150), (80,80,80))
|
| 101 |
-
|
| 102 |
-
new_image.paste(face2,(face2.width + 10, 0))
|
| 103 |
-
faces = new_image.copy()
|
| 104 |
-
|
| 105 |
except:
|
| 106 |
-
|
| 107 |
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
with gr.Blocks() as demo:
|
| 111 |
gr.Markdown(
|
| 112 |
"""
|
| 113 |
# KBY-AI - Face Recognition
|
| 114 |
We offer SDKs for face recognition, liveness detection(anti-spoofing) and ID card recognition.
|
| 115 |
-
|
| 116 |
<h4 style="display: flex; align-items: center;">
|
| 117 |
ID Document Liveness Detection - Linux - <a href="https://web.kby-ai.com">https://web.kby-ai.com</a>
|
| 118 |
<span>
|
|
@@ -135,21 +145,21 @@ with gr.Blocks() as demo:
|
|
| 135 |
```
|
| 136 |
"""
|
| 137 |
)
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
inputs=
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fkby-ai%2FFaceRecognition"><img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fkby-ai%2FFaceRecognition&countColor=%23263759" /></a>')
|
| 154 |
|
| 155 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
|
| 5 |
from PIL import Image as PILImage, ImageDraw, ImageFont # This import may be needed if you're processing images
|
| 6 |
from PIL import Image
|
| 7 |
|
| 8 |
+
def face_crop(image, face_rect):
|
| 9 |
+
x1 = face_rect.get('x1')
|
| 10 |
+
y1 = face_rect.get('y1')
|
| 11 |
+
x2 = face_rect.get('x2')
|
| 12 |
+
y2 = face_rect.get('y2')
|
| 13 |
+
width = x2 - x1 + 1
|
| 14 |
+
height = y2 - y1 + 1
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
if x1 < 0:
|
| 18 |
+
x1 = 0
|
| 19 |
+
if y1 < 0:
|
| 20 |
+
y1 = 0
|
| 21 |
+
if x2 >= image.width:
|
| 22 |
+
x2 = image.width - 1
|
| 23 |
+
if y2 >= image.height:
|
| 24 |
+
y2 = image.height - 1
|
| 25 |
+
|
| 26 |
+
face_image = image.crop((x1, y1, x2, y2))
|
| 27 |
+
face_image_ratio = face_image.width / float(face_image.height)
|
| 28 |
+
resized_w = int(face_image_ratio * 150)
|
| 29 |
+
resized_h = 150
|
| 30 |
+
|
| 31 |
+
face_image = face_image.resize((int(resized_w), int(resized_h)))
|
| 32 |
+
return face_image
|
| 33 |
+
|
| 34 |
+
def pil_image_to_base64(image, format="PNG"):
|
| 35 |
+
"""
|
| 36 |
+
Converts a PIL.Image object to a Base64-encoded string.
|
| 37 |
+
:param image: PIL.Image object
|
| 38 |
+
:param format: Format to save the image, e.g., "PNG", "JPEG"
|
| 39 |
+
:return: Base64-encoded string
|
| 40 |
+
"""
|
| 41 |
+
# Save the image to a BytesIO buffer
|
| 42 |
+
buffer = io.BytesIO()
|
| 43 |
+
image.save(buffer, format=format)
|
| 44 |
+
buffer.seek(0) # Rewind the buffer
|
| 45 |
+
|
| 46 |
+
# Convert the buffer's contents to a Base64 string
|
| 47 |
+
base64_string = base64.b64encode(buffer.getvalue()).decode('utf-8')
|
| 48 |
+
return base64_string
|
| 49 |
+
|
| 50 |
def compare_face(frame1, frame2):
|
| 51 |
url = "http://127.0.0.1:8080/compare_face"
|
| 52 |
files = {'file1': open(frame1, 'rb'), 'file2': open(frame2, 'rb')}
|
| 53 |
|
| 54 |
+
file1 = None
|
| 55 |
+
file2 = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
try:
|
| 57 |
+
file1 = open(frame1, 'rb')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
except:
|
| 59 |
+
return "Failed to open image1"
|
| 60 |
|
| 61 |
+
try:
|
| 62 |
+
file2 = open(frame2, 'rb')
|
| 63 |
+
except:
|
| 64 |
+
return "Failed to open image2"
|
| 65 |
+
|
| 66 |
+
url = "http://127.0.0.1:8080/compare_face"
|
| 67 |
+
files = {'file1': file1, 'file2': file2}
|
| 68 |
+
result = requests.post(url=url, files=files)
|
| 69 |
+
|
| 70 |
+
if result.ok:
|
| 71 |
+
json_result = result.json()
|
| 72 |
+
|
| 73 |
+
if json_result.get("resultCode") != "Ok":
|
| 74 |
+
return json_result.get("resultCode")
|
| 75 |
+
|
| 76 |
+
try:
|
| 77 |
+
image1 = Image.open(frame1)
|
| 78 |
+
image2 = Image.open(frame2)
|
| 79 |
+
|
| 80 |
+
html = ""
|
| 81 |
+
faces1 = json_result.get("faces1", {})
|
| 82 |
+
faces2 = json_result.get("faces2", {})
|
| 83 |
+
results = json_result.get("results", {})
|
| 84 |
+
|
| 85 |
+
for result in results:
|
| 86 |
+
similarity = result.get('similarity')
|
| 87 |
+
face1_idx = result.get('face1')
|
| 88 |
+
face2_idx = result.get('face2')
|
| 89 |
+
|
| 90 |
+
face_image1 = face_crop(image1, faces1[face1_idx])
|
| 91 |
+
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"))
|
| 92 |
+
|
| 93 |
+
face_image2 = face_crop(image2, faces2[face2_idx])
|
| 94 |
+
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"))
|
| 95 |
+
|
| 96 |
+
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>'
|
| 97 |
+
if similarity > 0.67:
|
| 98 |
+
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>'
|
| 99 |
+
|
| 100 |
+
item_value = ('<div style="align-items: center; gap: 10px; display: flex; flex-direction: column;">'
|
| 101 |
+
'<div style="display: flex; align-items: center; gap: 20px;">'
|
| 102 |
+
'{face_value1}'
|
| 103 |
+
'{match_icon}'
|
| 104 |
+
'{face_value2}'
|
| 105 |
+
'</div>'
|
| 106 |
+
'<div style="text-align: center; margin-top: 10px;">'
|
| 107 |
+
'Similarity: {similarity}'
|
| 108 |
+
'</div>'
|
| 109 |
+
'</div>'
|
| 110 |
+
).format(face_value1=face_value1, face_value2=face_value2, match_icon=match_icon, similarity=f"{similarity:.2f}")
|
| 111 |
+
|
| 112 |
+
html += item_value
|
| 113 |
+
html += '<hr style="border: 1px solid #C0C0C0; margin: 10px 0;"/>'
|
| 114 |
+
|
| 115 |
+
return html
|
| 116 |
+
except:
|
| 117 |
+
return "Processing failed"
|
| 118 |
+
else:
|
| 119 |
+
return result.text
|
| 120 |
|
| 121 |
with gr.Blocks() as demo:
|
| 122 |
gr.Markdown(
|
| 123 |
"""
|
| 124 |
# KBY-AI - Face Recognition
|
| 125 |
We offer SDKs for face recognition, liveness detection(anti-spoofing) and ID card recognition.
|
|
|
|
| 126 |
<h4 style="display: flex; align-items: center;">
|
| 127 |
ID Document Liveness Detection - Linux - <a href="https://web.kby-ai.com">https://web.kby-ai.com</a>
|
| 128 |
<span>
|
|
|
|
| 145 |
```
|
| 146 |
"""
|
| 147 |
)
|
| 148 |
+
with gr.Column(scale=7):
|
| 149 |
+
with gr.Row():
|
| 150 |
+
with gr.Column():
|
| 151 |
+
image_input1 = gr.Image(type='filepath')
|
| 152 |
+
gr.Examples(['face_examples/1.jpg', 'face_examples/3.jpg', 'face_examples/5.jpg', 'face_examples/7.jpg', 'face_examples/9.jpg'],
|
| 153 |
+
inputs=image_input1)
|
| 154 |
+
with gr.Column():
|
| 155 |
+
image_input2 = gr.Image(type='filepath')
|
| 156 |
+
gr.Examples(['face_examples/2.jpg', 'face_examples/4.jpg', 'face_examples/6.jpg', 'face_examples/8.jpg', 'face_examples/10.jpg'],
|
| 157 |
+
inputs=image_input2)
|
| 158 |
+
face_recog_button = gr.Button("Compare Face", variant="primary", size="lg")
|
| 159 |
+
with gr.Column(scale=3):
|
| 160 |
+
recog_html_output = gr.HTML()
|
| 161 |
+
|
| 162 |
+
face_recog_button.click(compare_face, inputs=[image_input1, image_input2], outputs=recog_html_output)
|
| 163 |
gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fkby-ai%2FFaceRecognition"><img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fkby-ai%2FFaceRecognition&countColor=%23263759" /></a>')
|
| 164 |
|
| 165 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|