-
Notifications
You must be signed in to change notification settings - Fork 13
/
recognition.py
76 lines (61 loc) · 1.7 KB
/
recognition.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
from flask import Flask, request, abort
import urllib
import numpy as np
import cv2
import easyocr
import os
SECRET_KEY = os.getenv('SECRET_KEY', 'easyocr_vdt');
reader = easyocr.Reader(['en'], gpu=False)
app = Flask(__name__)
def url_to_image(url):
"""
download the image, convert it to a NumPy array, and then read it into OpenCV format
:param url: url to the image
:return: image in format of Opencv
"""
resp = urllib.request.urlopen(url)
image = np.asarray(bytearray(resp.read()), dtype="uint8")
print("url = ", url)
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
return image
def data_process(data):
"""
read params from the received data
:param data: in json format
:return: params for image processing
"""
image_url = data["image_url"]
secret_key = data["secret_key"]
return url_to_image(image_url), secret_key
def recognition(image):
"""
:param image:
:return:
"""
results = []
texts = reader.readtext(image)
for (bbox, text, prob) in texts:
output = {
"coordinate": [list(map(float, coordinate)) for coordinate in bbox],
"text": text,
"score": prob
}
results.append(output)
return results
@app.route('/ocr', methods=['GET', 'POST'])
def process():
"""
received request from client and process the image
:return: dict of width and points
"""
data = request.get_json()
image, secret_key = data_process(data)
if secret_key == SECRET_KEY:
results = recognition(image)
return {
"results": results
}
else:
abort(401)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=2000)