File size: 4,097 Bytes
bab1bd7
 
 
8f6d539
bab1bd7
 
 
f53c455
bab1bd7
 
dc9dc06
abc1fdb
bab1bd7
 
 
 
 
 
f822f4a
 
 
bab1bd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abc1fdb
f822f4a
30d9a11
f822f4a
bab1bd7
 
 
 
4967de5
bab1bd7
 
 
 
 
 
 
 
4967de5
bab1bd7
 
 
 
 
 
abc1fdb
30d9a11
bab1bd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abc1fdb
bab1bd7
 
13ad2b9
abc1fdb
bab1bd7
 
 
 
 
 
 
 
 
 
 
cbf79d3
779f1b7
bab1bd7
6319ba7
 
cbf79d3
779f1b7
bab1bd7
 
13ad2b9
bab1bd7
 
9b27b8e
bab1bd7
 
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import gradio as gr
import requests
import json
from PIL import Image

def compare_face(frame1, frame2):
    url = "http://127.0.0.1:8000/api/compare_face"
    files = {'image1': open(frame1, 'rb'), 'image2': open(frame2, 'rb')}

    r = requests.post(url=url, files=files)
    faces = None
    log = "start"
    try:
        image1 = Image.open(frame1)
        image2 = Image.open(frame2)

        face1 = None
        face2 = None
        data = r.json().get('data')
        if data.get('face1') is not None:
            face = data.get('face1')
            x1 = face.get('x1')
            y1 = face.get('y1')
            x2 = face.get('x2')
            y2 = face.get('y2')
            if x1 < 0:
                x1 = 0
            if y1 < 0:
                y1 = 0
            if x2 >= image1.width:
                x2 = image1.width - 1
            if y2 >= image1.height:
                y2 = image1.height - 1

            face1 = image1.crop((x1, y1, x2, y2))
            face_image_ratio = face1.width / float(face1.height)
            resized_w = int(face_image_ratio * 150)
            resized_h = 150

            face1 = face1.resize((int(resized_w), int(resized_h)))
        log += "2 " + str(face1.width) + "\n"
        if data.get('face2') is not None:
            log += "3 \n"
            face = data.get('face2')
            x1 = face.get('x1')
            y1 = face.get('y1')
            x2 = face.get('x2')
            y2 = face.get('y2')
            log += "3-1 \n"
            if x1 < 0:
                x1 = 0
            if y1 < 0:
                y1 = 0
            if x2 >= image2.width:
                x2 = image2.width - 1
            if y2 >= image2.height:
                y2 = image2.height - 1
            log += "3-2 " + str(x1) + " " + str(y1) + " " + str(x2) + " " + str(y2) + "\n"
            face2 = image2.crop((x1, y1, x2, y2))
            face_image_ratio = face2.width / float(face2.height)
            resized_w = int(face_image_ratio * 150)
            resized_h = 150

            face2 = face2.resize((int(resized_w), int(resized_h)))
        
        log += "3 " + str(face2) + "\n"
        if face1 is not None and face2 is not None:
            new_image = Image.new('RGB',(face1.width + face2.width + 10, 150), (80,80,80))

            new_image.paste(face1,(0,0))
            new_image.paste(face2,(face1.width + 10, 0))
            faces = new_image.copy()
        elif face1 is not None and face2 is None:
            new_image = Image.new('RGB',(face1.width + face1.width + 10, 150), (80,80,80))

            new_image.paste(face1,(0,0))
            faces = new_image.copy()
        elif face1 is None and face2 is not None:
            new_image = Image.new('RGB',(face2.width + face2.width + 10, 150), (80,80,80))

            new_image.paste(face2,(face2.width + 10, 0))
            faces = new_image.copy()

        log += "4 " + str(faces)
    except:
        pass

    return [{"log": log}, faces]

with gr.Blocks() as demo:
    gr.Markdown(
        """
    # Face Liveness Detection
    Get your own Face Liveness Detection Server by duplicating this space.<br/>
    Contact us at [email protected] for issues and support.<br/>
    """
    )
    with gr.Row():
        with gr.Column():
            compare_face_input1 = gr.Image(type='filepath', height=480)
            gr.Examples(['gradio/examples/1.jpg', 'gradio/examples/2.jpg'], 
                        inputs=compare_face_input1)
            compare_face_button = gr.Button("Compare Face")
        with gr.Column():
            compare_face_input2 = gr.Image(type='filepath', height=480)
            gr.Examples(['gradio/examples/3.jpg', 'gradio/examples/4.jpg'], 
                        inputs=compare_face_input2)
        with gr.Column():
            compare_face_output = gr.Image(type="pil", height=150)
            compare_result_output = gr.JSON(label='Result')

    compare_face_button.click(compare_face, inputs=[compare_face_input1, compare_face_input2], outputs=[compare_result_output, compare_face_output])

demo.launch(server_name="0.0.0.0", server_port=7860)