Update app.py
Browse files
app.py
CHANGED
@@ -1,212 +1,211 @@
|
|
1 |
-
import sys
|
2 |
-
|
3 |
-
|
4 |
-
import
|
5 |
-
import
|
6 |
-
import
|
7 |
-
import
|
8 |
-
import
|
9 |
-
import
|
10 |
-
import
|
11 |
-
from
|
12 |
-
from
|
13 |
-
from fastapi import
|
14 |
-
from
|
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 |
-
file
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
ret
|
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 |
-
ret
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
uvicorn.run(gr_app, host="0.0.0.0", port=9000)
|
|
|
1 |
+
import sys
|
2 |
+
|
3 |
+
import os
|
4 |
+
import base64
|
5 |
+
import json
|
6 |
+
import uuid
|
7 |
+
import cv2
|
8 |
+
import numpy as np
|
9 |
+
import gradio as gr
|
10 |
+
from time import gmtime, strftime
|
11 |
+
from pydantic import BaseModel
|
12 |
+
from fastapi import FastAPI, File, UploadFile
|
13 |
+
from fastapi.responses import JSONResponse
|
14 |
+
from typing import Dict
|
15 |
+
|
16 |
+
from engine.header import *
|
17 |
+
|
18 |
+
file_path = os.path.abspath(__file__)
|
19 |
+
dir_path = os.path.dirname(file_path)
|
20 |
+
root_path = os.path.dirname(dir_path)
|
21 |
+
dump_path = os.path.join(root_path, "dump2/")
|
22 |
+
|
23 |
+
device_id = get_deviceid().decode('utf-8')
|
24 |
+
print_info('\t <Hardware ID> \t\t {}'.format(device_id))
|
25 |
+
|
26 |
+
def activate_sdk():
|
27 |
+
online_key = os.environ.get("LICENSE_KEY")
|
28 |
+
offline_key_path = os.path.join(root_path, "license.txt")
|
29 |
+
|
30 |
+
dict_path = os.path.join(root_path, "engine/bin")
|
31 |
+
|
32 |
+
ret = -1
|
33 |
+
if online_key is None:
|
34 |
+
print_warning("Online license key not found!")
|
35 |
+
else:
|
36 |
+
activate_ret = set_activation(online_key.encode('utf-8')).decode('utf-8')
|
37 |
+
ret = json.loads(activate_ret).get("errorCode", None)
|
38 |
+
|
39 |
+
if ret == 0:
|
40 |
+
print_log("Successfully online activation SDK!")
|
41 |
+
else:
|
42 |
+
print_error(f"Failed to online activation SDK, Error code {ret}\n Trying offline activation SDK...");
|
43 |
+
if os.path.exists(offline_key_path) is False:
|
44 |
+
print_warning("Offline license key file not found!")
|
45 |
+
print_error(f"Falied to offline activation SDK, Error code {ret}")
|
46 |
+
return ret
|
47 |
+
else:
|
48 |
+
file=open(offline_key_path,"r")
|
49 |
+
offline_key = file.read()
|
50 |
+
file.close()
|
51 |
+
activate_ret = set_activation(offline_key.encode('utf-8')).decode('utf-8')
|
52 |
+
ret = json.loads(activate_ret).get("errorCode", None)
|
53 |
+
if ret == 0:
|
54 |
+
print_log("Successfully offline activation SDK!")
|
55 |
+
else:
|
56 |
+
print_error(f"Falied to offline activation SDK, Error code {ret}")
|
57 |
+
return ret
|
58 |
+
|
59 |
+
init_ret = init_sdk(dict_path.encode('utf-8')).decode('utf-8')
|
60 |
+
ret = json.loads(activate_ret).get("errorCode", None)
|
61 |
+
print_log(f"Init SDK: {ret}")
|
62 |
+
return ret
|
63 |
+
|
64 |
+
|
65 |
+
async def save_upload_file(upload_file: UploadFile) -> str:
|
66 |
+
file_name = uuid.uuid4().hex[:6] + "_" + upload_file.filename
|
67 |
+
save_path = os.path.join(dump_path, file_name)
|
68 |
+
with open(save_path, "wb") as buffer:
|
69 |
+
shutil.copyfileobj(upload_file.file, buffer)
|
70 |
+
return os.path.abspath(save_path)
|
71 |
+
|
72 |
+
async def save_base64_file(base64_string: str) -> str:
|
73 |
+
file_name = uuid.uuid4().hex[:6] + ".jpg" # or ".png" depending on your use
|
74 |
+
save_path = os.path.join(dump_path, file_name)
|
75 |
+
|
76 |
+
with open(save_path, "wb") as buffer:
|
77 |
+
buffer.write(base64.b64decode(base64_string))
|
78 |
+
|
79 |
+
return os.path.abspath(save_path)
|
80 |
+
|
81 |
+
app = FastAPI()
|
82 |
+
|
83 |
+
class ImageBase64Request(BaseModel):
|
84 |
+
image: str
|
85 |
+
|
86 |
+
@app.get("/")
|
87 |
+
def read_root():
|
88 |
+
return {"status": "API is running"}
|
89 |
+
|
90 |
+
@app.post("/api/read_idcard")
|
91 |
+
async def read_idcard(image: UploadFile = File(...), image2: UploadFile = File(None)):
|
92 |
+
try:
|
93 |
+
file_path1 = await save_upload_file(image)
|
94 |
+
|
95 |
+
file_path2 = ""
|
96 |
+
if image2 is not None:
|
97 |
+
file_path2 = await save_upload_file(image2)
|
98 |
+
|
99 |
+
|
100 |
+
ocrResult = ocr_id_card(file_path1.encode('utf-8'), file_path2.encode('utf-8'))
|
101 |
+
ocrResDict = json.loads(ocrResult)
|
102 |
+
|
103 |
+
os.remove(file_path1)
|
104 |
+
if file_path2:
|
105 |
+
os.remove(file_path2)
|
106 |
+
|
107 |
+
return JSONResponse(content={"status": "ok", "data": ocrResDict}, status_code=200)
|
108 |
+
|
109 |
+
except Exception as e:
|
110 |
+
return JSONResponse(content={"status": "error", "message": str(e)}, status_code=500)
|
111 |
+
|
112 |
+
|
113 |
+
@app.post("/api/read_idcard_base64")
|
114 |
+
async def read_idcard_base64(request_data: ImageBase64Request):
|
115 |
+
try:
|
116 |
+
file_path = await save_base64_file(request_data.image)
|
117 |
+
|
118 |
+
# Your OCR function (pass bytes-encoded file path)
|
119 |
+
ocr_result = ocr_id_card(file_path.encode('utf-8'), ''.encode('utf-8'))
|
120 |
+
ocr_res_dict = json.loads(ocr_result)
|
121 |
+
|
122 |
+
# Remove the temp file
|
123 |
+
os.remove(file_path)
|
124 |
+
|
125 |
+
return JSONResponse(content={"status": "ok", "data": ocr_res_dict}, status_code=200)
|
126 |
+
|
127 |
+
except Exception as e:
|
128 |
+
return JSONResponse(content={"status": "error", "message": str(e)}, status_code=500)
|
129 |
+
|
130 |
+
@app.post("/api/read_credit")
|
131 |
+
async def read_credit(image: UploadFile = File(...)):
|
132 |
+
try:
|
133 |
+
file_path = await save_upload_file(image)
|
134 |
+
|
135 |
+
ocrResult = ocr_credit_card(file_path.encode('utf-8'))
|
136 |
+
ocrResDict = json.loads(ocrResult)
|
137 |
+
|
138 |
+
os.remove(file_path)
|
139 |
+
|
140 |
+
return JSONResponse(content={"status": "ok", "data": ocrResDict}, status_code=200)
|
141 |
+
|
142 |
+
except Exception as e:
|
143 |
+
return JSONResponse(content={"status": "error", "message": str(e)}, status_code=500)
|
144 |
+
|
145 |
+
@app.post("/api/read_credit_base64")
|
146 |
+
async def read_credit_base64(request_data: ImageBase64Request):
|
147 |
+
try:
|
148 |
+
file_path = await save_base64_file(request_data.image)
|
149 |
+
|
150 |
+
# Your OCR function (pass bytes-encoded file path)
|
151 |
+
ocr_result = ocr_credit_card(file_path.encode('utf-8'))
|
152 |
+
ocr_res_dict = json.loads(ocr_result)
|
153 |
+
|
154 |
+
# Remove the temp file
|
155 |
+
os.remove(file_path)
|
156 |
+
|
157 |
+
return JSONResponse(content={"status": "ok", "data": ocr_res_dict}, status_code=200)
|
158 |
+
|
159 |
+
except Exception as e:
|
160 |
+
return JSONResponse(content={"status": "error", "message": str(e)}, status_code=500)
|
161 |
+
|
162 |
+
|
163 |
+
@app.post("/api/read_barcode")
|
164 |
+
async def read_barcode(image: UploadFile = File(...)):
|
165 |
+
try:
|
166 |
+
file_path = await save_upload_file(image)
|
167 |
+
|
168 |
+
ocrResult = ocr_barcode(file_path.encode('utf-8'))
|
169 |
+
ocrResDict = json.loads(ocrResult)
|
170 |
+
|
171 |
+
os.remove(file_path)
|
172 |
+
|
173 |
+
return JSONResponse(content={"status": "ok", "data": ocrResDict}, status_code=200)
|
174 |
+
|
175 |
+
except Exception as e:
|
176 |
+
return JSONResponse(content={"status": "error", "message": str(e)}, status_code=500)
|
177 |
+
|
178 |
+
@app.post("/api/read_barcode_base64")
|
179 |
+
async def read_barcode_base64(request_data: ImageBase64Request):
|
180 |
+
try:
|
181 |
+
file_path = await save_base64_file(request_data.image)
|
182 |
+
|
183 |
+
# Your OCR function (pass bytes-encoded file path)
|
184 |
+
ocr_result = ocr_barcode(file_path.encode('utf-8'))
|
185 |
+
ocr_res_dict = json.loads(ocr_result)
|
186 |
+
|
187 |
+
# Remove the temp file
|
188 |
+
os.remove(file_path)
|
189 |
+
|
190 |
+
return JSONResponse(content={"status": "ok", "data": ocr_res_dict}, status_code=200)
|
191 |
+
|
192 |
+
except Exception as e:
|
193 |
+
return JSONResponse(content={"status": "error", "message": str(e)}, status_code=500)
|
194 |
+
|
195 |
+
|
196 |
+
if __name__ == '__main__':
|
197 |
+
ret = activate_sdk()
|
198 |
+
if ret != 0:
|
199 |
+
exit(-1)
|
200 |
+
|
201 |
+
dummy_interface = gr.Interface(
|
202 |
+
fn=lambda x: "API ready.",
|
203 |
+
inputs=gr.Textbox(label="Info"),
|
204 |
+
outputs=gr.Textbox(label="Response"),
|
205 |
+
allow_flagging="never" # 🚫 disables writing to `flagged/`
|
206 |
+
)
|
207 |
+
|
208 |
+
gr_app = gr.mount_gradio_app(app, dummy_interface, path="/gradio")
|
209 |
+
|
210 |
+
import uvicorn
|
211 |
+
uvicorn.run(gr_app, host="0.0.0.0", port=9000)
|
|