import os
import gradio as gr
import requests
import json
import io
import base64
import cv2
import numpy as np
from gradio.components import Image
screenReplayThreshold = 0.5
portraitReplaceThreshold = 0.5
printedCopyThreshold = 0.5
def proc_output(result):
if result.ok:
json_result = result.json()
if json_result.get("resultCode") == "Error":
return {"status": "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")
detResult = "genuine"
# Check for "Spoof" condition
if screenReply < screenReplayThreshold or portraitReplace < portraitReplaceThreshold or printedCopy < printedCopyThreshold:
detResult = "spoof"
# Update json_result with the modified process_results
return {"status": "ok", "data": {"result": detResult, "screenreplay_integrity_score": screenReply, "portraitreplace_integrity_score": portraitReplace, "printedcutout_integrity_score": printedCopy}}
return {"status": "error", "result": "document not found!"}
else:
return {"status": "error", "result": result.text}
def id_liveness(path):
# Convert PIL image to bytes to send in POST request
img_bytes = io.BytesIO()
path.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)
return proc_output(result)
with gr.Blocks() as demo:
gr.Markdown(
"""
# ID Document Liveness Detection
Contact us at https://faceonlive.com for issues and support.
** For security and privacy, kindly refrain from uploading real ID card or credit card information on this platform.
"""
)
with gr.Row():
with gr.Column():
image_input = gr.Image(type='pil')
gr.Examples(['examples/1.jpg', 'examples/2.jpg', 'examples/3.jpg'],
inputs=image_input)
process_button = gr.Button("ID Liveness Detection")
with gr.Column():
json_output = gr.JSON()
process_button.click(id_liveness, inputs=image_input, outputs=[json_output], api_name=False)
demo.queue(api_open=False).launch(server_name="0.0.0.0", show_api=False)