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 # This import may be needed if you're processing images from PIL import Image def process_image(image, screenReplyThreshold, printedCopyThreshold, portraitReplaceThreshold): # Convert PIL image to bytes to send in POST request 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") # Check for "Spoof" condition if screenReply < screenReplyThreshold or portraitReplace < portraitReplaceThreshold or printedCopy < printedCopyThreshold: process_results["status"] = "Spoof" else: process_results["status"] = "Real" # Update json_result with the modified process_results json_result["result"] = process_results return json_result else: return {"resultCode": "Error", "result": result.text} with gr.Blocks() as demo: gr.Markdown( """
Opulentyn Logo

ID Document Liveness Detection

We offer on-premises OCR and liveness check solutions available with a perpetual license.

## 🤝 Talk to us
Website      Email      Slack
""" ) 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('') demo.launch(server_name="0.0.0.0", server_port=7860)