|
0 |
# -*- coding: utf-8 -*-
|
|
1 |
"""
|
|
2 |
@author: lywen
|
|
3 |
"""
|
|
4 |
import os
|
|
5 |
import cv2
|
|
6 |
import json
|
|
7 |
import time
|
|
8 |
import uuid
|
|
9 |
import base64
|
|
10 |
import web
|
|
11 |
from PIL import Image
|
|
12 |
web.config.debug = True
|
|
13 |
import model
|
|
14 |
render = web.template.render('templates', base='base')
|
|
15 |
from config import DETECTANGLE
|
|
16 |
from apphelper.image import union_rbox,adjust_box_to_origin
|
|
17 |
from application import trainTicket,idcard
|
|
18 |
|
|
19 |
|
|
20 |
billList = ['通用OCR','火车票','身份证']
|
|
21 |
|
|
22 |
class OCR:
|
|
23 |
"""通用OCR识别"""
|
|
24 |
|
|
25 |
def GET(self):
|
|
26 |
post = {}
|
|
27 |
post['postName'] = 'ocr'##请求地址
|
|
28 |
post['height'] = 1000
|
|
29 |
post['H'] = 1000
|
|
30 |
post['width'] = 600
|
|
31 |
post['W'] = 600
|
|
32 |
post['uuid'] = uuid.uuid1().__str__()
|
|
33 |
post['billList'] = billList
|
|
34 |
return render.ocr(post)
|
|
35 |
|
|
36 |
def POST(self):
|
|
37 |
data = web.data()
|
|
38 |
data = json.loads(data)
|
|
39 |
billModel = data.get('billModel','')
|
|
40 |
textAngle = data.get('textAngle',False)##文字检测
|
|
41 |
textLine = data.get('textLine',False)##只进行单行识别
|
|
42 |
|
|
43 |
imgString = data['imgString'].encode().split(b';base64,')[-1]
|
|
44 |
imgString = base64.b64decode(imgString)
|
|
45 |
jobid = uuid.uuid1().__str__()
|
|
46 |
path = 'test/{}.jpg'.format(jobid)
|
|
47 |
with open(path,'wb') as f:
|
|
48 |
f.write(imgString)
|
|
49 |
img = cv2.imread(path)##GBR
|
|
50 |
H,W = img.shape[:2]
|
|
51 |
timeTake = time.time()
|
|
52 |
if textLine:
|
|
53 |
##单行识别
|
|
54 |
partImg = Image.fromarray(img)
|
|
55 |
text = model.crnnOcr(partImg.convert('L'))
|
|
56 |
res =[ {'text':text,'name':'0','box':[0,0,W,0,W,H,0,H]} ]
|
|
57 |
else:
|
|
58 |
detectAngle = textAngle
|
|
59 |
_,result,angle= model.model(img,
|
|
60 |
detectAngle=detectAngle,##是否进行文字方向检测,通过web传参控制
|
|
61 |
config=dict(MAX_HORIZONTAL_GAP=50,##字符之间的最大间隔,用于文本行的合并
|
|
62 |
MIN_V_OVERLAPS=0.6,
|
|
63 |
MIN_SIZE_SIM=0.6,
|
|
64 |
TEXT_PROPOSALS_MIN_SCORE=0.1,
|
|
65 |
TEXT_PROPOSALS_NMS_THRESH=0.3,
|
|
66 |
TEXT_LINE_NMS_THRESH = 0.7,##文本行之间测iou值
|
|
67 |
),
|
|
68 |
leftAdjust=True,##对检测的文本行进行向左延伸
|
|
69 |
rightAdjust=True,##对检测的文本行进行向右延伸
|
|
70 |
alph=0.01,##对检测的文本行进行向右、左延伸的倍数
|
|
71 |
)
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
if billModel=='' or billModel=='通用OCR' :
|
|
76 |
result = union_rbox(result,0.2)
|
|
77 |
res = [{'text':x['text'],
|
|
78 |
'name':str(i),
|
|
79 |
'box':{'cx':x['cx'],
|
|
80 |
'cy':x['cy'],
|
|
81 |
'w':x['w'],
|
|
82 |
'h':x['h'],
|
|
83 |
'angle':x['degree']
|
|
84 |
|
|
85 |
}
|
|
86 |
} for i,x in enumerate(result)]
|
|
87 |
res = adjust_box_to_origin(img,angle, res)##修正box
|
|
88 |
|
|
89 |
elif billModel=='火车票':
|
|
90 |
res = trainTicket.trainTicket(result)
|
|
91 |
res = res.res
|
|
92 |
res =[ {'text':res[key],'name':key,'box':{}} for key in res]
|
|
93 |
|
|
94 |
elif billModel=='身份证':
|
|
95 |
|
|
96 |
res = idcard.idcard(result)
|
|
97 |
res = res.res
|
|
98 |
res =[ {'text':res[key],'name':key,'box':{}} for key in res]
|
|
99 |
|
|
100 |
|
|
101 |
timeTake = time.time()-timeTake
|
|
102 |
|
|
103 |
|
|
104 |
os.remove(path)
|
|
105 |
return json.dumps({'res':res,'timeTake':round(timeTake,4)},ensure_ascii=False)
|
|
106 |
|
|
107 |
|
|
108 |
urls = ('/ocr','OCR',)
|
|
109 |
|
|
110 |
if __name__ == "__main__":
|
|
111 |
|
|
112 |
app = web.application(urls, globals())
|
|
113 |
app.run()
|