|
|
import os |
|
|
import gradio as gr |
|
|
import requests |
|
|
import json |
|
|
import io |
|
|
from gradio.components import Image |
|
|
from PIL import Image as PILImage, ImageDraw, ImageFont |
|
|
|
|
|
from PIL import Image |
|
|
|
|
|
def process_image(image, screenReplyThreshold, printedCopyThreshold, portraitReplaceThreshold): |
|
|
|
|
|
img_bytes = io.BytesIO() |
|
|
image.save(img_bytes, format="JPEG") |
|
|
img_bytes.seek(0) |
|
|
|
|
|
|
|
|
url = "http://127.0.0.1:9000/process_image" |
|
|
files = {'image': img_bytes} |
|
|
result = requests.post(url=url, files=files) |
|
|
if result.ok: |
|
|
json_result = result.json() |
|
|
if json_result.get("resultCode") == "Error": |
|
|
return {"resultCode": "Error", "result": "Failed to process image"} |
|
|
|
|
|
process_results = json_result.get("result") |
|
|
status = process_results.get("status") |
|
|
if status == "Ok": |
|
|
screenReply = process_results.get("screenReply") |
|
|
portraitReplace = process_results.get("portraitReplace") |
|
|
printedCopy = process_results.get("printedCopy") |
|
|
|
|
|
|
|
|
if screenReply < screenReplyThreshold or portraitReplace < portraitReplaceThreshold or printedCopy < printedCopyThreshold: |
|
|
process_results["status"] = "Spoof" |
|
|
else: |
|
|
process_results["status"] = "Real" |
|
|
|
|
|
|
|
|
json_result["result"] = process_results |
|
|
return json_result |
|
|
else: |
|
|
return {"resultCode": "Error", "result": result.text} |
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
gr.Markdown( |
|
|
""" |
|
|
<div style="display: flex;align-items: center;"> |
|
|
<img alt="Opulentyn Logo" src="https://github.com/user-attachments/assets/5fc78032-bff2-4f7e-a174-7d64b22f506d" width="350"/> |
|
|
<div> |
|
|
<h1>ID Document Liveness Detection</h1> |
|
|
<p>We offer <b>on-premises</b> OCR and liveness check solutions available with a <b>perpetual license</b>.</p> |
|
|
</div> |
|
|
</div> |
|
|
|
|
|
## 🤝 Talk to us |
|
|
|
|
|
<div style="display: flex; align-items: center;"> |
|
|
<a href="https://opulentyn.com" target="_blank"> |
|
|
<img src="https://img.shields.io/badge/Website-https%3A%2F%2Fopulentyn.com-blue?style=flat&logo=google-chrome&logoColor=white" alt="Website"> |
|
|
</a> |
|
|
|
|
|
<a href="mailto:support@opulentyn.com"> |
|
|
<img src="https://img.shields.io/badge/Email-support%40opulentyn.com-blue?style=flat&logo=gmail&logoColor=white" alt="Email"> |
|
|
</a> |
|
|
|
|
|
<a href="https://join.slack.com/t/opulentyn/shared_invite/zt-2s230jtbq-dWBs8XUZcrYim~nUqiimSA" target="_blank"> |
|
|
<img src="https://img.shields.io/badge/Slack-support--sdk-blueviolet?style=flat&logo=slack&logoColor=white" alt="Slack"> |
|
|
</a> |
|
|
</div> |
|
|
""" |
|
|
) |
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(): |
|
|
image_input = gr.Image(type='pil') |
|
|
screenReplyThreshold = gr.Slider(minimum=0, maximum=1, value=0.5, label="Screen Reply Threshold") |
|
|
printedCopyThreshold = gr.Slider(minimum=0, maximum=1, value=0.5, label="Printed Copy Threshold") |
|
|
portraitReplaceThreshold = gr.Slider(minimum=0, maximum=1, value=0.5, label="Portrait Replace Threshold") |
|
|
|
|
|
gr.Examples(['examples/1.jpg', 'examples/2.jpg', 'examples/3.jpg'], |
|
|
inputs=image_input) |
|
|
process_button = gr.Button("Process") |
|
|
with gr.Column(): |
|
|
json_output = gr.JSON() |
|
|
|
|
|
process_button.click(process_image, inputs=[image_input, screenReplyThreshold, printedCopyThreshold, portraitReplaceThreshold], outputs=[json_output]) |
|
|
|
|
|
gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fopulentyn%2FIDDocLive"><img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fopulentyn%2FIDDocLive&countColor=%23263759" /></a>') |
|
|
|
|
|
demo.launch(server_name="0.0.0.0", server_port=7860) |