hassan526 commited on
Commit
75a2847
·
verified ·
1 Parent(s): 411dea9

Upload 21 files

Browse files
.gitattributes CHANGED
@@ -33,3 +33,13 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ dependency/libimutils.so filter=lfs diff=lfs merge=lfs -text
37
+ dependency/libimutils.so_for_ubuntu22 filter=lfs diff=lfs merge=lfs -text
38
+ dependency/libttvcore.so filter=lfs diff=lfs merge=lfs -text
39
+ engine/libMetaChecker.so filter=lfs diff=lfs merge=lfs -text
40
+ engine/libOCR.so filter=lfs diff=lfs merge=lfs -text
41
+ examples/1_b.png filter=lfs diff=lfs merge=lfs -text
42
+ examples/1_f.png filter=lfs diff=lfs merge=lfs -text
43
+ examples/2_b.png filter=lfs diff=lfs merge=lfs -text
44
+ examples/2_f.png filter=lfs diff=lfs merge=lfs -text
45
+ examples/4.png filter=lfs diff=lfs merge=lfs -text
Dockerfile ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM ubuntu:22.04
2
+ RUN ln -snf /usr/share/zoneinfo/$CONTAINER_TIMEZONE /etc/localtime && echo $CONTAINER_TIMEZONE > /etc/timezone
3
+ RUN apt-get update && \
4
+ apt-get install -y binutils python3 python3-pip python3-opencv libcurl4-openssl-dev libssl-dev libpcsclite-dev psmisc && \
5
+ rm -rf /var/lib/apt/lists/*
6
+
7
+ RUN useradd -m -u 1000 user
8
+ USER user
9
+ ENV PATH="/home/user/.local/bin:$PATH"
10
+
11
+ WORKDIR /app
12
+ COPY --chown=user . .
13
+ COPY --chown=user ./dependency/libimutils.so_for_ubuntu22 /usr/lib/libimutils.so
14
+ COPY --chown=user ./dependency/libttvcore.so /usr/lib
15
+
16
+ RUN pip3 install gradio==5.23.2 opencv-python numpy fastapi
17
+ RUN chmod a+x run_demo.sh
18
+
19
+ EXPOSE 9000 7860
20
+
21
+ ENTRYPOINT ["./run_demo.sh"]
22
+
app.py ADDED
@@ -0,0 +1,212 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ sys.path.append('../')
3
+
4
+ import os
5
+ import base64
6
+ import json
7
+ import uuid
8
+ import cv2
9
+ import numpy as np
10
+ import gradio as gr
11
+ from time import gmtime, strftime
12
+ from pydantic import BaseModel
13
+ from fastapi import FastAPI, File, UploadFile
14
+ from fastapi.responses import JSONResponse
15
+ from typing import Dict
16
+
17
+ from engine.header import *
18
+
19
+ file_path = os.path.abspath(__file__)
20
+ dir_path = os.path.dirname(file_path)
21
+ root_path = os.path.dirname(dir_path)
22
+ dump_path = os.path.join(root_path, "dump2/")
23
+
24
+ device_id = get_deviceid().decode('utf-8')
25
+ print_info('\t <Hardware ID> \t\t {}'.format(device_id))
26
+
27
+ def activate_sdk():
28
+ online_key = os.environ.get("LICENSE_KEY")
29
+ offline_key_path = os.path.join(root_path, "license.txt")
30
+
31
+ dict_path = os.path.join(root_path, "engine/bin")
32
+
33
+ ret = -1
34
+ if online_key is None:
35
+ print_warning("Online license key not found!")
36
+ else:
37
+ activate_ret = set_activation(online_key.encode('utf-8')).decode('utf-8')
38
+ ret = json.loads(activate_ret).get("errorCode", None)
39
+
40
+ if ret == 0:
41
+ print_log("Successfully online activation SDK!")
42
+ else:
43
+ print_error(f"Failed to online activation SDK, Error code {ret}\n Trying offline activation SDK...");
44
+ if os.path.exists(offline_key_path) is False:
45
+ print_warning("Offline license key file not found!")
46
+ print_error(f"Falied to offline activation SDK, Error code {ret}")
47
+ return ret
48
+ else:
49
+ file=open(offline_key_path,"r")
50
+ offline_key = file.read()
51
+ file.close()
52
+ activate_ret = set_activation(offline_key.encode('utf-8')).decode('utf-8')
53
+ ret = json.loads(activate_ret).get("errorCode", None)
54
+ if ret == 0:
55
+ print_log("Successfully offline activation SDK!")
56
+ else:
57
+ print_error(f"Falied to offline activation SDK, Error code {ret}")
58
+ return ret
59
+
60
+ init_ret = init_sdk(dict_path.encode('utf-8')).decode('utf-8')
61
+ ret = json.loads(activate_ret).get("errorCode", None)
62
+ print_log(f"Init SDK: {ret}")
63
+ return ret
64
+
65
+
66
+ async def save_upload_file(upload_file: UploadFile) -> str:
67
+ file_name = uuid.uuid4().hex[:6] + "_" + upload_file.filename
68
+ save_path = os.path.join(dump_path, file_name)
69
+ with open(save_path, "wb") as buffer:
70
+ shutil.copyfileobj(upload_file.file, buffer)
71
+ return os.path.abspath(save_path)
72
+
73
+ async def save_base64_file(base64_string: str) -> str:
74
+ file_name = uuid.uuid4().hex[:6] + ".jpg" # or ".png" depending on your use
75
+ save_path = os.path.join(dump_path, file_name)
76
+
77
+ with open(save_path, "wb") as buffer:
78
+ buffer.write(base64.b64decode(base64_string))
79
+
80
+ return os.path.abspath(save_path)
81
+
82
+ app = FastAPI()
83
+
84
+ class ImageBase64Request(BaseModel):
85
+ image: str
86
+
87
+ @app.get("/")
88
+ def read_root():
89
+ return {"status": "API is running"}
90
+
91
+ @app.post("/api/read_idcard")
92
+ async def read_idcard(image: UploadFile = File(...), image2: UploadFile = File(None)):
93
+ try:
94
+ file_path1 = await save_upload_file(image)
95
+
96
+ file_path2 = ""
97
+ if image2 is not None:
98
+ file_path2 = await save_upload_file(image2)
99
+
100
+
101
+ ocrResult = ocr_id_card(file_path1.encode('utf-8'), file_path2.encode('utf-8'))
102
+ ocrResDict = json.loads(ocrResult)
103
+
104
+ os.remove(file_path1)
105
+ if file_path2:
106
+ os.remove(file_path2)
107
+
108
+ return JSONResponse(content={"status": "ok", "data": ocrResDict}, status_code=200)
109
+
110
+ except Exception as e:
111
+ return JSONResponse(content={"status": "error", "message": str(e)}, status_code=500)
112
+
113
+
114
+ @app.post("/api/read_idcard_base64")
115
+ async def read_idcard_base64(request_data: ImageBase64Request):
116
+ try:
117
+ file_path = await save_base64_file(request_data.image)
118
+
119
+ # Your OCR function (pass bytes-encoded file path)
120
+ ocr_result = ocr_id_card(file_path.encode('utf-8'), ''.encode('utf-8'))
121
+ ocr_res_dict = json.loads(ocr_result)
122
+
123
+ # Remove the temp file
124
+ os.remove(file_path)
125
+
126
+ return JSONResponse(content={"status": "ok", "data": ocr_res_dict}, status_code=200)
127
+
128
+ except Exception as e:
129
+ return JSONResponse(content={"status": "error", "message": str(e)}, status_code=500)
130
+
131
+ @app.post("/api/read_credit")
132
+ async def read_credit(image: UploadFile = File(...)):
133
+ try:
134
+ file_path = await save_upload_file(image)
135
+
136
+ ocrResult = ocr_credit_card(file_path.encode('utf-8'))
137
+ ocrResDict = json.loads(ocrResult)
138
+
139
+ os.remove(file_path)
140
+
141
+ return JSONResponse(content={"status": "ok", "data": ocrResDict}, status_code=200)
142
+
143
+ except Exception as e:
144
+ return JSONResponse(content={"status": "error", "message": str(e)}, status_code=500)
145
+
146
+ @app.post("/api/read_credit_base64")
147
+ async def read_credit_base64(request_data: ImageBase64Request):
148
+ try:
149
+ file_path = await save_base64_file(request_data.image)
150
+
151
+ # Your OCR function (pass bytes-encoded file path)
152
+ ocr_result = ocr_credit_card(file_path.encode('utf-8'))
153
+ ocr_res_dict = json.loads(ocr_result)
154
+
155
+ # Remove the temp file
156
+ os.remove(file_path)
157
+
158
+ return JSONResponse(content={"status": "ok", "data": ocr_res_dict}, status_code=200)
159
+
160
+ except Exception as e:
161
+ return JSONResponse(content={"status": "error", "message": str(e)}, status_code=500)
162
+
163
+
164
+ @app.post("/api/read_barcode")
165
+ async def read_barcode(image: UploadFile = File(...)):
166
+ try:
167
+ file_path = await save_upload_file(image)
168
+
169
+ ocrResult = ocr_barcode(file_path.encode('utf-8'))
170
+ ocrResDict = json.loads(ocrResult)
171
+
172
+ os.remove(file_path)
173
+
174
+ return JSONResponse(content={"status": "ok", "data": ocrResDict}, status_code=200)
175
+
176
+ except Exception as e:
177
+ return JSONResponse(content={"status": "error", "message": str(e)}, status_code=500)
178
+
179
+ @app.post("/api/read_barcode_base64")
180
+ async def read_barcode_base64(request_data: ImageBase64Request):
181
+ try:
182
+ file_path = await save_base64_file(request_data.image)
183
+
184
+ # Your OCR function (pass bytes-encoded file path)
185
+ ocr_result = ocr_barcode(file_path.encode('utf-8'))
186
+ ocr_res_dict = json.loads(ocr_result)
187
+
188
+ # Remove the temp file
189
+ os.remove(file_path)
190
+
191
+ return JSONResponse(content={"status": "ok", "data": ocr_res_dict}, status_code=200)
192
+
193
+ except Exception as e:
194
+ return JSONResponse(content={"status": "error", "message": str(e)}, status_code=500)
195
+
196
+
197
+ if __name__ == '__main__':
198
+ ret = activate_sdk()
199
+ if ret != 0:
200
+ exit(-1)
201
+
202
+ dummy_interface = gr.Interface(
203
+ fn=lambda x: "API ready.",
204
+ inputs=gr.Textbox(label="Info"),
205
+ outputs=gr.Textbox(label="Response"),
206
+ allow_flagging="never" # 🚫 disables writing to `flagged/`
207
+ )
208
+
209
+ gr_app = gr.mount_gradio_app(app, dummy_interface, path="/gradio")
210
+
211
+ import uvicorn
212
+ uvicorn.run(gr_app, host="0.0.0.0", port=9000)
dependency/libimutils.so ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:260e9ecead1a8ef7598ee383c792bacc3dfeeca28f63a602e4e5f32210517bce
3
+ size 412424
dependency/libimutils.so_for_ubuntu22 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:41a366e1d555901c6a3207d9d0948c72f71f2f7c40d3d7a3f889e2fa2049f0fb
3
+ size 411600
dependency/libttvcore.so ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1334d5ca8b298b06d655cf2044174feb8c977d39950e21fbc2a7a19219363896
3
+ size 97975464
dump2/.gitignore ADDED
File without changes
engine/bin/data1.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:38182bc12601153ae6c791fb73ddabc5ddb9bc841e3a622d2eec1346706c1658
3
+ size 190945284
engine/bin/data2.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5f73ca54d3d76ac32ffaa3005a427d36372c1405931a176f3247af231519393d
3
+ size 15719219
engine/bin/data3.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5c139b9935c79396a6be062c2fd4f3572f771a6c575ff4699c56d3777a91deba
3
+ size 197236740
engine/bin/data4.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:18cd11b5750f9b5b3e94bba007d77f202fd089415904a8aaeddd293d7ab45b8f
3
+ size 133390117
engine/header.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import numpy as np
4
+ import ctypes, ctypes.util
5
+ from enum import Enum
6
+ from ctypes import *
7
+ from numpy.ctypeslib import ndpointer
8
+
9
+ def print_log(fmt): print("[LOG] \033[98m{}\033[00m" .format(fmt))
10
+ def print_info(fmt): print("[INFO] \033[92m{}\033[00m" .format(fmt))
11
+ def print_error(fmt): print("[ERR] \033[91m{}\033[00m" .format(fmt))
12
+ def print_warning(fmt): print("[WARNING] \033[93m{}\033[00m" .format(fmt))
13
+
14
+
15
+ ocr_path = os.path.abspath(os.path.dirname(__file__)) + '/libOCR.so'
16
+ ocr_engine = cdll.LoadLibrary(ocr_path)
17
+
18
+ get_deviceid = ocr_engine.TTVOcrGetHWID
19
+ get_deviceid.argtypes = []
20
+ get_deviceid.restype = ctypes.c_char_p
21
+
22
+ set_activation = ocr_engine.TTVOcrSetActivation
23
+ set_activation.argtypes = []
24
+ set_activation.restype = ctypes.c_char_p
25
+
26
+ init_sdk = ocr_engine.TTVOcrInit
27
+ init_sdk.argtypes = [ctypes.c_char_p]
28
+ init_sdk.restype = ctypes.c_char_p
29
+
30
+ ocr_id_card = ocr_engine.TTVOcrProcess
31
+ ocr_id_card.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
32
+ ocr_id_card.restype = ctypes.c_char_p
33
+
34
+ ocr_credit_card = ocr_engine.TTVOcrCreditCard
35
+ ocr_credit_card.argtypes = [ctypes.c_char_p]
36
+ ocr_credit_card.restype = ctypes.c_char_p
37
+
38
+ ocr_barcode = ocr_engine.TTVOcrBarCode
39
+ ocr_barcode.argtypes = [ctypes.c_char_p]
40
+ ocr_barcode.restype = ctypes.c_char_p
41
+
42
+ metacheck_path = os.path.abspath(os.path.dirname(__file__)) + '/libMetaChecker.so'
43
+ meta_engine = cdll.LoadLibrary(metacheck_path)
44
+
45
+ check_meta = meta_engine.ttv_if_checker
46
+ check_meta.argtypes = [ctypes.c_char_p]
47
+ check_meta.restype = ctypes.c_int32
engine/libMetaChecker.so ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e5e3b916d3674b7b41a0b8f3f1a6331d83057a37012074e2c119362fcb9dd5a9
3
+ size 1613856
engine/libOCR.so ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3bd0860a5deec1e399c4df11659207eedab799c1c9bb44b15bc6d6cd3f099584
3
+ size 1529136
examples/1_b.png ADDED

Git LFS Details

  • SHA256: 3dd9689e0bd52177129f0cff59b720c427476cb5938737436104ca54cbd15f33
  • Pointer size: 131 Bytes
  • Size of remote file: 118 kB
examples/1_f.png ADDED

Git LFS Details

  • SHA256: c4d1e678fe8fc28ed0099bc6cb2f01367c975e97dfc1f334a9647e1c7ec85c01
  • Pointer size: 131 Bytes
  • Size of remote file: 112 kB
examples/2_b.png ADDED

Git LFS Details

  • SHA256: 4dc0344f3e049e3e13fdcc55cac153a8cf872a6c39ec000060bda21b9b7c2a6a
  • Pointer size: 131 Bytes
  • Size of remote file: 385 kB
examples/2_f.png ADDED

Git LFS Details

  • SHA256: 6ba4b503c3761dc6e4e848cce92f08ee2e395aa30bc3d6626b55577ed04f9601
  • Pointer size: 131 Bytes
  • Size of remote file: 431 kB
examples/3_b.png ADDED
examples/3_f.png ADDED
examples/4.png ADDED

Git LFS Details

  • SHA256: 4999a6bf409bfa7409eec276bfbd3e714610d6326bf31b8aba12829a41cc790b
  • Pointer size: 131 Bytes
  • Size of remote file: 617 kB
run_demo.sh ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/sh
2
+
3
+ # Add the current directory to PYTHONPATH
4
+ export PYTHONPATH=$(dirname "$(pwd)"):$PYTHONPATH
5
+
6
+ # Set LD_LIBRARY_PATH
7
+ # export LD_LIBRARY_PATH="/usr/lib/openvino:$LD_LIBRARY_PATH"
8
+
9
+ python3 app.py
10
+