-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy patheval.py
135 lines (98 loc) · 3.96 KB
/
eval.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
from keras.applications.resnet50 import preprocess_input
import numpy as np
import os
import cv2
from model import RFBText
import east_utils
import re
def get_path_list(root_path):
filename_list = list()
for root, _, filenames in os.walk(root_path):
filename_list.extend([(os.path.join(root, filename), filename) for filename in filenames
if filename.lower().endswith(('jpeg', 'png', 'bmp', 'jpg'))
])
return filename_list
def paint_boxes_to_image(image, predicted_boxes):
"""
Draw the quad-boxes on-to the image
:param image:
:param predicted_boxes:
:return:
"""
for box in predicted_boxes:
cv2.polylines(image,
[box.astype(np.int32).reshape((-1, 1, 2))],
True,
color=(0, 0, 255),
thickness=2)
return image
def predict_single_image(model, image):
"""
Predict the bounding box quads for a single image
:param model:
:param image:
:return:
"""
im_resized, (ratio_h, ratio_w) = east_utils.resize_image(image, max_side_len=2400)
im_resized = cv2.cvtColor(im_resized, cv2.COLOR_BGR2RGB)
image_batch = np.expand_dims(im_resized, axis=0)
image_batch = preprocess_input(image_batch)
preds = model.predict(image_batch)
score, geometry = preds
boxes = east_utils.detect(score_map=score,
geo_map=geometry,
score_map_thresh=0.8,
box_thresh=0.1,
nms_thres=0.2)
predicted_boxes = list()
if boxes is not None:
boxes = boxes[:, :8].reshape((-1, 4, 2))
boxes[:, :, 0] /= ratio_w
boxes[:, :, 1] /= ratio_h
for box in boxes:
box = east_utils.sort_poly(box.astype(np.int32))
# reduce false positives
if np.linalg.norm(box[0] - box[1]) < 5 or np.linalg.norm( box[3] - box[0]) < 5:
continue
predicted_boxes.append(box)
return predicted_boxes
def evaluate_image(model, test_images_path):
print('predicting from test_images at path: {0}'.format(test_images_path))
input_path = os.path.join(test_images_path, 'evaluation_images')
pred_path = os.path.join(test_images_path, 'predicted_boxes')
if not os.path.isdir(test_images_path):
os.makedirs(test_images_path)
os.makedirs(input_path)
os.makedirs(pred_path)
elif not os.path.isdir(input_path):
os.makedirs(input_path)
elif not os.path.isdir(pred_path):
os.makedirs(pred_path)
input_path_list = get_path_list(input_path)
image_number_pattern = re.compile(r'img_(\d*).jpg')
for image_path, filename in input_path_list:
image = cv2.imread(image_path)
image_copy = np.copy(image)
print('prediction in progress')
predicted_boxes = predict_single_image(model, image_copy)
print('prediction done')
matcher = image_number_pattern.match(filename)
image_number = matcher.group(1)
txt_path = os.path.join(pred_path, 'res_img_' + image_number + '.txt')
with open(txt_path, 'w') as txt_file:
for box in predicted_boxes:
txt_file.write('{},{},{},{},{},{},{},{}\r\n'.format(
box[0][0],
box[0][1],
box[1][0],
box[1][1],
box[2][0],
box[2][1],
box[3][0],
box[3][1]))
if __name__ == '__main__':
CWD = '.'
model_path = os.path.join(CWD, 'model', 'epoch_258_loss_0.010577003471553326.hdf5')
rfb_text_model = RFBText(model_weights_path=model_path)()
evaluate_image(model=rfb_text_model,
test_images_path=os.path.join(CWD, 'eval_images'))