Turing311 commited on
Commit
d30a373
·
1 Parent(s): 39f05fa

Upload 12 files

Browse files
.gitattributes CHANGED
@@ -33,3 +33,6 @@ 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
+ ocrengine/libttvcore.so filter=lfs diff=lfs merge=lfs -text
37
+ ocrengine/libttvifchecker.so filter=lfs diff=lfs merge=lfs -text
38
+ ocrengine/libttvocrengine.so filter=lfs diff=lfs merge=lfs -text
Dockerfile ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.8-slim
2
+ RUN apt-get update -y
3
+ RUN apt-get install -y libpcsclite-dev psmisc
4
+ RUN mkdir -p /home/faceonlive_iddocrecog
5
+ WORKDIR /home/faceonlive_iddocrecog
6
+ COPY ./requirements.txt .
7
+ COPY ./ocrengine .
8
+ COPY ./app.py .
9
+ COPY ./demo.py .
10
+ RUN pip3 install -r requirements.txt
11
+ CMD [ "python3", "app.py"]
12
+ EXPOSE 8080
app.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ sys.path.append('.')
3
+
4
+ from flask import Flask, render_template, request, jsonify, send_from_directory
5
+ from time import gmtime, strftime
6
+ import os
7
+ import base64
8
+ import json
9
+ import uuid
10
+ import cv2
11
+ import numpy as np
12
+
13
+ from ocrengine.ocrengine import TTVOcrGetHWID
14
+ from ocrengine.ocrengine import TTVOcrSetActivation
15
+ from ocrengine.ocrengine import TTVOcrInit
16
+ from ocrengine.ocrengine import TTVOcrProcess
17
+ from ocrengine.ocrengine import TTVOcrCreditCard
18
+ from ocrengine.ocrengine import TTVOcrBarCode
19
+ from ocrengine.ocrengine import ttv_if_checker
20
+
21
+ # Set the template and static folder to the client build
22
+ app = Flask(__name__, template_folder="client/build", static_folder="client/build/static")
23
+
24
+ ocrHWID = TTVOcrGetHWID()
25
+ print('ocr hwid: ', ocrHWID.decode('utf-8'))
26
+
27
+ licensePath = os.path.abspath(os.path.dirname(__file__)) + '/ocrengine/dict/license.txt'
28
+ ocrRet = TTVOcrSetActivation(licensePath.encode('utf-8'))
29
+ print('ocr activation: ', ocrRet.decode('utf-8'))
30
+
31
+ dictPath = os.path.abspath(os.path.dirname(__file__)) + '/ocrengine/dict'
32
+ ocrRet = TTVOcrInit(dictPath.encode('utf-8'))
33
+ print('ocr engine init: ', ocrRet.decode('utf-8'))
34
+
35
+ @app.route('/ocr/idcard', methods=['POST'])
36
+ def ocr_idcard():
37
+ print(request.files)
38
+ file1 = request.files['image1']
39
+
40
+ file_name1 = uuid.uuid4().hex[:6]
41
+ save_path1 = 'dump/' + file_name1 + '_' + file1.filename
42
+ file1.save(save_path1)
43
+
44
+ file_path1 = os.path.abspath(save_path1)
45
+
46
+ if 'image2' not in request.files:
47
+ file_path2 = ''
48
+ else:
49
+ file2 = request.files['image2']
50
+
51
+ file_name2 = uuid.uuid4().hex[:6]
52
+ save_path2 = 'dump/' + file_name2 + '_' + file2.filename
53
+ file2.save(save_path2)
54
+ print(file2.filename)
55
+
56
+ file_path2 = os.path.abspath(save_path2)
57
+
58
+
59
+ ocrResult = TTVOcrProcess(file_path1.encode('utf-8'), file_path2.encode('utf-8'))
60
+ ocrResDict = json.loads(ocrResult)
61
+ status = "ok"
62
+
63
+ if_check = ttv_if_checker(file_path1.encode('utf-8'))
64
+ response = jsonify({"status": status, "data": ocrResDict, "authenticity": if_check})
65
+
66
+ os.remove(file_path1)
67
+ if 'image2' in request.files:
68
+ os.remove(file_path2)
69
+
70
+ response.status_code = 200
71
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
72
+ return response
73
+
74
+ @app.route('/ocr/idcard_base64', methods=['POST'])
75
+ def ocr_idcard_base64():
76
+ content = request.get_json()
77
+ imageBase64 = content['image']
78
+
79
+ file_name = uuid.uuid4().hex[:6]
80
+ save_path = 'dump/' + file_name
81
+ with open(save_path, "wb") as fh:
82
+ fh.write(base64.b64decode(imageBase64))
83
+
84
+ file_path = os.path.abspath(save_path)
85
+
86
+ ocrResult = TTVOcrProcess(file_path.encode('utf-8'))
87
+ ocrResDict = json.loads(ocrResult)
88
+ status = "ok"
89
+
90
+ if_check = ttv_if_checker(file_path.encode('utf-8'))
91
+ response = jsonify({"status": status, "data": ocrResDict, "authenticity": if_check})
92
+
93
+ os.remove(file_path)
94
+
95
+ response.status_code = 200
96
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
97
+ return response
98
+
99
+
100
+ @app.route('/ocr/credit', methods=['POST'])
101
+ def ocr_credit():
102
+ file = request.files['image']
103
+ print('ocr_credit ', file)
104
+
105
+ image = cv2.imdecode(np.fromstring(file.read(), np.uint8), cv2.IMREAD_COLOR)
106
+ file_name = uuid.uuid4().hex[:6]
107
+ save_path = 'dump/' + file_name + '.png'
108
+ cv2.imwrite(save_path, image)
109
+
110
+ file_path = os.path.abspath(save_path)
111
+
112
+ ocrResult = TTVOcrCreditCard(file_path.encode('utf-8'))
113
+ ocrResDict = json.loads(ocrResult)
114
+ status = "ok"
115
+
116
+ response = jsonify({"status": status, "data": ocrResDict})
117
+
118
+ os.remove(file_path)
119
+
120
+ response.status_code = 200
121
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
122
+ return response
123
+
124
+ @app.route('/ocr/credit_base64', methods=['POST'])
125
+ def ocr_credit_base64():
126
+ print('ocr_credit_base64');
127
+ content = request.get_json()
128
+ imageBase64 = content['image']
129
+ image = cv2.imdecode(np.frombuffer(base64.b64decode(imageBase64), dtype=np.uint8), cv2.IMREAD_COLOR)
130
+
131
+ file_name = uuid.uuid4().hex[:6]
132
+ save_path = 'dump/' + file_name + '.png'
133
+ cv2.imwrite(save_path, image)
134
+
135
+ file_path = os.path.abspath(save_path)
136
+
137
+ ocrResult = TTVOcrCreditCard(file_path.encode('utf-8'))
138
+ ocrResDict = json.loads(ocrResult)
139
+ status = "ok"
140
+
141
+ response = jsonify({"status": status, "data": ocrResDict})
142
+
143
+ os.remove(file_path)
144
+
145
+ response.status_code = 200
146
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
147
+ return response
148
+
149
+ @app.route('/ocr/barcode', methods=['POST'])
150
+ def ocr_barcode():
151
+ file = request.files['image']
152
+ print('ocr_barcode ', file)
153
+
154
+ image = cv2.imdecode(np.fromstring(file.read(), np.uint8), cv2.IMREAD_COLOR)
155
+ file_name = uuid.uuid4().hex[:6]
156
+ save_path = 'dump/' + file_name + '.png'
157
+ cv2.imwrite(save_path, image)
158
+
159
+ file_path = os.path.abspath(save_path)
160
+
161
+ ocrResult = TTVOcrBarCode(file_path.encode('utf-8'))
162
+ ocrResDict = json.loads(ocrResult)
163
+ status = "ok"
164
+
165
+ response = jsonify({"status": status, "data": ocrResDict})
166
+
167
+ os.remove(file_path)
168
+
169
+ response.status_code = 200
170
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
171
+ return response
172
+
173
+ @app.route('/ocr/barcode_base64', methods=['POST'])
174
+ def ocr_barcode_base64():
175
+ content = request.get_json()
176
+ imageBase64 = content['image']
177
+ image = cv2.imdecode(np.frombuffer(base64.b64decode(imageBase64), dtype=np.uint8), cv2.IMREAD_COLOR)
178
+
179
+ file_name = uuid.uuid4().hex[:6]
180
+ save_path = 'dump/' + file_name + '.png'
181
+ cv2.imwrite(save_path, image)
182
+
183
+ file_path = os.path.abspath(save_path)
184
+ print('file_path: ', file_path)
185
+
186
+ ocrResult = TTVOcrBarCode(file_path.encode('utf-8'))
187
+ ocrResDict = json.loads(ocrResult)
188
+ status = "ok"
189
+
190
+ response = jsonify({"status": status, "data": ocrResDict})
191
+
192
+ os.remove(file_path)
193
+
194
+ response.status_code = 200
195
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
196
+
197
+ return response
198
+
199
+
200
+
201
+ if __name__ == '__main__':
202
+ port = int(os.environ.get("PORT", 8000))
203
+ app.run(host='0.0.0.0', port=port)
demo.py ADDED
@@ -0,0 +1,186 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import json
4
+ from PIL import Image
5
+
6
+ def idcard_recognition(frame1, frame2):
7
+ url = "http://127.0.0.1:8000/ocr/idcard"
8
+ files = None
9
+ if frame1 is not None and frame2 is not None:
10
+ files = {'image1': open(frame1, 'rb'), 'image2': open(frame2, 'rb')}
11
+ elif frame1 is not None and frame2 is None:
12
+ files = {'image1': open(frame1, 'rb')}
13
+ elif frame1 is None and frame2 is not None:
14
+ files = {'image1': open(frame2, 'rb')}
15
+ else:
16
+ return ['', None]
17
+
18
+ print(frame1, files)
19
+ r = requests.post(url=url, files=files)
20
+
21
+ images = None
22
+ resultValues = {}
23
+ table_value = ""
24
+ for key, value in r.json().items():
25
+
26
+ if key == 'data':
27
+ if 'image' in value:
28
+ del value['image']
29
+ resultValues[key] = value
30
+ else:
31
+ resultValues[key] = value
32
+
33
+
34
+ if 'data' in r.json():
35
+ for key, value in r.json()['data'].items():
36
+ if key == 'image':
37
+ for image_key, image_value in value.items():
38
+ row_value = ("<tr>"
39
+ "<td>{key}</td>"
40
+ "<td><img src=""data:image/png;base64,{base64_image} width = '200' height= '100' /></td>"
41
+ "</tr>".format(key=image_key, base64_image=image_value))
42
+ table_value = table_value + row_value
43
+
44
+ images = ("<table>"
45
+ "<tr>"
46
+ "<th>Field</th>"
47
+ "<th>Image</th>"
48
+ "</tr>"
49
+ "{table_value}"
50
+ "</table>".format(table_value=table_value))
51
+
52
+ json_result = json.dumps(resultValues, indent=4)
53
+ return [json_result, images]
54
+
55
+ def barcode_recognition(frame):
56
+ url = "http://127.0.0.1:8000/ocr/barcode"
57
+ files = None
58
+ if frame is None:
59
+ return ['', None]
60
+
61
+ files = {'image': open(frame, 'rb')}
62
+ r = requests.post(url=url, files=files)
63
+
64
+ images = None
65
+ resultValues = {}
66
+ table_value = ""
67
+ for key, value in r.json().items():
68
+
69
+ if key == 'data':
70
+ if 'image' in value:
71
+ del value['image']
72
+ resultValues[key] = value
73
+ else:
74
+ resultValues[key] = value
75
+
76
+
77
+ if 'data' in r.json():
78
+ for key, value in r.json()['data'].items():
79
+ if key == 'image':
80
+ for image_key, image_value in value.items():
81
+ row_value = ("<tr>"
82
+ "<td>{key}</td>"
83
+ "<td><img src=""data:image/png;base64,{base64_image} width = '200' height= '100' /></td>"
84
+ "</tr>".format(key=image_key, base64_image=image_value))
85
+ table_value = table_value + row_value
86
+
87
+ images = ("<table>"
88
+ "<tr>"
89
+ "<th>Field</th>"
90
+ "<th>Image</th>"
91
+ "</tr>"
92
+ "{table_value}"
93
+ "</table>".format(table_value=table_value))
94
+
95
+ json_result = json.dumps(resultValues, indent=4)
96
+ return [json_result, images]
97
+
98
+ def credit_recognition(frame):
99
+ url = "http://127.0.0.1:8000/ocr/credit"
100
+ files = None
101
+ if frame is None:
102
+ return ['', None]
103
+
104
+ files = {'image': open(frame, 'rb')}
105
+ r = requests.post(url=url, files=files)
106
+
107
+ images = None
108
+ resultValues = {}
109
+ table_value = ""
110
+ for key, value in r.json().items():
111
+
112
+ if key == 'data':
113
+ if 'image' in value:
114
+ del value['image']
115
+ resultValues[key] = value
116
+ else:
117
+ resultValues[key] = value
118
+
119
+
120
+ if 'data' in r.json():
121
+ for key, value in r.json()['data'].items():
122
+ if key == 'image':
123
+ for image_key, image_value in value.items():
124
+ row_value = ("<tr>"
125
+ "<td>{key}</td>"
126
+ "<td><img src=""data:image/png;base64,{base64_image} width = '200' height= '100' /></td>"
127
+ "</tr>".format(key=image_key, base64_image=image_value))
128
+ table_value = table_value + row_value
129
+
130
+ images = ("<table>"
131
+ "<tr>"
132
+ "<th>Field</th>"
133
+ "<th>Image</th>"
134
+ "</tr>"
135
+ "{table_value}"
136
+ "</table>".format(table_value=table_value))
137
+
138
+ json_result = json.dumps(resultValues, indent=4)
139
+ return [json_result, images]
140
+
141
+ with gr.Blocks() as demo:
142
+ gr.Markdown(
143
+ """
144
+ OCR Demo
145
+ """
146
+ )
147
+ with gr.TabItem("ID Card Recognition"):
148
+ with gr.Row():
149
+ with gr.Column(scale=3):
150
+ id_image_input1 = gr.Image(type='filepath')
151
+ id_image_input2 = gr.Image(type='filepath')
152
+ id_recognition_button = gr.Button("ID Card Recognition")
153
+ with gr.Column(scale=5):
154
+ id_result_output = gr.Textbox()
155
+
156
+ with gr.Column(scale=2):
157
+ image_result_output = gr.HTML()
158
+
159
+ id_recognition_button.click(idcard_recognition, inputs=[id_image_input1, id_image_input2], outputs=[id_result_output, image_result_output])
160
+ with gr.TabItem("Barcode Recognition"):
161
+ with gr.Row():
162
+ with gr.Column(scale=3):
163
+ barcode_image_input = gr.Image(type='filepath')
164
+ barcode_recognition_button = gr.Button("Barcode Recognition")
165
+ with gr.Column(scale=5):
166
+ barcode_result_output = gr.HTML()
167
+
168
+ with gr.Column(scale=2):
169
+ image_result_output = gr.HTML()
170
+
171
+ barcode_recognition_button.click(barcode_recognition, inputs=barcode_image_input, outputs=[barcode_result_output, image_result_output])
172
+
173
+ with gr.TabItem("Credit Card Recognition"):
174
+ with gr.Row():
175
+ with gr.Column(scale=3):
176
+ credit_image_input = gr.Image(type='filepath')
177
+ credit_recognition_button = gr.Button("Credit Card Recognition")
178
+ with gr.Column(scale=5):
179
+ credit_result_output = gr.HTML()
180
+
181
+ with gr.Column(scale=2):
182
+ image_result_output = gr.HTML()
183
+
184
+ credit_recognition_button.click(credit_recognition, inputs=credit_image_input, outputs=[credit_result_output, image_result_output])
185
+
186
+ demo.launch(server_name="0.0.0.0", server_port=9000)
ocrengine/dict/data1.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3984b15b29f28886e3ce8634b3fdc93633becb9f34a0c9aed152ce2b7126d5a9
3
+ size 142606336
ocrengine/dict/data2.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6bdd1d2f0701d443e771b04eef5adb7b752961b6ace6f60eb1c2c5d533a65fda
3
+ size 9212703
ocrengine/dict/data3.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0244637e40fa09ad753ab0299d39360acc8b69afd3883296def40aeaa984ec26
3
+ size 188743680
ocrengine/dict/data4.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7f7d151420087f4c2b5c7d47a7d37310ae4c40fc9f2cb129866e8ff1132c0d2f
3
+ size 96905216
ocrengine/libttvcore.so ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2ca8d29bc483e307ad8f4b2e92acc7b2a97460e0f8ddbbce05b8d7a7764d3c7c
3
+ size 95098400
ocrengine/libttvifchecker.so ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e5e3b916d3674b7b41a0b8f3f1a6331d83057a37012074e2c119362fcb9dd5a9
3
+ size 1613856
ocrengine/libttvocrengine.so ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3e6178a01edfc73c9ea15763cd731182cec3faff7a16f01bd6969166acced95e
3
+ size 3907352
ocrengine/ocrengine.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import ctypes, ctypes.util
2
+ from ctypes import *
3
+ from numpy.ctypeslib import ndpointer
4
+ import sys
5
+ import os
6
+
7
+ dll_path = os.path.abspath(os.path.dirname(__file__)) + '/libttvocrengine.so'
8
+ ocr_engine = cdll.LoadLibrary(dll_path)
9
+
10
+ TTVOcrInit = ocr_engine.TTVOcrInit
11
+ TTVOcrInit.argtypes = [ctypes.c_char_p]
12
+ TTVOcrInit.restype = ctypes.c_char_p
13
+
14
+ TTVOcrProcess = ocr_engine.TTVOcrProcess
15
+ TTVOcrProcess.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
16
+ TTVOcrProcess.restype = ctypes.c_char_p
17
+
18
+ TTVOcrCreditCard = ocr_engine.TTVOcrCreditCard
19
+ TTVOcrCreditCard.argtypes = [ctypes.c_char_p]
20
+ TTVOcrCreditCard.restype = ctypes.c_char_p
21
+
22
+ TTVOcrBarCode = ocr_engine.TTVOcrBarCode
23
+ TTVOcrBarCode.argtypes = [ctypes.c_char_p]
24
+ TTVOcrBarCode.restype = ctypes.c_char_p
25
+
26
+ TTVOcrGetHWID = ocr_engine.TTVOcrGetHWID
27
+ TTVOcrGetHWID.argtypes = []
28
+ TTVOcrGetHWID.restype = ctypes.c_char_p
29
+
30
+ TTVOcrSetActivation = ocr_engine.TTVOcrSetActivation
31
+ TTVOcrSetActivation.argtypes = []
32
+ TTVOcrSetActivation.restype = ctypes.c_char_p
33
+
34
+ dll_path = os.path.abspath(os.path.dirname(__file__)) + '/libttvifchecker.so'
35
+ if_engine = cdll.LoadLibrary(dll_path)
36
+
37
+ ttv_if_checker = if_engine.ttv_if_checker
38
+ ttv_if_checker.argtypes = [ctypes.c_char_p]
39
+ ttv_if_checker.restype = ctypes.c_int32
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ flask
2
+ flask-cors
3
+ gradio