-
Notifications
You must be signed in to change notification settings - Fork 3
/
object_detection_save_results.py
executable file
·171 lines (138 loc) · 5.52 KB
/
object_detection_save_results.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
import os
import math
import random
import numpy as np
import tensorflow as tf
import cv2
import csv
from tensorflow.contrib import slim
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import sys
sys.path.append('../')
from nets import ssd_vgg_300, ssd_common, np_methods, ssd_vgg_512
from preprocessing import ssd_vgg_preprocessing
from notebooks import visualization
"""
gpu memory manage
"""
gpu_options = tf.GPUOptions(allow_growth=True)
config = tf.ConfigProto(log_device_placement=False, gpu_options=gpu_options)
isess = tf.InteractiveSession(config=config)
"""
define graph & restore SSD model
"""
# input image placeholder
net_shape = (512, 512)
data_format = 'NHWC'
img_input = tf.placeholder(tf.uint8, shape=(None, None, 3))
# Evaluation pre-processing: resize to SSD net shape.
image_pre, labels_pre, bboxes_pre, bbox_img = ssd_vgg_preprocessing.preprocess_for_eval(
img_input, None, None, net_shape, data_format, resize=ssd_vgg_preprocessing.Resize.WARP_RESIZE)
image_4d = tf.expand_dims(image_pre, 0)
# Define the SSD model.
reuse = True if 'ssd_net' in locals() else None
# ssd_net = ssd_vgg_300.SSDNet()
ssd_net = ssd_vgg_512.SSDNet()
with slim.arg_scope(ssd_net.arg_scope(data_format=data_format)):
predictions, localisations, _, _ = ssd_net.net(image_4d, is_training=False, reuse=reuse)
# Restore SSD model.
ckpt_filename = '../checkpoints/model.ckpt-91516'
# print(ckpt_filename)
isess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
saver.restore(isess, ckpt_filename)
# SSD default anchor boxes.
ssd_anchors = ssd_net.anchors(net_shape)
"""
object detection
"""
def process_image(img, select_threshold=0.5, nms_threshold=.45, net_shape=(300, 300)):
# Run SSD network.
rimg, rpredictions, rlocalisations, rbbox_img = isess.run([image_4d, predictions, localisations, bbox_img],
feed_dict={img_input: img})
# Get classes and bboxes from the net outputs.
rclasses, rscores, rbboxes = np_methods.ssd_bboxes_select(
rpredictions, rlocalisations, ssd_anchors,
select_threshold=select_threshold, img_shape=net_shape, num_classes=21, decode=True)
rbboxes = np_methods.bboxes_clip(rbbox_img, rbboxes)
rclasses, rscores, rbboxes = np_methods.bboxes_sort(rclasses, rscores, rbboxes, top_k=400)
rclasses, rscores, rbboxes = np_methods.bboxes_nms(rclasses, rscores, rbboxes, nms_threshold=nms_threshold)
# Resize bboxes to original image shape. Note: useless for Resize.WARP!
rbboxes = np_methods.bboxes_resize(rbbox_img, rbboxes)
return rclasses, rscores, rbboxes
# object detection
image_path = sys.argv[1]
img = mpimg.imread(image_path)
rclasses, rscores, rbboxes = process_image(img)
# print detection results
height = img.shape[0]
width = img.shape[1]
print('number of object', rclasses.shape[0])
print('\n')
detection_results = []
for i in range(rclasses.shape[0]):
cls_id = int(rclasses[i])
temp = []
if cls_id >= 0:
score = rscores[i]
ymin = int(rbboxes[i, 0] * height)
xmin = int(rbboxes[i, 1] * width)
ymax = int(rbboxes[i, 2] * height)
xmax = int(rbboxes[i, 3] * width)
temp.extend([width, height, cls_id, xmin, ymin, xmax, ymax, score])
detection_results.append(temp)
print('object {0}'.format(i))
print('class', rclasses[i])
print('coordination', xmin,ymin,xmax,ymax)
print('confidence', score)
print('\n')
# save detection results which are used for composition classification
save_path = '../../sequence_classifier/demo/' + image_path.split('/')[-1][:-4] + '.csv'
with open(save_path, 'w', newline='') as out:
writer = csv.writer(out)
for item in detection_results:
writer.writerow(item)
"""
label in original image
"""
# [label, color]
label_dict = {0: [None, None], # empty object
1: [None, None], # cheng guan
2: ['Thatched cottage', (100/256, 149/256, 237/256)],
3: ['Rock', (200/256, 200/256, 0/256)],
4: ['Bridge', (255/256, 106/256, 106/256)],
5: ['Mountain slope', (255/256, 105/256, 180/256)],
6: ['Peak', (255/256, 48/256, 48/256)],
7: ['Tree', (160/256, 32/256, 240/256)],
8: ['Cascading peaks', (255/256, 165/256, 0/256)],
9: ['Inscription', (0/256, 200/256, 0/256)],
10: ['Stamp', (0/256, 190/256, 200/256)]}
# draw & show
dpi = 100
fig = plt.figure(figsize=(img.shape[1] / dpi, img.shape[0] / dpi), dpi=dpi) # canvas
plt.imshow(img) # draw img
for i in range(rclasses.shape[0]):
cls_id = int(rclasses[i])
if cls_id >= 0:
score = rscores[i]
ymin = int(rbboxes[i, 0] * height)
xmin = int(rbboxes[i, 1] * width)
ymax = int(rbboxes[i, 2] * height)
xmax = int(rbboxes[i, 3] * width)
# draw rectangle
rect = plt.Rectangle((xmin, ymin),
xmax - xmin,
ymax - ymin,
fill=False,
edgecolor=label_dict[cls_id][1],
linewidth=2)
plt.gca().add_patch(rect) # draw rect
# draw label
class_name = label_dict[cls_id][0]
plt.gca().text(int(xmin) - 2, int(ymin) - 2,
'{:s} | {:.3f}'.format(class_name, score),
bbox=dict(facecolor=label_dict[cls_id][1], alpha=0.5),
fontsize=10, color='white')
plt.axis('off') # off axis
plt.show()