Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,4 @@
|
|
| 1 |
import sys
|
| 2 |
-
sys.path.append('../')
|
| 3 |
-
|
| 4 |
import os
|
| 5 |
import gradio as gr
|
| 6 |
import json
|
|
@@ -9,15 +7,6 @@ import requests
|
|
| 9 |
import numpy as np
|
| 10 |
from PIL import Image
|
| 11 |
|
| 12 |
-
from engine.header import *
|
| 13 |
-
|
| 14 |
-
file_path = os.path.abspath(__file__)
|
| 15 |
-
dir_path = os.path.dirname(file_path)
|
| 16 |
-
root_path = os.path.dirname(dir_path)
|
| 17 |
-
|
| 18 |
-
device_id = get_deviceid().decode('utf-8')
|
| 19 |
-
print_info('\t <Hardware ID> \t\t {}'.format(device_id))
|
| 20 |
-
|
| 21 |
css = """
|
| 22 |
.example-image img{
|
| 23 |
display: flex; /* Use flexbox to align items */
|
|
@@ -69,63 +58,25 @@ def json_to_html_table(data, image_keys):
|
|
| 69 |
html += "</table>"
|
| 70 |
return html
|
| 71 |
|
| 72 |
-
def
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
print_info(f"LICENSE_KEY: {online_key}")
|
| 83 |
-
activate_ret = set_activation(online_key.encode('utf-8')).decode('utf-8')
|
| 84 |
-
ret = json.loads(activate_ret).get("errorCode", None)
|
| 85 |
-
|
| 86 |
-
if ret == 0:
|
| 87 |
-
print_log("Successfully online activation SDK!")
|
| 88 |
-
else:
|
| 89 |
-
print_error(f"Failed to online activation SDK, Error code {ret}\n Trying offline activation SDK...");
|
| 90 |
-
if os.path.exists(offline_key_path) is False:
|
| 91 |
-
print_warning("Offline license key file not found!")
|
| 92 |
-
print_error(f"Falied to offline activation SDK, Error code {ret}")
|
| 93 |
-
return ret
|
| 94 |
else:
|
| 95 |
-
|
| 96 |
-
offline_key = file.read()
|
| 97 |
-
file.close()
|
| 98 |
-
activate_ret = set_activation(offline_key.encode('utf-8')).decode('utf-8')
|
| 99 |
-
ret = json.loads(activate_ret).get("errorCode", None)
|
| 100 |
-
if ret == 0:
|
| 101 |
-
print_log("Successfully offline activation SDK!")
|
| 102 |
-
else:
|
| 103 |
-
print_error(f"Falied to offline activation SDK, Error code {ret}")
|
| 104 |
-
return ret
|
| 105 |
-
|
| 106 |
-
init_ret = init_sdk(dict_path.encode('utf-8')).decode('utf-8')
|
| 107 |
-
ret = json.loads(activate_ret).get("errorCode", None)
|
| 108 |
-
print_log(f"Init SDK: {ret}")
|
| 109 |
-
return ret
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
return ['', None]
|
| 116 |
-
|
| 117 |
-
image1, image2 = '', ''
|
| 118 |
-
if frame1 is not None and frame2 is not None:
|
| 119 |
-
image1, image2 = frame1, frame2
|
| 120 |
-
elif frame1 is not None and frame2 is None:
|
| 121 |
-
image1 = frame1
|
| 122 |
-
elif frame1 is None and frame2 is not None:
|
| 123 |
-
image1 = frame2
|
| 124 |
-
elif frame1 is None and frame2 is None:
|
| 125 |
raise gr.Error("Please select images files!")
|
| 126 |
|
| 127 |
-
ocrResult = ocr_id_card(image1.encode('utf-8'), image2.encode('utf-8'))
|
| 128 |
-
ocrResDict = json.loads(ocrResult)
|
| 129 |
images = None
|
| 130 |
rawValues = {}
|
| 131 |
image_table_value = ""
|
|
@@ -143,14 +94,15 @@ def idcard_recognition(frame1, frame2):
|
|
| 143 |
'documentNumber':'',
|
| 144 |
}
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
| 154 |
|
| 155 |
images = ("<table>"
|
| 156 |
"<tr>"
|
|
@@ -160,16 +112,17 @@ def idcard_recognition(frame1, frame2):
|
|
| 160 |
"{image_table_value}"
|
| 161 |
"</table>".format(image_table_value=image_table_value))
|
| 162 |
|
| 163 |
-
value
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
|
|
|
| 169 |
|
| 170 |
|
| 171 |
for result_key in result_table_dict.keys():
|
| 172 |
-
result_table_dict[result_key] = find_key_in_dict(
|
| 173 |
|
| 174 |
result = json_to_html_table(result_table_dict, {'portrait'})
|
| 175 |
json_result = json.dumps(rawValues, indent=6)
|
|
@@ -245,5 +198,4 @@ def launch_demo(activate_result):
|
|
| 245 |
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False)
|
| 246 |
|
| 247 |
if __name__ == '__main__':
|
| 248 |
-
g_activation_result = activate_sdk()
|
| 249 |
launch_demo(g_activation_result)
|
|
|
|
| 1 |
import sys
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
| 4 |
import json
|
|
|
|
| 7 |
import numpy as np
|
| 8 |
from PIL import Image
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
css = """
|
| 11 |
.example-image img{
|
| 12 |
display: flex; /* Use flexbox to align items */
|
|
|
|
| 58 |
html += "</table>"
|
| 59 |
return html
|
| 60 |
|
| 61 |
+
def idcard_recognition(frame1, frame2):
|
| 62 |
+
url = "recognito-iddocumentrecognition.p.rapidapi.com/api/read_idcard"
|
| 63 |
+
try:
|
| 64 |
+
files = None
|
| 65 |
+
if frame1 is not None and frame2 is not None:
|
| 66 |
+
files = {'image': open(frame1, 'rb'), 'image2': open(frame2, 'rb')}
|
| 67 |
+
elif frame1 is not None and frame2 is None:
|
| 68 |
+
files = {'image': open(frame1, 'rb')}
|
| 69 |
+
elif frame1 is None and frame2 is not None:
|
| 70 |
+
files = {'image': open(frame2, 'rb')}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
else:
|
| 72 |
+
return ['', None]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
headers = {"X-RapidAPI-Key": os.environ.get("API_KEY")}
|
| 75 |
+
|
| 76 |
+
r = requests.post(url=url, files=files, headers=headers)
|
| 77 |
+
except:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
raise gr.Error("Please select images files!")
|
| 79 |
|
|
|
|
|
|
|
| 80 |
images = None
|
| 81 |
rawValues = {}
|
| 82 |
image_table_value = ""
|
|
|
|
| 94 |
'documentNumber':'',
|
| 95 |
}
|
| 96 |
|
| 97 |
+
if 'data' in r.json():
|
| 98 |
+
for key, value in r.json()['data'].items():
|
| 99 |
+
if key == 'image':
|
| 100 |
+
for image_key, image_value in value.items():
|
| 101 |
+
row_value = ("<tr>"
|
| 102 |
+
"<td>{key}</td>"
|
| 103 |
+
"<td><img src=""data:image/png;base64,{base64_image} width = '200' height= '100' /></td>"
|
| 104 |
+
"</tr>".format(key=image_key, base64_image=image_value))
|
| 105 |
+
image_table_value = image_table_value + row_value
|
| 106 |
|
| 107 |
images = ("<table>"
|
| 108 |
"<tr>"
|
|
|
|
| 112 |
"{image_table_value}"
|
| 113 |
"</table>".format(image_table_value=image_table_value))
|
| 114 |
|
| 115 |
+
for key, value in r.json().items():
|
| 116 |
+
if key == 'data':
|
| 117 |
+
if 'image' in value:
|
| 118 |
+
del value['image']
|
| 119 |
+
rawValues = value
|
| 120 |
+
else:
|
| 121 |
+
rawValues = value
|
| 122 |
|
| 123 |
|
| 124 |
for result_key in result_table_dict.keys():
|
| 125 |
+
result_table_dict[result_key] = find_key_in_dict(r.json(), result_key)
|
| 126 |
|
| 127 |
result = json_to_html_table(result_table_dict, {'portrait'})
|
| 128 |
json_result = json.dumps(rawValues, indent=6)
|
|
|
|
| 198 |
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False)
|
| 199 |
|
| 200 |
if __name__ == '__main__':
|
|
|
|
| 201 |
launch_demo(g_activation_result)
|