-
Notifications
You must be signed in to change notification settings - Fork 16
/
demo.py
188 lines (155 loc) · 6.57 KB
/
demo.py
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import gradio as gr
import requests
import json
from PIL import Image
def idcard_recognition(frame1, frame2):
url = "http://127.0.0.1:8000/ocr/idcard"
files = None
if frame1 is not None and frame2 is not None:
files = {'image1': open(frame1, 'rb'), 'image2': open(frame2, 'rb')}
elif frame1 is not None and frame2 is None:
files = {'image1': open(frame1, 'rb')}
elif frame1 is None and frame2 is not None:
files = {'image1': open(frame2, 'rb')}
else:
return ['', None]
print(frame1, files)
r = requests.post(url=url, files=files)
images = None
resultValues = {}
table_value = ""
for key, value in r.json().items():
if key == 'data':
if 'image' in value:
del value['image']
resultValues[key] = value
else:
resultValues[key] = value
if 'data' in r.json():
for key, value in r.json()['data'].items():
if key == 'image':
for image_key, image_value in value.items():
row_value = ("<tr>"
"<td>{key}</td>"
"<td><img src=""data:image/png;base64,{base64_image} width = '200' height= '100' /></td>"
"</tr>".format(key=image_key, base64_image=image_value))
table_value = table_value + row_value
images = ("<table>"
"<tr>"
"<th>Field</th>"
"<th>Image</th>"
"</tr>"
"{table_value}"
"</table>".format(table_value=table_value))
json_result = json.dumps(resultValues, indent=4)
return [json_result, images]
def barcode_recognition(frame):
url = "http://127.0.0.1:8000/ocr/barcode"
files = None
if frame is None:
return ['', None]
files = {'image': open(frame, 'rb')}
r = requests.post(url=url, files=files)
images = None
resultValues = {}
table_value = ""
for key, value in r.json().items():
if key == 'data':
if 'image' in value:
del value['image']
resultValues[key] = value
else:
resultValues[key] = value
if 'data' in r.json():
for key, value in r.json()['data'].items():
if key == 'image':
for image_key, image_value in value.items():
row_value = ("<tr>"
"<td>{key}</td>"
"<td><img src=""data:image/png;base64,{base64_image} width = '200' height= '100' /></td>"
"</tr>".format(key=image_key, base64_image=image_value))
table_value = table_value + row_value
images = ("<table>"
"<tr>"
"<th>Field</th>"
"<th>Image</th>"
"</tr>"
"{table_value}"
"</table>".format(table_value=table_value))
json_result = json.dumps(resultValues, indent=4)
return [json_result, images]
def credit_recognition(frame):
url = "http://127.0.0.1:8000/ocr/credit"
files = None
if frame is None:
return ['', None]
files = {'image': open(frame, 'rb')}
r = requests.post(url=url, files=files)
images = None
resultValues = {}
table_value = ""
for key, value in r.json().items():
if key == 'data':
if 'image' in value:
del value['image']
resultValues[key] = value
else:
resultValues[key] = value
if 'data' in r.json():
for key, value in r.json()['data'].items():
if key == 'image':
for image_key, image_value in value.items():
row_value = ("<tr>"
"<td>{key}</td>"
"<td><img src=""data:image/png;base64,{base64_image} width = '200' height= '100' /></td>"
"</tr>".format(key=image_key, base64_image=image_value))
table_value = table_value + row_value
images = ("<table>"
"<tr>"
"<th>Field</th>"
"<th>Image</th>"
"</tr>"
"{table_value}"
"</table>".format(table_value=table_value))
json_result = json.dumps(resultValues, indent=4)
return [json_result, images]
with gr.Blocks() as demo:
gr.Markdown(
"""
# ID Document Recognition
Get your own ID Document Recognition Server by duplicating this space.<br/>
Contact us at [email protected] for issues and support.<br/>
"""
)
with gr.TabItem("ID Card Recognition"):
with gr.Row():
with gr.Column(scale=3):
id_image_input1 = gr.Image(type='filepath', label='Front')
id_image_input2 = gr.Image(type='filepath', label='Back')
id_recognition_button = gr.Button("ID Card Recognition")
with gr.Column(scale=5):
id_result_output = gr.JSON()
with gr.Column(scale=2):
image_result_output = gr.HTML()
id_recognition_button.click(idcard_recognition, inputs=[id_image_input1, id_image_input2], outputs=[id_result_output, image_result_output])
with gr.TabItem("Barcode Recognition"):
with gr.Row():
with gr.Column(scale=3):
barcode_image_input = gr.Image(type='filepath')
barcode_recognition_button = gr.Button("Barcode Recognition")
with gr.Column(scale=5):
barcode_result_output = gr.JSON()
with gr.Column(scale=2):
image_result_output = gr.HTML()
barcode_recognition_button.click(barcode_recognition, inputs=barcode_image_input, outputs=[barcode_result_output, image_result_output])
with gr.TabItem("Credit Card Recognition"):
with gr.Row():
with gr.Column(scale=3):
credit_image_input = gr.Image(type='filepath')
credit_recognition_button = gr.Button("Credit Card Recognition")
with gr.Column(scale=5):
credit_result_output = gr.JSON()
with gr.Column(scale=2):
image_result_output = gr.HTML()
credit_recognition_button.click(credit_recognition, inputs=credit_image_input, outputs=[credit_result_output, image_result_output])
demo.launch(server_name="0.0.0.0", server_port=7860)