-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
65 lines (52 loc) · 1.87 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
from flask import Flask, request, jsonify
from eagle_ocr_model import model
from PIL import Image, ExifTags
import numpy as np
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def exe_ocr():
"""
OCR算法服务接口
:return: json数据, 字典结构, 识别结果, 包括文字框坐标和识别文字
文字框坐标顺序左上, 右上, 左下, 右下
识别文字与文字框坐标对应
{
'ocr_text_res': ocr_text_res,
'ocr_region_res': ocr_region_res
}
"""
if request.method == 'POST':
img_path = request.form.get('img_path')
detection = request.form.get('detection')
if not detection:
detection = 'ctpn'
# 读取图片
im = Image.open(img_path)
# 防止图片旋转
try:
for orientation in ExifTags.TAGS.keys():
if ExifTags.TAGS[orientation] == 'Orientation':
break
exif = dict(im._getexif().items())
if exif[orientation] == 3:
im = im.rotate(180, expand=True)
elif exif[orientation] == 6:
im = im.rotate(270, expand=True)
elif exif[orientation] == 8:
im = im.rotate(90, expand=True)
except:
pass
img = np.array(im.convert('RGB'))
# 执行ocr获得结果
result, img, angle, ocr_region_res = model.model(
img, img_path, model='pytorch', adjust=False, detectAngle=False, detect=detection)
ocr_text_res = []
for i, key in enumerate(result):
ocr_text_res.append(result[key][1])
response_data = {
'ocr_text_res': ocr_text_res,
'ocr_region_res': ocr_region_res
}
return jsonify(response_data)
if __name__ == '__main__':
app.run()