File size: 3,038 Bytes
0a5550b
 
 
 
 
 
 
 
 
 
a943db9
0a5550b
 
 
 
 
 
 
 
 
 
 
 
 
a943db9
0a5550b
 
a943db9
0a5550b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7903e7a
0a5550b
 
 
 
fcdc0b9
0a5550b
 
 
a943db9
0a5550b
a943db9
0a5550b
 
 
7903e7a
 
8887a1d
0a5550b
 
 
 
92b2265
0a5550b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import aiohttp
import gradio as  gr
import numba
import requests 
import  base64
from PIL import Image
import  io
import json
from numba import jit
import matplotlib.pyplot as plt
import os 


examples =  ["examples/0002_01_00_01_55.jpg",
"examples/0-spoof.jpg",
"examples/0.jpg",
"examples/3.jpg",
"examples/6-mask.jpg",
"examples/AGL752VM_id147_s0_150.png",
"examples/FT720P_G780_REDMI4X_id0_s0_105.png",
"examples/7.jpg"]


async def spoof_trigger(b64):
    url = os.getenv('url') 
    payload = {"img": b64}
    headers = {
    'x-functions-key': os.getenv('token'),
    'Content-Type': 'text/plain'
    }

    async with aiohttp.ClientSession() as session:
        async with session.post(url, json=payload, headers=headers) as response:
            response_text = await response.text()
    return response_text
# @jit
async def predict_image(img):
    # Convert NumPy array to PIL Image
    img = Image.fromarray(img.astype('uint8'))
    
    # Create a BytesIO object
    buffer = io.BytesIO()
    
    # Save the PIL Image to the BytesIO object
    img.save(buffer, format='JPEG')
    
    # Get the base64 representation
    img_base64 = base64.b64encode(buffer.getvalue()).decode()

    print(len(img_base64))
 
    res = await spoof_trigger(img_base64)
    # print(json.loads(res))
    spoof_res = json.loads(res)['spoof_res']
    annotated_image  = json.loads(res)['annotated_image']
    conf_score = float( json.loads(spoof_res)['confidence_score'])

    # img_base64 to  plot
    img = Image.open(io.BytesIO(base64.b64decode(annotated_image)))
    confidences = {'Real': conf_score, 'Fake': 1-conf_score}

    return (confidences,img)


with gr.Blocks(title="Spoof-Demo", css="#custom_header {min-height: 3rem; text-align:  center} #custom_title {min-height: 3rem; text-align: center}") as demo :
    gr.Markdown("# Face Antispoof-Demo", elem_id="custom_title")
    gr.Markdown("## Gradio Demo for Face Antispoofing Detection using DeepPairNet based on ResNet50", elem_id="custom_header")
    gr.Markdown("## πŸ‘¨β€πŸ’» Only for research preview Intended" ,elem_id="custom_header")
    with gr.Row():
        with gr.Column():
            with gr.Box():
                gr.Markdown("### Input")
                image = gr.Image(source="webcam",label="Input Image",invert_color=False,image_mode="RGB")
                image.style(height=240) 
                btn = gr.Button(text="Submit")
                btn.style(full_width=True)          
        with gr.Column():
            with gr.Box():
                gr.Markdown("### Output")
                output_image = gr.Image(label="Output Image")
                output_image.style(height=240) 
                label_probs  =  gr.outputs.Label()

    btn.click(predict_image, image , outputs=[label_probs,output_image ],api_name="Face Antispoofing")
    gr.Examples(
        examples=examples,
        inputs=image,
        outputs = output_image,   
        fn=predict_image,
        cache_examples=False,
    ) 

if  __name__ == "__main__":
    demo.launch(debug=True)