forked from DayBreak-u/chineseocr_lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
132 lines (78 loc) · 3.58 KB
/
model.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from config import *
from crnn import FullCrnn,LiteCrnn,CRNNHandle
from psenet import PSENet,PSENetHandel
from angle_class import AangleClassHandle,shufflenet_v2_x0_5
from utils import rotate_cut_img,solve,sort_box,draw_bbox,crop_rect
from PIL import Image
import numpy as np
import cv2
if pse_model_type == "mobilenetv2":
text_detect_net = PSENet(backbone=pse_model_type, pretrained=False, result_num=6, scale=pse_scale)
text_handle = PSENetHandel(pse_model_path, text_detect_net, pse_scale, gpu_id=GPU_ID)
crnn_net = None
if crnn_type == "full_lstm" or crnn_type == "full_dense":
crnn_net = FullCrnn(32, 1, len(alphabet) + 1, nh, n_rnn=2, leakyRelu=False, lstmFlag=LSTMFLAG)
elif crnn_type == "lite_lstm" or crnn_type == "lite_dense":
crnn_net = LiteCrnn(32, 1, len(alphabet) + 1, nh, n_rnn=2, leakyRelu=False, lstmFlag=LSTMFLAG)
assert crnn_type is not None
crnn_handle = CRNNHandle(crnn_model_path , crnn_net , gpu_id=GPU_ID)
crnn_vertical_handle = None
if crnn_vertical_model_path is not None:
crnn_vertical_net = LiteCrnn(32, 1, len(alphabet) + 1, nh, n_rnn=2, leakyRelu=False, lstmFlag=True)
crnn_vertical_handle = CRNNHandle(crnn_vertical_model_path , crnn_vertical_net , gpu_id=GPU_ID)
assert angle_type in ["shufflenetv2_05"]
if angle_type == "shufflenetv2_05":
angle_net = shufflenet_v2_x0_5(num_classes=len(lable_map_dict), pretrained=False)
angle_handle = AangleClassHandle(angle_model_path,angle_net,gpu_id=GPU_ID)
def crnnRec(im, rects_re, leftAdjust=False, rightAdjust=False, alph=0.2, f=1.0):
"""
crnn模型,ocr识别
@@model,
@@converter,
@@im:Array
@@text_recs:text box
@@ifIm:是否输出box对应的img
"""
results = []
im = Image.fromarray(im)
for index, rect in enumerate(rects_re):
degree, w, h, cx, cy = rect
# partImg, newW, newH = rotate_cut_img(im, 90 + degree , cx, cy, w, h, leftAdjust, rightAdjust, alph)
partImg = crop_rect(im, ((cx, cy ),(h, w),degree))
newW,newH = partImg.size
partImg_array = np.uint8(partImg)
#
if newH > 1.5* newW:
partImg_array = np.rot90(partImg_array,1)
# partImg = Image.fromarray(partImg_array).convert("RGB")
# partImg.save("./debug_im/{}.jpg".format(index))
angel_index = angle_handle.predict(partImg_array)
angel_class = lable_map_dict[angel_index]
# print(angel_class)
rotate_angle = rotae_map_dict[angel_class]
if rotate_angle != 0 :
partImg_array = np.rot90(partImg_array,rotate_angle//90)
partImg = Image.fromarray(partImg_array).convert("RGB")
#
# partImg.save("./debug_im/{}.jpg".format(index))
partImg_ = partImg.convert('L')
try:
if crnn_vertical_handle is not None and angel_class in ["shudao", "shuzhen"]:
simPred = crnn_vertical_handle.predict(partImg_)
else:
simPred = crnn_handle.predict(partImg_) ##识别的文本
except :
continue
if simPred.strip() != u'':
results.append({'cx': cx * f, 'cy': cy * f, 'text': simPred, 'w': newW * f, 'h': newH * f,
'degree': degree })
return results
def text_predict(img):
# img = cv2.imread(imgpath)
preds, boxes_list, rects_re, t = text_handle.predict(img, long_size=pse_long_size)
img2 = draw_bbox(img, boxes_list, color=(0, 255, 0))
cv2.imwrite("debug_im/draw.jpg", img2)
result = crnnRec(np.array(img), rects_re)
return result
if __name__ == "__main__":
pass