forked from Sanster/DeepOcrService
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
93 lines (65 loc) · 2.21 KB
/
app.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
import io
import os
import time
import cv2
import numpy as np
from flask import Flask, Response, json, request, g, send_from_directory
from flask_cors import CORS
from detector import Detector
from recoer import Recoer
detector = Detector('./data/models/ctpn.pb')
recoer = Recoer('./tf_crnn/data/chars/chn.txt', './data/models/crnn.pb')
app = Flask(__name__, static_folder='web/build')
CORS(app)
def responseJson(data):
return Response(json.dumps(data), mimetype='application/json')
def get_cv_img(r):
f = r.files['img']
in_memory_file = io.BytesIO()
f.save(in_memory_file)
nparr = np.fromstring(in_memory_file.getvalue(), np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
return img
@app.before_request
def before_request():
g.request_start_time = time.time()
@app.after_request
def after_request(r):
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
r.headers['Cache-Control'] = 'public, max-age=0'
print("request time: {:.03f}s".format(time.time() - g.request_start_time))
return r
@app.route("/ocr", methods=['POST'])
def ocr():
img = get_cv_img(request)
print('Image size: {}x{}'.format(img.shape[1], img.shape[0]))
res = process(img)
return responseJson(res)
# Serve React App
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def serve(path):
if path != "" and os.path.exists("web/build/" + path):
return send_from_directory('web/build', path)
else:
return send_from_directory('web/build', 'index.html')
def process(img):
start_time = time.time()
rois = detector.detect(img)
print("CTPN time: %.03fs" % (time.time() - start_time))
start_time = time.time()
ocr_result = recoer.recognize(rois, img)
print("CRNN time: %.03fs" % (time.time() - start_time))
sorted_data = sorted(zip(rois, ocr_result), key=lambda x: x[0][1] + x[0][3] / 2)
rois, ocr_result = zip(*sorted_data)
res = {"results": []}
for i in range(len(rois)):
res["results"].append({
'position': rois[i],
'text': ocr_result[i]
})
return res
if __name__ == "__main__":
app.run(host='0.0.0.0')