-
Notifications
You must be signed in to change notification settings - Fork 125
/
detect.py
197 lines (165 loc) · 5.53 KB
/
detect.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
import torch
from torchvision import transforms
from PIL import Image, ImageDraw
from model import EAST
import os
from dataset import get_rotate_mat
import numpy as np
import lanms
def resize_img(img):
'''resize image to be divisible by 32
'''
w, h = img.size
resize_w = w
resize_h = h
resize_h = resize_h if resize_h % 32 == 0 else int(resize_h / 32) * 32
resize_w = resize_w if resize_w % 32 == 0 else int(resize_w / 32) * 32
img = img.resize((resize_w, resize_h), Image.BILINEAR)
ratio_h = resize_h / h
ratio_w = resize_w / w
return img, ratio_h, ratio_w
def load_pil(img):
'''convert PIL Image to torch.Tensor
'''
t = transforms.Compose([transforms.ToTensor(), transforms.Normalize(mean=(0.5,0.5,0.5),std=(0.5,0.5,0.5))])
return t(img).unsqueeze(0)
def is_valid_poly(res, score_shape, scale):
'''check if the poly in image scope
Input:
res : restored poly in original image
score_shape: score map shape
scale : feature map -> image
Output:
True if valid
'''
cnt = 0
for i in range(res.shape[1]):
if res[0,i] < 0 or res[0,i] >= score_shape[1] * scale or \
res[1,i] < 0 or res[1,i] >= score_shape[0] * scale:
cnt += 1
return True if cnt <= 1 else False
def restore_polys(valid_pos, valid_geo, score_shape, scale=4):
'''restore polys from feature maps in given positions
Input:
valid_pos : potential text positions <numpy.ndarray, (n,2)>
valid_geo : geometry in valid_pos <numpy.ndarray, (5,n)>
score_shape: shape of score map
scale : image / feature map
Output:
restored polys <numpy.ndarray, (n,8)>, index
'''
polys = []
index = []
valid_pos *= scale
d = valid_geo[:4, :] # 4 x N
angle = valid_geo[4, :] # N,
for i in range(valid_pos.shape[0]):
x = valid_pos[i, 0]
y = valid_pos[i, 1]
y_min = y - d[0, i]
y_max = y + d[1, i]
x_min = x - d[2, i]
x_max = x + d[3, i]
rotate_mat = get_rotate_mat(-angle[i])
temp_x = np.array([[x_min, x_max, x_max, x_min]]) - x
temp_y = np.array([[y_min, y_min, y_max, y_max]]) - y
coordidates = np.concatenate((temp_x, temp_y), axis=0)
res = np.dot(rotate_mat, coordidates)
res[0,:] += x
res[1,:] += y
if is_valid_poly(res, score_shape, scale):
index.append(i)
polys.append([res[0,0], res[1,0], res[0,1], res[1,1], res[0,2], res[1,2],res[0,3], res[1,3]])
return np.array(polys), index
def get_boxes(score, geo, score_thresh=0.9, nms_thresh=0.2):
'''get boxes from feature map
Input:
score : score map from model <numpy.ndarray, (1,row,col)>
geo : geo map from model <numpy.ndarray, (5,row,col)>
score_thresh: threshold to segment score map
nms_thresh : threshold in nms
Output:
boxes : final polys <numpy.ndarray, (n,9)>
'''
score = score[0,:,:]
xy_text = np.argwhere(score > score_thresh) # n x 2, format is [r, c]
if xy_text.size == 0:
return None
xy_text = xy_text[np.argsort(xy_text[:, 0])]
valid_pos = xy_text[:, ::-1].copy() # n x 2, [x, y]
valid_geo = geo[:, xy_text[:, 0], xy_text[:, 1]] # 5 x n
polys_restored, index = restore_polys(valid_pos, valid_geo, score.shape)
if polys_restored.size == 0:
return None
boxes = np.zeros((polys_restored.shape[0], 9), dtype=np.float32)
boxes[:, :8] = polys_restored
boxes[:, 8] = score[xy_text[index, 0], xy_text[index, 1]]
boxes = lanms.merge_quadrangle_n9(boxes.astype('float32'), nms_thresh)
return boxes
def adjust_ratio(boxes, ratio_w, ratio_h):
'''refine boxes
Input:
boxes : detected polys <numpy.ndarray, (n,9)>
ratio_w: ratio of width
ratio_h: ratio of height
Output:
refined boxes
'''
if boxes is None or boxes.size == 0:
return None
boxes[:,[0,2,4,6]] /= ratio_w
boxes[:,[1,3,5,7]] /= ratio_h
return np.around(boxes)
def detect(img, model, device):
'''detect text regions of img using model
Input:
img : PIL Image
model : detection model
device: gpu if gpu is available
Output:
detected polys
'''
img, ratio_h, ratio_w = resize_img(img)
with torch.no_grad():
score, geo = model(load_pil(img).to(device))
boxes = get_boxes(score.squeeze(0).cpu().numpy(), geo.squeeze(0).cpu().numpy())
return adjust_ratio(boxes, ratio_w, ratio_h)
def plot_boxes(img, boxes):
'''plot boxes on image
'''
if boxes is None:
return img
draw = ImageDraw.Draw(img)
for box in boxes:
draw.polygon([box[0], box[1], box[2], box[3], box[4], box[5], box[6], box[7]], outline=(0,255,0))
return img
def detect_dataset(model, device, test_img_path, submit_path):
'''detection on whole dataset, save .txt results in submit_path
Input:
model : detection model
device : gpu if gpu is available
test_img_path: dataset path
submit_path : submit result for evaluation
'''
img_files = os.listdir(test_img_path)
img_files = sorted([os.path.join(test_img_path, img_file) for img_file in img_files])
for i, img_file in enumerate(img_files):
print('evaluating {} image'.format(i), end='\r')
boxes = detect(Image.open(img_file), model, device)
seq = []
if boxes is not None:
seq.extend([','.join([str(int(b)) for b in box[:-1]]) + '\n' for box in boxes])
with open(os.path.join(submit_path, 'res_' + os.path.basename(img_file).replace('.jpg','.txt')), 'w') as f:
f.writelines(seq)
if __name__ == '__main__':
img_path = '../ICDAR_2015/test_img/img_2.jpg'
model_path = './pths/east_vgg16.pth'
res_img = './res.bmp'
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = EAST().to(device)
model.load_state_dict(torch.load(model_path))
model.eval()
img = Image.open(img_path)
boxes = detect(img, model, device)
plot_img = plot_boxes(img, boxes)
plot_img.save(res_img)