-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyradox.py
232 lines (194 loc) · 10.1 KB
/
pyradox.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 24 13:02:09 2020
@author: utsav
"""
import numpy as np
import cv2
import base64
import requests
import json
def to_image_string(image_filepath):
return base64.b64encode(open(image_filepath, 'rb').read())#.encode('base64')
def from_base64(base64_data):
nparr = np.fromstring(base64_data.decode('base64'), np.uint8)
return cv2.imdecode(nparr, cv2.IMREAD_ANYCOLOR)
def hit_api_validate(number):
# prepare headers for http request
content_type = 'application/json'
headers = {'content-type': content_type}
addr = 'http://localhost:9001'
url = addr + '/api/validate'
response = requests.post(url, json={"test_number": number} , headers=headers)
return json.loads(response.text)
def hit_api_extract(img):
#img_bytes = base64.b64encode(img)
#convert byte to string
#encoded_string = img_bytes.decode("utf-8")
# prepare headers for http request
content_type = 'application/json'
headers = {'content-type': content_type}
addr = 'http://localhost:9001'
url = addr + '/api/ocr'
response = requests.post(url, json={"doc_b64": base64.b64encode(img.getvalue()).decode()} , headers=headers)
return json.loads(response.text)
def hit_api_mask_aadhaar(img,number_list):
# img_bytes = to_image_string(filepath)
# #convert byte to string
# encoded_string = img_bytes.decode("utf-8")
# # prepare headers for http request
content_type = 'application/json'
headers = {'content-type': content_type}
addr = 'http://localhost:9001'
url = addr + '/api/mask'
response = requests.post(url, json={"doc_b64": base64.b64encode(img.getvalue()).decode(), 'aadhaar': [str(number_list)]}, headers=headers)
return json.loads(response.text)
def hit_api_brut_mask(img):
# img_bytes = to_image_string(input_name)
# #convert byte to string
# encoded_string = img_bytes.decode("utf-8")
# # prepare headers for http request
content_type = 'application/json'
headers = {'content-type': content_type}
addr = 'http://localhost:9001'
url = addr + '/api/brut_mask'
response = requests.post(url, json={"doc_b64": base64.b64encode(img.getvalue()).decode()}, headers=headers)
return json.loads(response.text)
# r = json.loads(response.text)
# save_name = output_name
# decoded_data = base64.b64decode(r['doc_b64_brut_masked'])
# np_data = np.fromstring(decoded_data,np.uint8)
# img = cv2.imdecode(np_data,cv2.IMREAD_UNCHANGED)
# cv2.imwrite(save_name,img)
# return "masked document saved as "+ save_name
def hit_api_sample_pipe(img,brut = False):
# img_bytes = to_image_string(input_name)
# #convert byte to string
# encoded_string = img_bytes.decode("utf-8")
# # prepare headers for http request
content_type = 'application/json'
headers = {'content-type': content_type}
addr = 'http://localhost:9001'
url = addr + '/api/sample_pipe'
response = requests.post(url, json={"doc_b64": base64.b64encode(img.getvalue()).decode(), "brut" : brut}, headers=headers)
return json.loads(response.text)
# r = json.loads(response.text)
# if r['is_masked']:
# save_name = output_name
# decoded_data = base64.b64decode(r['doc_b64_masked'])
# np_data = np.fromstring(decoded_data,np.uint8)
# img = cv2.imdecode(np_data,cv2.IMREAD_UNCHANGED)
# cv2.imwrite(save_name,img)
# print("Execution Mode =>",r['mode_executed'])
# if r['mode_executed'] == "OCR-MASKING":
# print("Aadhaar List =>",r['aadhaar_list'])
# print("Validated Aadhaar list =>",r['valid_aadhaar_list'])
# return "masked document saved as "+ save_name
# else:
# print("Execution Mode =>",r['mode_executed'])
# print("Error =>",r['error'])
# return "Unable to find given number in the image :/ (try brut mode)"
#
################################################### UI ################################
import streamlit as st
st.title("[PyraDox :page_with_curl:](https://github.com/festivitymishra/PyraDox)")
st.info(
"""
**PyraDox is a python tool which helps in document digitization** by extracting text
information and masking of personal information with the help of Tesseract-ocr.
"""
)
st.sidebar.subheader('PyraDox :page_with_curl:')
Run_Mode = st.sidebar.selectbox(
'Select Document Type',
('Select Doc','Aadhaar Card', 'Driver Licence', 'Passport', 'Pan Card'))
if Run_Mode == 'Select Doc':
image_filepath = 'resources/PyraDox.jpg'
st.image(open(image_filepath, 'rb').read(), caption='', use_column_width=True)
st.write("Supported Documents : \n - Aadhaar Card (UIDAI)"
)
elif Run_Mode == 'Aadhaar Card':
st.write(" Document Type **Aadhaar Card** ")
feature = st.sidebar.radio("What's dow you want to try?",('Mask 1st 8 digits of Aadhaar',
'Validate Aadhaar Number',
'Extract Aadhaar Number',
'Mask Aadhaar Number',
'Brut Mask Numbers'))
if feature == "Validate Aadhaar Number":
st.write('Please enter 12 digit Aadhaar Number')
number = st.number_input('Insert aadhaar number', min_value=10000000000,
max_value=999999999999, value=397788000234,
step=1,format = '%d')
#st.write('The current number is ', number)
if st.button('Validate'):
if hit_api_validate(number)['validity']:
st.write(" :white_check_mark: Valid Aadhaar Card Sequence Number")
else:
st.write(" :no_entry: Invalid Aadhaar Card Sequence Number")
elif feature == "Extract Aadhaar Number":
uploaded_file = st.file_uploader("Upload an image file", type=['png', 'jpg'])
if uploaded_file is not None:
# image = Image.open(uploaded_file)
st.image(uploaded_file, caption='Uploaded Image.', use_column_width=True) #width = 450)#
st.write("Click on extract to **extract** Aadhar Number from the image!")
if st.button('Extract'):
#st.write(hit_api_extract_new(uploaded_file))
extract_response = hit_api_extract(uploaded_file)['aadhaar_list']
if len(extract_response)>0:
st.write(" :white_check_mark: Extracted Aadhaar Number is **",extract_response[0],"**")
else:
st.write(" :no_entry: Failed to find any Aadhaar Number in the given document")
elif feature == "Mask Aadhaar Number":
uploaded_file_2 = st.file_uploader("Upload an image file", type=['png', 'jpg'])
st.write('Please enter 12 digit Aadhaar Number to be Masked')
number_2 = st.number_input('Insert aadhaar number', min_value=10000000000,
max_value=999999999999, value=397788000234,
step=1,format = '%d')
if uploaded_file_2 is not None and number_2 is not None:
st.image(uploaded_file_2, caption='Uploaded Image.', width = 400)#
st.write("Click on **Mask** to mask given Aadhar Number from the image!")
if st.button('Mask'):
r = hit_api_mask_aadhaar(uploaded_file_2,number_2)
if r['is_masked']:
decoded_data = base64.b64decode(r['doc_b64_masked'])
st.image(decoded_data, caption='Masked Image.', width = 400)#
st.write(" :white_check_mark: Masked Given Number in the Document")
else:
st.write(" :no_entry: Unable to find given number in the image :/ (try brut mode)")
elif feature == "Mask 1st 8 digits of Aadhaar":
uploaded_file_3 = st.file_uploader("Upload an image file", type=['png', 'jpg'])
if uploaded_file_3 is not None:
st.image(uploaded_file_3, caption='Uploaded Image.', width = 400)#
st.write("Click on **Mask** to Mask first 8 digits Aadhar Number from the image!")
brut_mode = st.checkbox('Use Brut Mode')
# st.write("brut_mode is",brut_mode)
if st.button('Mask'):
r = hit_api_sample_pipe(uploaded_file_3,brut_mode)
if r['is_masked']:
decoded_data_1 = base64.b64decode(r['doc_b64_masked'])
st.image(decoded_data_1, caption='Masked Image.', width = 400)#
st.write(" :white_check_mark: Masked Given Number in the Document")
st.write("Execution Mode =>",r['mode_executed'])
if r['mode_executed'] == "OCR-MASKING":
st.write("Aadhaar List =>**",r['aadhaar_list'][0],"**")
if len(r['valid_aadhaar_list'])>0:
st.write("Validated Aadhaar list =>**",r['valid_aadhaar_list'][0],"**")
else:
st.write("Execution Mode =>",r['mode_executed'])
st.write("Error =>",r['error'])
st.write(" :no_entry: Unable to find given number in the image :/ (try brut mode)")
elif feature == "Brut Mask Numbers":
uploaded_file_4 = st.file_uploader("Upload an image file", type=['png', 'jpg'])
if uploaded_file_4 is not None:
st.image(uploaded_file_4, caption='Uploaded Image.', width = 400)#
st.write("Click on **BRUT Mask** to Brut Mask Numbers from the image!")
# st.write("brut_mode is",brut_mode)
if st.button('BRUT Mask'):
r = hit_api_brut_mask(uploaded_file_4)
decoded_data_2 = base64.b64decode(r['doc_b64_brut_masked'])
st.image(decoded_data_2, caption='Masked Image.', width = 400)#
else:
st.write(" **Yowza! we are yet baking it for you ...** :penguin:")
comingsoon = 'resources/coming-soon.jpg'
st.image(open(comingsoon, 'rb').read(), caption='', use_column_width=True)