-
Notifications
You must be signed in to change notification settings - Fork 35
/
recognition_test_pb.py
209 lines (171 loc) · 6.88 KB
/
recognition_test_pb.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import tensorflow as tf
import numpy as np
import cv2
import os
import pandas as pd
from text_recognition.dataset import Dataset
from format_prech import revise
import time
def order_points_old(pts):
# initialize a list of coordinates that will be ordered
# such that the first entry in the list is the top-left,
# the second entry is the top-right, the third is the
# bottom-right, and the fourth is the bottom-left
rect = np.zeros((4, 2), dtype="float32")
# the top-left point will have the smallest sum, whereas
# the bottom-right point will have the largest sum
s = pts.sum(axis=1)
rect[0] = pts[np.argmin(s)]
rect[2] = pts[np.argmax(s)]
# now, compute the difference between the points, the
# top-right point will have the smallest difference,
# whereas the bottom-left will have the largest difference
diff = np.diff(pts, axis=1)
rect[1] = pts[np.argmin(diff)]
rect[3] = pts[np.argmax(diff)]
# return the ordered coordinates
return rect
def affine(im, pts, r_size, direction):
bbox = [[pts[0],pts[1]],[pts[2],pts[3]],[pts[4],pts[5]],[pts[6],pts[7]]]
rect = order_points_old(np.array(bbox))
if rect[1][1] < rect[0][1] and abs(rect[1][1] - rect[0][1])> 100:
rect[[0,1,2,3],:] = rect[[1,2,3,0],:]
pts1 = np.float32([rect[0], rect[1], rect[2]])
if direction == 0:
w, h = r_size
w = 240
else:
h, w = r_size
h = 320
pts2 = np.float32([[0,0], [w-1,0], [w-1, h-1]])
M = cv2.getAffineTransform(pts1, pts2)
dst = cv2.warpAffine(im, M, (w, h))
return dst
def sort_box(box_file):
res = []
direction = -1
count_v = 0
count_h = 0
listx = []
listy = []
listx2 = []
listy2 = []
boxlist = []
for box in box_file:
box = [int(i) for i in box.rstrip().split(", ")]
xmin = np.min(np.array(box[::2]))
xmax = np.max(np.array(box[::2]))
ymin = np.min(np.array(box[1::2]))
ymax = np.max(np.array(box[1::2]))
listx.append(int(xmin))
listy.append(int(ymin))
listx2.append(int(xmax))
listy2.append(int(ymax))
boxlist.append(box)
w = xmax - xmin
h = ymax - ymin
if w > h:
count_h += 1
else:
count_v +=1
if count_h > count_v:
direction = 0
df = pd.DataFrame({'x':listx,'y':listy,'x2':listx2,'y2':listy2,"bboxes":boxlist})
else:
direction = 1
df = pd.DataFrame({'y':listx,'x':listy,'y2':listx2,'x2':listy2,"bboxes":boxlist})
#check wheher in same line
df = df.sort_values(['y2','x'])
temp1 =0
dfidx = 0
listline = []
line_threshold = 25
for index, row in df.iterrows():
y1 = row["y"]
if dfidx != 0 and abs(temp1 - y1) > line_threshold:
newlistline = sorted(listline, key = lambda k:k['x'])
res.extend(newlistline)
listline = []
temp1 = y1
dfidx+=1
dictbox = {"x":row["x"],"bboxes":row["bboxes"]}
listline.append(dictbox)
newlistline = sorted(listline, key = lambda k:k['x'])
res.extend(newlistline)
return res, direction
def recognition(img, session_r_h, session_r_v, bboxs, r_size, images_ph_h, images_ph_v, model_out_h, model_out_v, decoded_h, decoded_v):
int_to_char=['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
boxlist, direction = sort_box(bboxs)
result = ''
num = len(boxlist)
idx = 0
for idx, item in enumerate(boxlist):
ori_box = item['bboxes']
img_affined = affine(img, ori_box, r_size, direction)
img_affined = cv2.cvtColor(img_affined, cv2.COLOR_BGR2GRAY)
#img_affined = cv2.resize(img_affined,(w, h))
if direction == 1:
img_affined = img_affined.transpose((1,0))
img_affined = img_affined[::-1]
#sq = 80
# cv2.imshow('2',img_affined)
# cv2.waitKey()
img_affined = np.array([img_affined])
img_affined = np.expand_dims(img_affined, axis = 3)
if idx == 0 :
img_affined_final = img_affined
if idx != 0 and idx < 4:
img_affined_final = np.concatenate((img_affined_final, img_affined), 0)
if idx == 4 :
img_affined_final2 = img_affined
if idx > 4:
img_affined_final2 = np.concatenate((img_affined_final2, img_affined), 0)
# tt1 = time.time()
# decoded, _ = tf.nn.ctc_beam_search_decoder(model_out, sq * np.ones(1), merge_repeated=False)
# tt2 = time.time()
if idx < 3:
for j in range(3-idx):
img_affined_final = np.concatenate((img_affined_final, img_affined), 0)
if idx > 3 and idx <7:
for j in range(4, 7-idx):
img_affined_final2 = np.concatenate((img_affined_final2, img_affined), 0)
if idx < 4:
if direction == 0:
preds, _ = session_r_h.run([decoded_h, model_out_h], feed_dict={images_ph_h: img_affined_final})
else:
preds, _ = session_r_v.run([decoded_v, model_out_v], feed_dict={images_ph_v: img_affined_final})
for i in range (0,num):
try:
predicted = Dataset.sparse_tensor_to_str(preds[0], int_to_char)[i]
result += predicted
except:
predicted = ''
result += predicted
else:
if direction == 0:
preds1, _ = session_r_h.run([decoded_h, model_out_h], feed_dict={images_ph_h: img_affined_final})
preds2, _ = session_r_h.run([decoded_h, model_out_h], feed_dict={images_ph_h: img_affined_final2})
else:
preds1, _ = session_r_v.run([decoded_v, model_out_v], feed_dict={images_ph_v: img_affined_final})
preds2, _ = session_r_v.run([decoded_v, model_out_v], feed_dict={images_ph_v: img_affined_final2})
for i in range (0,3):
try:
predicted = Dataset.sparse_tensor_to_str(preds1[0], int_to_char)[i]
result += predicted
except:
predicted = ''
result += predicted
for i in range (4,num):
try:
predicted = Dataset.sparse_tensor_to_str(preds2[0], int_to_char)[i]
result += predicted
except:
predicted = ''
result += predicted
#result += predicted
return revise(result)
#[print(n.name) for n in tf.get_default_graph().as_graph_def().node]
# result = summarize_graph("/home/blin/Downloads/text_detection/tools/detection.pb")
# print(result)
# predicted = recognition("/home/blin/Downloads/text_recognition/chim/1-122728001-OCR-LB-C02.jpg_001.jpg", '/home/blin/Downloads/text_recognition/tools/recognition_h.pb')
# print(predicted)