-
Notifications
You must be signed in to change notification settings - Fork 2
/
evaluate_localizer.py
174 lines (134 loc) · 6.35 KB
/
evaluate_localizer.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
import numpy as np
import cv2
import time
import tensorflow as tf
import csv
import os
import datetime
from models import localizer as nn
from utils import utils
from utils import input_data
from utils.rect import Rect
flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_integer('input_size', 24, 'width and height of the cnn input')
flags.DEFINE_integer('min_window_size', 30, 'minimum window height and width')
flags.DEFINE_integer('max_window_size', 80, 'maximum window height and width')
flags.DEFINE_integer('label_size', 12, 'width and height of the input images')
flags.DEFINE_string('test', '../data/data/eval/eval_1.png', 'test image path')
flags.DEFINE_integer('scale_factor', 1.25, 'scale factor')
flags.DEFINE_integer('step_size', 12, 'sliding window step size')
flags.DEFINE_string('checkpoint_dir','../output/checkpoints/localizer', 'path to thensorflow checkpoint dir')
flags.DEFINE_string('output_dir','../output/results/localizer/', 'path to output dir')
# start session
sess = tf.InteractiveSession()
def create_mask(model, x, keep_prob, src):
"""
object detection via sliding windows
Args:
model: tensorflow model which is used for detection
x: input data placeholder
keep_prob: keep probability placeholder (dropout)
src: image to apply the detection
Returns:
image mask scaled between 0 and 255
"""
global sess
height, width = src.shape
mask = np.zeros((height,width), np.float32)
input_size = (FLAGS.input_size, FLAGS.input_size)
min_window_size = (FLAGS.min_window_size, FLAGS.min_window_size)
max_window_size = (FLAGS.max_window_size, FLAGS.max_window_size)
for windows, coords in utils.slidingWindow(src, FLAGS.step_size, input_size, FLAGS.scale_factor, min_window_size, max_window_size):
feed = {x:windows, keep_prob:1.0}
out = sess.run(model, feed_dict = feed)
for i in range(0, len(out)):
out_scaled = cv2.resize(np.reshape(out[i], [FLAGS.label_size,FLAGS.label_size]),
coords[i].size(), interpolation=cv2.INTER_CUBIC)
mask[coords[i].y : coords[i].y2(), coords[i].x : coords[i].x2()] += out_scaled
# image processing
mask = cv2.normalize(mask, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
# cv2.imwrite(FLAGS.output_dir + FLAGS.test + '_mask_' + str(FLAGS.step_size) + '_' + str(datetime.datetime.now()) + '.png', mask)
return mask
# ============================================================= #
def mask_to_objects(mask, threshold):
"""
applies a blob detection algorithm to the image
Args:
mask: image mask scaled between 0 and 255
threshold: min pixel intensity of interest
Returns:
list of objects [(x,y)]
"""
params = cv2.SimpleBlobDetector_Params()
params.minThreshold = threshold
params.maxThreshold = 255
params.filterByArea = True
params.minArea = 150
params.maxArea = 10000
params.filterByCircularity = False
params.filterByInertia = False
params.filterByConvexity = False
params.filterByColor = False
params.blobColor = 255
# Create a detector with the parameters
ver = (cv2.__version__).split('.')
if int(ver[0]) < 3:
detector = cv2.SimpleBlobDetector(params)
else:
detector = cv2.SimpleBlobDetector_create(params)
keypoints = detector.detect(mask)
objects = []
for k in keypoints:
objects.append(Rect(int(k.pt[0] - k.size), int(k.pt[1] - k.size), int(k.size * 2), int(k.size * 2)))
return objects
# ============================================================= #
def main(_):
image_path = FLAGS.test
csv_path = os.path.splitext(image_path)[0] + ".csv"
# ---------- create model ----------------#
x = tf.placeholder("float", shape=[None, FLAGS.input_size * FLAGS.input_size])
keep_prob = tf.placeholder("float")
global_step = tf.Variable(0, trainable=False, name='global_step')
model = nn.create(x, keep_prob)
# ---------- restore model ---------------#
saver = tf.train.Saver()
if tf.train.latest_checkpoint(FLAGS.checkpoint_dir) != None:
saver.restore(sess, tf.train.latest_checkpoint(FLAGS.checkpoint_dir))
# ---------- object detection ------------#
print 'starting detection of ' + FLAGS.test + '...'
img = utils.getImage(image_path)
img = cv2.copyMakeBorder(img, FLAGS.max_window_size, FLAGS.max_window_size, FLAGS.max_window_size, FLAGS.max_window_size, cv2.BORDER_REPLICATE)
start = time.time()
#sliding window detection
mask = create_mask(model, x, keep_prob, img)
elapsed = time.time() - start
print 'detection time: %d' % (elapsed)
# ------------- evaluation --------------#
global_step = tf.train.global_step(sess, global_step)
ground_truth_data = utils.get_ground_truth_data(csv_path)
ground_truth_data = [(x + FLAGS.max_window_size,y + FLAGS.max_window_size) for (x,y) in ground_truth_data]
for th in [150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250]:
detected = mask_to_objects(mask, th)
tp, fn, fp = utils.evaluate(ground_truth_data, detected)
# ----------------output ----------------#
# image output
"""
img_out = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) * 255
for (r,score) in candidates:
cv2.rectangle(img_out, (r.x,r.y), (r.x2(), r.y2()), [200,200,200], 2)
for r in detected:
cv2.rectangle(img_out, (r.x,r.y), (r.x2(), r.y2()), [0,255,0], 2)
for c in ground_truth_data:
cv2.circle(img_out, (c[0], c[1]), 3, [0,0,255],3)
output_file = "out" + '_' + str(global_step) + 'its_' + str(FLAGS.step_size) + 'step_' + str(th) + 'threshold_' + str(datetime.datetime.now())
cv2.imwrite(FLAGS.output_dir + output_file + '.png', img_out)
"""
# csv output
with open(FLAGS.output_dir + 'results.csv', 'ab') as file:
writer = csv.writer(file, delimiter=',')
writer.writerow([FLAGS.test, str(elapsed),
str(global_step), str(len(ground_truth_data)), str(th),
str(len(detected)), str(FLAGS.step_size), str(tp), str(fp), str(fn)])
if __name__ == '__main__':
tf.app.run()