Zhu-FaceOnLive commited on
Commit
ba351d8
·
verified ·
1 Parent(s): 148ad99

Update demo.py

Browse files
Files changed (1) hide show
  1. demo.py +71 -71
demo.py CHANGED
@@ -1,71 +1,71 @@
1
- import os
2
- import gradio as gr
3
- import requests
4
- import json
5
- import io
6
- import base64
7
- import cv2
8
- import numpy as np
9
- from gradio.components import Image
10
-
11
- screenReplayThreshold = 0.5
12
- portraitReplaceThreshold = 0.5
13
- printedCopyThreshold = 0.5
14
-
15
- def proc_output(result):
16
- if result.ok:
17
- json_result = result.json()
18
- if json_result.get("resultCode") == "Error":
19
- return {"status": "error", "result": "failed to process image"}
20
-
21
- process_results = json_result.get("result")
22
- status = process_results.get("status")
23
- if status == "Ok":
24
- screenReply = process_results.get("screenReply")
25
- portraitReplace = process_results.get("portraitReplace")
26
- printedCopy = process_results.get("printedCopy")
27
- detResult = "genuine"
28
-
29
- # Check for "Spoof" condition
30
- if screenReply < screenReplayThreshold or portraitReplace < portraitReplaceThreshold or printedCopy < printedCopyThreshold:
31
- detResult = "spoof"
32
-
33
- # Update json_result with the modified process_results
34
- return {"status": "ok", "data": {"result": detResult, "screenreplay_integrity_score": screenReply, "portraitreplace_integrity_score": portraitReplace, "printedcutout_integrity_score": printedCopy}}
35
-
36
- return {"status": "error", "result": "document not found!"}
37
- else:
38
- return {"status": "error", "result": result.text}
39
-
40
- def id_liveness(path):
41
- # Convert PIL image to bytes to send in POST request
42
- img_bytes = io.BytesIO()
43
- path.save(img_bytes, format="JPEG")
44
- img_bytes.seek(0)
45
-
46
- url = "http://127.0.0.1:9000/process_image"
47
- files = {'image': img_bytes}
48
- result = requests.post(url=url, files=files)
49
- return proc_output(result)
50
-
51
- with gr.Blocks() as demo:
52
- gr.Markdown(
53
- """
54
- # ID Document Liveness Detection
55
- Contact us at https://faceonlive.com for issues and support.<br/><br/>
56
- ** For security and privacy, kindly refrain from uploading real ID card or credit card information on this platform.
57
- """
58
- )
59
- with gr.Row():
60
- with gr.Column():
61
- image_input = gr.Image(type='pil')
62
- gr.Examples(['examples/1.jpg', 'examples/2.jpg', 'examples/3.jpg'],
63
- inputs=image_input)
64
- process_button = gr.Button("ID Liveness Detection")
65
-
66
- with gr.Column():
67
- json_output = gr.JSON()
68
-
69
- process_button.click(id_liveness, inputs=image_input, outputs=[json_output])
70
-
71
- demo.launch(server_name="0.0.0.0")
 
1
+ import os
2
+ import gradio as gr
3
+ import requests
4
+ import json
5
+ import io
6
+ import base64
7
+ import cv2
8
+ import numpy as np
9
+ from gradio.components import Image
10
+
11
+ screenReplayThreshold = 0.5
12
+ portraitReplaceThreshold = 0.5
13
+ printedCopyThreshold = 0.5
14
+
15
+ def proc_output(result):
16
+ if result.ok:
17
+ json_result = result.json()
18
+ if json_result.get("resultCode") == "Error":
19
+ return {"status": "error", "result": "failed to process image"}
20
+
21
+ process_results = json_result.get("result")
22
+ status = process_results.get("status")
23
+ if status == "Ok":
24
+ screenReply = process_results.get("screenReply")
25
+ portraitReplace = process_results.get("portraitReplace")
26
+ printedCopy = process_results.get("printedCopy")
27
+ detResult = "genuine"
28
+
29
+ # Check for "Spoof" condition
30
+ if screenReply < screenReplayThreshold or portraitReplace < portraitReplaceThreshold or printedCopy < printedCopyThreshold:
31
+ detResult = "spoof"
32
+
33
+ # Update json_result with the modified process_results
34
+ return {"status": "ok", "data": {"result": detResult, "screenreplay_integrity_score": screenReply, "portraitreplace_integrity_score": portraitReplace, "printedcutout_integrity_score": printedCopy}}
35
+
36
+ return {"status": "error", "result": "document not found!"}
37
+ else:
38
+ return {"status": "error", "result": result.text}
39
+
40
+ def id_liveness(path):
41
+ # Convert PIL image to bytes to send in POST request
42
+ img_bytes = io.BytesIO()
43
+ path.save(img_bytes, format="JPEG")
44
+ img_bytes.seek(0)
45
+
46
+ url = "http://127.0.0.1:9000/process_image"
47
+ files = {'image': img_bytes}
48
+ result = requests.post(url=url, files=files)
49
+ return proc_output(result)
50
+
51
+ with gr.Blocks() as demo:
52
+ gr.Markdown(
53
+ """
54
+ # ID Document Liveness Detection
55
+ Contact us at https://faceonlive.com for issues and support.<br/><br/>
56
+ ** For security and privacy, kindly refrain from uploading real ID card or credit card information on this platform.
57
+ """
58
+ )
59
+ with gr.Row():
60
+ with gr.Column():
61
+ image_input = gr.Image(type='pil')
62
+ gr.Examples(['examples/1.jpg', 'examples/2.jpg', 'examples/3.jpg'],
63
+ inputs=image_input)
64
+ process_button = gr.Button("ID Liveness Detection")
65
+
66
+ with gr.Column():
67
+ json_output = gr.JSON()
68
+
69
+ process_button.click(id_liveness, inputs=image_input, outputs=[json_output], api_name=False)
70
+
71
+ demo.queue(api_open=False).launch(server_name="0.0.0.0", show_api=False)