File size: 2,629 Bytes
ba351d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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.<br/><br/>
    ** 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)