Spaces:
Runtime error
Runtime error
sanjay7178
commited on
Commit
β’
0a5550b
1
Parent(s):
90eec9e
Upload 10 files
Browse files- app.py +101 -0
- examples/0-spoof.jpg +0 -0
- examples/0.jpg +0 -0
- examples/0002_01_00_01_55.jpg +0 -0
- examples/3.jpg +0 -0
- examples/6-mask.jpg +0 -0
- examples/7.jpg +0 -0
- examples/AGL752VM_id147_s0_150.png +0 -0
- examples/FT720P_G780_REDMI4X_id0_s0_105.png +0 -0
- test.py +21 -0
app.py
CHANGED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import aiohttp
|
2 |
+
import gradio as gr
|
3 |
+
import numba
|
4 |
+
import requests
|
5 |
+
import base64
|
6 |
+
from PIL import Image
|
7 |
+
import io
|
8 |
+
import json
|
9 |
+
from numba import jit
|
10 |
+
import matplotlib.pyplot as plt
|
11 |
+
|
12 |
+
|
13 |
+
examples = ["examples/0002_01_00_01_55.jpg",
|
14 |
+
"examples/0-spoof.jpg",
|
15 |
+
"examples/0.jpg",
|
16 |
+
"examples/3.jpg",
|
17 |
+
"examples/6-mask.jpg",
|
18 |
+
"examples/AGL752VM_id147_s0_150.png",
|
19 |
+
"examples/FT720P_G780_REDMI4X_id0_s0_105.png",
|
20 |
+
"examples/7.jpg"]
|
21 |
+
|
22 |
+
|
23 |
+
async def spoof_trigger(b64):
|
24 |
+
url = "https://spoofapi1.azurewebsites.net/api/spoofvisualize"
|
25 |
+
payload = {"img": b64}
|
26 |
+
headers = {
|
27 |
+
'x-functions-key': 'wGw3zXXPlLCez-VrcSs9RTahE4gLC674pf7Fp6Au2kUHAzFuNnZZMw==',
|
28 |
+
'Content-Type': 'text/plain'
|
29 |
+
}
|
30 |
+
|
31 |
+
async with aiohttp.ClientSession() as session:
|
32 |
+
async with session.post(url, json=payload, headers=headers) as response:
|
33 |
+
response_text = await response.text()
|
34 |
+
return response_text
|
35 |
+
# @jit
|
36 |
+
async def predict_image(img):
|
37 |
+
# Convert NumPy array to PIL Image
|
38 |
+
img = Image.fromarray(img.astype('uint8'))
|
39 |
+
|
40 |
+
# Create a BytesIO object
|
41 |
+
buffer = io.BytesIO()
|
42 |
+
|
43 |
+
# Save the PIL Image to the BytesIO object
|
44 |
+
img.save(buffer, format='JPEG')
|
45 |
+
|
46 |
+
# Get the base64 representation
|
47 |
+
img_base64 = base64.b64encode(buffer.getvalue()).decode()
|
48 |
+
|
49 |
+
print(len(img_base64))
|
50 |
+
# # img_base64 to plot
|
51 |
+
# img = Image.open(io.BytesIO(base64.b64decode(img_base64)))
|
52 |
+
# # img save
|
53 |
+
# img.save("img.jpg")
|
54 |
+
|
55 |
+
|
56 |
+
res = await spoof_trigger(img_base64)
|
57 |
+
# print(json.loads(res))
|
58 |
+
spoof_res = json.loads(res)['spoof_res']
|
59 |
+
annotated_image = json.loads(res)['annotated_image']
|
60 |
+
conf_score = float( json.loads(spoof_res)['confidence_score'])
|
61 |
+
|
62 |
+
# img_base64 to plot
|
63 |
+
img = Image.open(io.BytesIO(base64.b64decode(annotated_image)))
|
64 |
+
# img save
|
65 |
+
img.save("cache/img.jpg")
|
66 |
+
|
67 |
+
confidences = {'Real': 1-conf_score, 'Fake': conf_score}
|
68 |
+
|
69 |
+
return (confidences,annotated_image)
|
70 |
+
|
71 |
+
|
72 |
+
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 :
|
73 |
+
gr.Markdown("## Face Antispoof-Demo", elem_id="custom_title")
|
74 |
+
gr.Markdown("Gradio Demo for Face Antispoofing", elem_id="custom_header")
|
75 |
+
gr.Markdown("π¨βπ» Only fot research preview Intended")
|
76 |
+
with gr.Row():
|
77 |
+
with gr.Column():
|
78 |
+
with gr.Box():
|
79 |
+
gr.Markdown("### Input")
|
80 |
+
image = gr.Image(label="Input Image")
|
81 |
+
image.style(height=240)
|
82 |
+
btn = gr.Button(text="Submit")
|
83 |
+
btn.style(full_width=True)
|
84 |
+
with gr.Column():
|
85 |
+
with gr.Box():
|
86 |
+
gr.Markdown("### Output")
|
87 |
+
output_image = gr.Image(label="Output Image")
|
88 |
+
output_image.style(height=240)
|
89 |
+
label_probs = gr.outputs.Label()
|
90 |
+
|
91 |
+
btn.click(predict_image, image , outputs=[label_probs,output_image ],api_name="Face Antispoofing")
|
92 |
+
gr.Examples(
|
93 |
+
examples=examples,
|
94 |
+
inputs=image,
|
95 |
+
outputs = output_image,
|
96 |
+
fn=predict_image,
|
97 |
+
cache_examples=False,
|
98 |
+
)
|
99 |
+
|
100 |
+
if __name__ == "__main__":
|
101 |
+
demo.launch(debug=True)
|
examples/0-spoof.jpg
ADDED
examples/0.jpg
ADDED
examples/0002_01_00_01_55.jpg
ADDED
examples/3.jpg
ADDED
examples/6-mask.jpg
ADDED
examples/7.jpg
ADDED
examples/AGL752VM_id147_s0_150.png
ADDED
examples/FT720P_G780_REDMI4X_id0_s0_105.png
ADDED
test.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import aiohttp
|
2 |
+
import json
|
3 |
+
import base64
|
4 |
+
|
5 |
+
async def spoof_trigger(b64):
|
6 |
+
url = "https://spoofapi1.azurewebsites.net/api/spoofvisualize"
|
7 |
+
payload = {"img": b64}
|
8 |
+
headers = {
|
9 |
+
'x-functions-key': 'wGw3zXXPlLCez-VrcSs9RTahE4gLC674pf7Fp6Au2kUHAzFuNnZZMw==',
|
10 |
+
'Content-Type': 'text/plain'
|
11 |
+
}
|
12 |
+
|
13 |
+
async with aiohttp.ClientSession() as session:
|
14 |
+
async with session.post(url, json=payload, headers=headers) as response:
|
15 |
+
response_text = await response.text()
|
16 |
+
spoof_res = json.loads(response_text)
|
17 |
+
return spoof_res
|
18 |
+
|
19 |
+
a = input('input:')
|
20 |
+
x = spoof_trigger(a)
|
21 |
+
print(x)
|