/
selective_search.py
258 lines (206 loc) · 9.55 KB
/
selective_search.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
import tempfile
import subprocess
import shlex
import os
import sys
import pdb
import getopt
import numpy as np
import scipy.io
import cv2
import cPickle as pickle
from reporting import Evaluation, Detections
import utils
from timer import Timer
from collections import defaultdict
def get_windows(image_fnames, script_dirname, cmd='selective_search', k=200, scale=1.08):
"""
Run MATLAB Selective Search code on the given image filenames to
generate window proposals.
Parameters
----------
image_filenames: strings
Paths to images to run on.
cmd: string
selective search function to call:
- 'selective_search' for a few quick proposals
- 'selective_seach_rcnn' for R-CNN configuration for more coverage.
"""
# Form the MATLAB script command that processes images and write to
# temporary results file.
f, output_filename = tempfile.mkstemp(suffix='.mat')
os.close(f)
fnames_cell = '{' + ','.join("'{}'".format(x) for x in image_fnames) + '}'
if cmd == 'selective_search':
command = "{}({}, '{}', {}, {})".format(cmd, fnames_cell, output_filename, k, scale)
elif cmd == 'selective_search_rcnn':
command = "{}({}, '{}')".format(cmd, fnames_cell, output_filename)
else:
raise ValueError('Unknown MATLAB command issues by python script')
print(command)
# Execute command in MATLAB.
mc = "matlab -nojvm -r \"try; {}; catch; exit; end; exit\"".format(command)
pid = subprocess.Popen(
shlex.split(mc), stdout=open('/dev/null', 'w'), cwd=script_dirname)
retcode = pid.wait()
if retcode != 0:
raise Exception("Matlab script did not exit successfully!")
# Read the results and undo Matlab's 1-based indexing.
all_boxes = list(scipy.io.loadmat(output_filename)['all_boxes'][0])
subtractor = np.array((1, 1, 0, 0))[np.newaxis, :]
all_boxes = [boxes - subtractor for boxes in all_boxes]
# Remove temporary file, and return.
os.remove(output_filename)
if len(all_boxes) != len(image_fnames):
raise Exception("Something went wrong computing the windows!")
return all_boxes
def draw_windows(img_path, windows):
windows = np.array(windows)
img = cv2.imread(img_path)
for win in windows:
cv2.rectangle(img, (win[1], win[0]), (win[3], win[2]), (0,0,0), 2)
return img
def selectionboxes2polygons(boxes):
#the proposals are passed in as y1,x1, y2,x2
polygons = np.empty((boxes.shape[0], 4, 2))
boxes = np.array(boxes)
#follow the same convention as in order_points:
#order points in
#1.top_left 2.top_right 3.bottom_right 4.bottom_left order
#1. top left point
polygons[:,0,0] = boxes[:,1]#x1
polygons[:,0,1] = boxes[:,0]#y1
#3. bottom right
polygons[:,2,0] = boxes[:,3]#x2
polygons[:,2,1] = boxes[:,2]#y2
#2. top right
polygons[:,1,0] = boxes[:,3]# == x2
polygons[:,1,1] = boxes[:,0]# == y1
#4. bottom left
polygons[:,3,0] = boxes[:,1] # == x1
polygons[:,3,1] = boxes[:,2] # == y2
return polygons
def get_parameters():
k = 300
scale = 0.8
try:
opts, args = getopt.getopt(sys.argv[1:], "k:s:")
except getopt.GetoptError:
sys.exit(2)
print 'Command line failed'
for opt, arg in opts:
if opt == '-k':
k = int(float(arg))
elif opt == '-s':
scale = float(arg)
return k, scale
def save_training_FP_and_TP_helper(img_name,evaluation, detections, patches_path, general_path, img, roof_type, extraction_type, color):
#this is where we write the detections we're extraction. One image per roof type
#we save: 1. the patches and 2. the image with marks of what the detections are, along with the true roofs (for debugging)
img_debug = np.copy(img)
if roof_type == 'background':
utils.draw_detections(evaluation.correct_roofs['metal'][img_name], img_debug, color=(0, 0, 0), thickness=2)
utils.draw_detections(evaluation.correct_roofs['thatch'][img_name], img_debug, color=(0, 0, 0), thickness=2)
else:
utils.draw_detections(evaluation.correct_roofs[roof_type][img_name], img_debug, color=(0, 0, 0), thickness=2)
for i, detection in enumerate(detections):
#extract the patch, rotate it to a horizontal orientation, save it
bitmap = np.zeros((img.shape[:2]), dtype=np.uint8)
padded_detection = utils.add_padding_polygon(detection, bitmap)
warped_patch = utils.four_point_transform(img, padded_detection)
cv2.imwrite('{0}{1}_{2}_roof{3}.jpg'.format(patches_path, roof_type, img_name[:-4], i), warped_patch)
#mark where roofs where taken out from for debugging
utils.draw_polygon(padded_detection, img_debug, fill=False, color=color, thickness=2, number=i)
#write this type of extraction and the roofs to an image
cv2.imwrite('{0}{1}_{2}_extract_{3}.jpg'.format(general_path, img_name[:-4], roof_type, extraction_type), img_debug)
def save_training_TP_FP_using_voc(evaluation, img_names, in_path, out_folder_name=None, neg_thresh=0.3):
'''use the voc scores to decide if a patch should be saved as a TP or FP or not
'''
assert out_folder_name is not None
general_path = utils.get_path(neural=True, data_fold=utils.TRAINING, in_or_out=utils.IN, out_folder_name=out_folder_name)
path_true = general_path+'truepos_from_selective_search/'
utils.mkdir(path_true)
path_false = general_path+'falsepos_from_selective_search/'
utils.mkdir(path_false)
for img_name in img_names:
good_detections = defaultdict(list)
bad_detections = defaultdict(list)
try:
img = cv2.imread(in_path+img_name, flags=cv2.IMREAD_COLOR)
except:
print 'Cannot open image'
sys.exit(-1)
for roof_type in utils.ROOF_TYPES:
detection_scores = evaluation.detections.best_score_per_detection[img_name][roof_type]
for detection, score in detection_scores:
if score > 0.5:
#true positive
good_detections[roof_type].append(detection)
if score < neg_thresh:
#false positive
bad_detections[roof_type].append(detection)
for roof_type in utils.ROOF_TYPES:
extraction_type = 'good'
save_training_FP_and_TP_helper(img_name, evaluation, good_detections[roof_type], path_true, general_path, img, roof_type, extraction_type, (0,255,0))
extraction_type = 'background'
save_training_FP_and_TP_helper(img_name, evaluation, bad_detections[roof_type], path_false, general_path, img, roof_type, extraction_type, (0,0,255))
def copy_images(data_fold):
'''
if data_fold == utils.TRAINING:
prefix = 'training_'
elif data_fold == utils.VALIDATION:
prefix = 'validation_'
'''
prefix = ''
in_path = utils.get_path(in_or_out = utils.IN, data_fold=data_fold)
for img_name in os.listdir(in_path):
if img_name.endswith('jpg') or img_name.endswith('xml'):
#move the image over and save it with a prefix
subprocess.check_call('cp {} {}'.format(in_path+img_name, prefix+img_name), shell=True)
def main():
script_dirname = os.path.abspath(os.path.dirname(__file__))
output_patches = False
fold = utils.TRAINING if output_patches else utils.VALIDATION
#only use this path to get the names of the files you want to use
in_path = utils.get_path(in_or_out=utils.IN, data_fold=fold)
in_path_selective = script_dirname+'/' #this is where the files actually live
img_names = [img for img in os.listdir(in_path) if img.endswith('jpg')]
image_filenames = [in_path_selective+img for img in os.listdir(in_path) if img.endswith('jpg')]
#get the proposals
k, scale = get_parameters()
sim = 'all'
color = 'hsv'
cmd = 'selective_search'
if cmd == 'selective_search':
folder_name = 'k{}_scale{}_sim{}_color{}_FIXING/'.format(k, scale, sim, color)
else:
folder_name = 'selectiveRCNN/'
print 'Folder name is: {}'.format(folder_name)
with Timer() as t:
boxes = get_windows(image_filenames, script_dirname, cmd=cmd, k=k, scale=scale)
print 'Time to process {}'.format(t.secs)
detections = Detections()
detections.total_time = t.secs
out_path = utils.get_path(selective=True, in_or_out=utils.OUT, data_fold=fold, out_folder_name=folder_name)
evaluation = Evaluation(#use_corrected_roofs=True,
report_name='report.txt', method='windows',
folder_name=folder_name, out_path=out_path,
detections=detections, in_path=in_path)
#score the proposals
for img, proposals in zip(img_names, boxes):
print 'Evaluating {}'.format(img)
print("Found {} windows".format(len(proposals)))
proposals = selectionboxes2polygons(proposals)
detections.set_detections(detection_list=proposals,roof_type='metal', img_name=img)
detections.set_detections(detection_list=proposals,roof_type='thatch', img_name=img)
print 'Evaluating...'
evaluation.score_img(img, (1200,2000))
evaluation.save_images(img)
save_training_TP_FP_using_voc(evaluation, img_names, in_path_selective, out_folder_name=folder_name, neg_thresh=0.3)
evaluation.print_report()
with open(out_path+'evaluation.pickle', 'wb') as f:
pickle.dump(evaluation, f)
if __name__ == '__main__':
copy_images(utils.TRAINING)
copy_images(utils.VALIDATION)
main()