-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain_api.py
54 lines (43 loc) · 1.65 KB
/
main_api.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
import config
if config.INFERENCE_ENGINE_TYPE == 'opencv':
import text_detection_cv as text_detection
import text_recognition_cv as text_recognition
else:
import text_detection_ie as text_detection
import text_recognition_ie as text_recognition
import cv2
from flask import Flask, Response, request
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
import numpy as np
import json
app = Flask(__name__)
td = text_detection.PixelLinkDecoder()
tr = text_recognition.TextRecognizer()
class NumpyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.ndarray):
return obj.tolist()
return json.JSONEncoder.default(self, obj)
# route http posts to this method
@app.route('/ocr', methods=['POST', 'GET'])
def main():
r = request
# convert string of image data to uint8
nparr = np.fromstring(r.data, np.uint8)
# decode image
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
image_show, bounding_rects = td.inference(image)
texts = tr.inference(image, bounding_rects)
result = dict(zip(texts, bounding_rects))
response = json.dumps(result, cls=NumpyEncoder)
return Response(response=response, status=200, mimetype="application/json")
if __name__ == '__main__':
if config.SERVER_TYPE == 'wsgi':
from gevent.pywsgi import WSGIServer
WSGIServer((config.IP, config.PORT), app).serve_forever()
elif config.SERVER_TYPE == 'flask':
app.run(config.IP, config.PORT, debug=True, threaded=True)
else:
raise Exception('server type not found')