-
Notifications
You must be signed in to change notification settings - Fork 108
/
predict.py
288 lines (242 loc) · 13.8 KB
/
predict.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import cv2
import numpy as np
import core.utils as utils
from PIL import Image
import tensorflow
if tensorflow.__version__.startswith('1.'):
import tensorflow as tf
else:
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
if __name__ == '__main__':
"""
argv = sys.argv
if len(argv) < 5:
print('usage: python test.py gpu_id pb_file img_path_file out_path')
sys.exit()
"""
gpu_id = '0' #argv[1]
os.environ['CUDA_VISIBLE_DEVICES'] = str(gpu_id)
pb_file = 'ckpts/MosEggs_yolov3_loss=33.0915.ckpt-8.pb' #argv[2]
if not os.path.exists(pb_file):
print('pb_file=%s not exist' % pb_file)
sys.exit()
img_path_file = 'D:/datasets/MosEggs/test' #argv[3]
if not os.path.exists(img_path_file):
print('img_path_file=%s not exist' % img_path_file)
sys.exit()
out_path = 'D:/datasets/MosEggs/out' #argv[4]
if not os.path.exists(out_path):
os.makedirs(out_path)
print('test gpu_id=%s, pb_file=%s, img_file=%s, out_path=%s' % (gpu_id, pb_file, img_path_file, out_path))
num_classes = 1
input_size = 512
score_thresh = 0.3
iou_type = 'iou' #yolov4:diou, else giou
iou_thresh = 0.3
graph = tf.Graph()
return_elements = ["input/input_data:0", "pred_sbbox/concat_2:0", "pred_mbbox/concat_2:0", "pred_lbbox/concat_2:0"]
return_tensors = utils.read_pb_return_tensors(graph, pb_file, return_elements)
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(graph=graph, config=config) as sess:
if os.path.isfile(img_path_file):
img = cv2.imread(img_path_file)
# 开始切图 cut
h_step = img.shape[0] // input_size
w_step = img.shape[1] // input_size
h_rest = -(img.shape[0] - input_size * h_step)
w_rest = -(img.shape[1] - input_size * w_step)
img_list = []
# 循环切图
for h in range(h_step):
for w in range(w_step):
# 划窗采样
im = img[(h * input_size) : (h * input_size + input_size), (w * input_size) : (w * input_size + input_size), :]
img_list.append(im)
img_list.append(img[(h * input_size) : (h * input_size + input_size), -input_size:, :])
for w in range(w_step - 1):
img_list.append(img[-input_size:, (w * input_size) : (w * input_size + input_size), :])
img_list.append(img[-input_size:, -input_size:, :])
predict_img_list = []
for i, ims in enumerate(img_list):
img_size = ims.shape[:2]
image_data = utils.image_preporcess(np.copy(ims), [input_size, input_size])
image_data = image_data[np.newaxis, ...]
pred_sbbox, pred_mbbox, pred_lbbox = sess.run([return_tensors[1], return_tensors[2], return_tensors[3]],
feed_dict={return_tensors[0]: image_data})
pred_bbox = np.concatenate([np.reshape(pred_sbbox, (-1, 5 + num_classes)),
np.reshape(pred_mbbox, (-1, 5 + num_classes)),
np.reshape(pred_lbbox, (-1, 5 + num_classes))], axis=0)
bboxes = utils.postprocess_boxes(pred_bbox, img_size, input_size, score_thresh)
bboxes = utils.nms(bboxes, iou_type, iou_thresh, method='nms')
out_img = ims
if len(bboxes) > 0:
image = utils.draw_bbox(ims, bboxes)
# image = Image.fromarray(image)
# image.show()
out_img = np.asarray(image)
score = bboxes[0][4]
file_path, file_name = os.path.split(img_path_file)
file, postfix = os.path.splitext(file_name)
# out_file = os.path.join(out_path, file + '_%.6f' % (score) + postfix)
# cv2.imwrite(out_file, out_img)
# print('idx=', idx, 'in_img_file=', in_img_file, 'out_file=', out_file)
predict_img_list.append(out_img)
# 将预测后的图像块再拼接起来
count_temp = 0
result_img = img.copy()
for h in range(h_step):
for w in range(w_step):
result_img[h * input_size: (h + 1) * input_size, w * input_size: (w + 1) * input_size] = predict_img_list[count_temp]
count_temp += 1
result_img[h * input_size: (h + 1) * input_size, w_rest:] = predict_img_list[count_temp][:, w_rest:]
count_temp += 1
for w in range(w_step - 1):
result_img[h_rest:, (w * input_size) : (w * input_size + input_size)] = predict_img_list[count_temp][h_rest:, :]
count_temp += 1
result_img[h_rest:, w_rest:] = predict_img_list[count_temp][h_rest:, w_rest:]
out_file = os.path.join(out_path, img_path_file.replace('.jpg', '_result.jpg'))
cv2.imwrite(out_file, result_img)
print('in_img_file=', img_path_file, 'out_file=', out_file)
elif os.path.isdir(img_path_file):
def detect_img(fim, start_h, start_w, fpredict_bboxes):
img_size = fim.shape[:2]
image_data = utils.image_preporcess(np.copy(fim), [input_size, input_size])
image_data = image_data[np.newaxis, ...]
pred_sbbox, pred_mbbox, pred_lbbox = sess.run([return_tensors[1], return_tensors[2], return_tensors[3]],
feed_dict={return_tensors[0]: image_data})
pred_bbox = np.concatenate([np.reshape(pred_sbbox, (-1, 5 + num_classes)),
np.reshape(pred_mbbox, (-1, 5 + num_classes)),
np.reshape(pred_lbbox, (-1, 5 + num_classes))], axis=0)
bboxes = utils.postprocess_boxes(pred_bbox, img_size, input_size, score_thresh)
bboxes = utils.nms(bboxes, iou_type, iou_thresh, method='nms')
for i, bbox in enumerate(bboxes):
# bboxes: [x_min, y_min, x_max, y_max, probability, cls_id] format coordinates
coor = np.array(bbox[:4], dtype=np.int32)
bbox[0] = int(start_w + coor[0])
bbox[1] = int(start_h + coor[1])
bbox[2] = int(start_w + coor[2])
bbox[3] = int(start_h + coor[3])
fpredict_bboxes.append(bbox)
img_files = os.listdir(img_path_file)
for idx, img_file in enumerate(img_files):
in_img_file = os.path.join(img_path_file, img_file)
#print('idx=', idx, 'in_img_file=', in_img_file)
if not os.path.exists(in_img_file):
print('idx=', idx, 'in_img_file=', in_img_file, ' not exist')
continue
img = cv2.imread(in_img_file)
if img is None:
print('idx=', idx, 'in_img_file=', in_img_file, ' read error')
continue
# 开始切图 cut
h_step = img.shape[0] // input_size
w_step = img.shape[1] // input_size
h_rest = -(img.shape[0] - input_size * h_step)
w_rest = -(img.shape[1] - input_size * w_step)
predict_bboxes = []
# 循环切图
for h in range(h_step):
for w in range(w_step):
# 划窗采样
im = img[(h * input_size) : (h * input_size + input_size), (w * input_size) : (w * input_size + input_size), :]
detect_img(im, h * input_size, w * input_size, predict_bboxes)
# for w_rest
im = img[(h * input_size) : (h * input_size + input_size), -input_size:, :]
detect_img(im, h * input_size, w_rest, predict_bboxes)
# for h_rest
for w in range(w_step - 1):
im = img[-input_size:, (w * input_size) : (w * input_size + input_size), :]
detect_img(im, h_rest, w * input_size, predict_bboxes)
# for h_rest and w_rest
im = img[-input_size:, -input_size:, :]
detect_img(im, h_rest, w_rest, predict_bboxes)
if len(predict_bboxes) > 0:
image = utils.draw_bbox(img, predict_bboxes)
out_img = np.asarray(image)
score = predict_bboxes[0][4]
file_path, file_name = os.path.split(in_img_file)
file, postfix = os.path.splitext(file_name)
out_file = os.path.join(out_path, file + '_%d_%.6f' % (input_size, score) + postfix)
cv2.imwrite(out_file, out_img)
print('idx=', idx, 'in_img_file=', in_img_file, 'out_file=', out_file, 'predict_bboxes.len=', len(predict_bboxes))
#break
elif os.path.isdir(img_path_file) and False:
img_files = os.listdir(img_path_file)
for idx, img_file in enumerate(img_files):
in_img_file = os.path.join(img_path_file, img_file)
# print('idx=', idx, 'in_img_file=', in_img_file)
if not os.path.exists(in_img_file):
print('idx=', idx, 'in_img_file=', in_img_file, ' not exist')
continue
img = cv2.imread(in_img_file)
if img is None:
print('idx=', idx, 'in_img_file=', in_img_file, ' read error')
continue
# 开始切图 cut
h_step = img.shape[0] // input_size
w_step = img.shape[1] // input_size
h_rest = -(img.shape[0] - input_size * h_step)
w_rest = -(img.shape[1] - input_size * w_step)
img_list = []
# 循环切图
for h in range(h_step):
for w in range(w_step):
# 划窗采样
im = img[(h * input_size) : (h * input_size + input_size), (w * input_size) : (w * input_size + input_size), :]
img_list.append(im)
img_list.append(img[(h * input_size):(h * input_size + input_size), -input_size:, :])
for w in range(w_step - 1):
img_list.append(img[-input_size:, (w * input_size):(w * input_size + input_size), :])
img_list.append(img[-input_size:, -input_size:, :])
predict_img_list = []
predict_bboxes_list = []
for i, ims in enumerate(img_list):
img_size = ims.shape[:2]
image_data = utils.image_preporcess(np.copy(ims), [input_size, input_size])
image_data = image_data[np.newaxis, ...]
pred_sbbox, pred_mbbox, pred_lbbox = sess.run([return_tensors[1], return_tensors[2], return_tensors[3]],
feed_dict={return_tensors[0]: image_data})
pred_bbox = np.concatenate([np.reshape(pred_sbbox, (-1, 5 + num_classes)),
np.reshape(pred_mbbox, (-1, 5 + num_classes)),
np.reshape(pred_lbbox, (-1, 5 + num_classes))], axis=0)
bboxes = utils.postprocess_boxes(pred_bbox, img_size, input_size, score_thresh)
bboxes = utils.nms(bboxes, iou_type, iou_thresh, method='nms')
out_img = ims
if len(bboxes) > 0:
image = utils.draw_bbox(ims, bboxes)
out_img = np.asarray(image)
for i, bbox in enumerate(bboxes):
predict_bboxes_list.append(bbox)
# score = bboxes[0][4]
# file_path, file_name = os.path.split(in_img_file)
# file, postfix = os.path.splitext(file_name)
# out_file = os.path.join(out_path, file + '_%.6f' % (score) + postfix)
# cv2.imwrite(out_file, out_img)
# print('idx=', idx, 'in_img_file=', in_img_file, 'out_file=', out_file)
predict_img_list.append(out_img)
# 将预测后的图像块再拼接起来
count_temp = 0
result_img = img.copy()
for h in range(h_step):
for w in range(w_step):
result_img[h * input_size: (h + 1) * input_size, w * input_size: (w + 1) * input_size] = predict_img_list[count_temp]
count_temp += 1
result_img[h * input_size: (h + 1) * input_size, w_rest:] = predict_img_list[count_temp][:, w_rest:]
count_temp += 1
if h_rest != 0:
for w in range(w_step - 1):
result_img[h_rest:, (w * input_size): (w * input_size + input_size)] = predict_img_list[count_temp][h_rest:,:]
count_temp += 1
result_img[h_rest:, w_rest:] = predict_img_list[count_temp][h_rest:, w_rest:]
out_file = os.path.join(out_path, img_file.replace('.jpg', '_%d.jpg' % input_size))
cv2.imwrite(out_file, result_img)
print('idx=', idx, 'in_img_file=', in_img_file, 'out_file=', out_file, 'predict_bboxes_list.len=', len(predict_bboxes_list))
break
else:
print('img_path_file=%s is error' % img_path_file)