forked from kuwal786/localization_using_deep_reinforcement
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathall_helper_functions.py
295 lines (225 loc) · 8.55 KB
/
all_helper_functions.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
from keras.preprocessing import image
import numpy as np
def get_all_ids(annotations):
all_ids = []
for i in range(len(annotations)):
all_ids.append(get_ids_objects_from_annotation(annotations[i]))
return all_ids
def get_all_images(image_names, path_voc):
images = []
for j in range(np.size(image_names)):
image_name = image_names[0][j]
string = path_voc + '/JPEGImages/' + image_name + '.jpg'
images.append(image.load_img(string, False))
return images
def get_all_images_pool(image_names, path_voc):
images = []
for j in range(np.size(image_names)):
image_name = image_names[j]
string = path_voc + '/JPEGImages/' + image_name + '.jpg'
images.append(image.load_img(string, False))
return images
def load_images_names_in_data_set(data_set_name, path_voc):
file_path = path_voc + '/ImageSets/Main/' + data_set_name + '.txt'
f = open(file_path)
image_names = f.readlines()
image_names = [x.strip('\n') for x in image_names]
if data_set_name.startswith("aeroplane") | data_set_name.startswith("bird") | data_set_name.startswith("cow"):
return [x.split(None, 1)[0] for x in image_names]
else:
return [x.strip('\n') for x in image_names]
def load_images_labels_in_data_set(data_set_name, path_voc):
file_path = path_voc + '/ImageSets/Main/' + data_set_name + '.txt'
f = open(file_path)
images_names = f.readlines()
images_names = [x.split(None, 1)[1] for x in images_names]
images_names = [x.strip('\n') for x in images_names]
return images_names
def mask_image_with_mean_background(mask_object_found, image):
new_image = image
size_image = np.shape(mask_object_found)
for j in range(size_image[0]):
for i in range(size_image[1]):
if mask_object_found[j][i] == 1:
new_image[j, i, 0] = 103.939
new_image[j, i, 1] = 116.779
new_image[j, i, 2] = 123.68
return new_image
#metrics
import cv2
def calculate_iou(img_mask, gt_mask):
gt_mask *= 1.0
img_and = cv2.bitwise_and(img_mask, gt_mask)
img_or = cv2.bitwise_or(img_mask, gt_mask)
j = np.count_nonzero(img_and)
i = np.count_nonzero(img_or)
iou = float(float(j)/float(i))
return iou
def calculate_overlapping(img_mask, gt_mask):
gt_mask *= 1.0
img_and = cv2.bitwise_and(img_mask, gt_mask)
j = np.count_nonzero(img_and)
i = np.count_nonzero(gt_mask)
overlap = float(float(j)/float(i))
return overlap
def follow_iou(gt_masks, mask, array_classes_gt_objects, object_id, last_matrix, available_objects):
results = np.zeros([np.size(array_classes_gt_objects), 1])
for k in range(np.size(array_classes_gt_objects)):
if array_classes_gt_objects[k] == object_id:
if available_objects[k] == 1:
gt_mask = gt_masks[:, :, k]
iou = calculate_iou(mask, gt_mask)
results[k] = iou
else:
results[k] = -1
max_result = max(results)
ind = np.argmax(results)
iou = last_matrix[ind]
new_iou = max_result
return iou, new_iou, results, ind
#parse_xml_annotations
import xml.etree.ElementTree as ET
def get_bb_of_gt_from_pascal_xml_annotation(xml_name, voc_path):
string = voc_path + '/Annotations/' + xml_name + '.xml'
tree = ET.parse(string)
root = tree.getroot()
names = []
x_min = []
x_max = []
y_min = []
y_max = []
for child in root:
if child.tag == 'object':
for child2 in child:
if child2.tag == 'name':
names.append(child2.text)
elif child2.tag == 'bndbox':
for child3 in child2:
if child3.tag == 'xmin':
x_min.append(child3.text)
elif child3.tag == 'xmax':
x_max.append(child3.text)
elif child3.tag == 'ymin':
y_min.append(child3.text)
elif child3.tag == 'ymax':
y_max.append(child3.text)
category_and_bb = np.zeros([np.size(names), 5])
for i in range(np.size(names)):
category_and_bb[i][0] = get_id_of_class_name(names[i])
category_and_bb[i][1] = x_min[i]
category_and_bb[i][2] = x_max[i]
category_and_bb[i][3] = y_min[i]
category_and_bb[i][4] = y_max[i]
return category_and_bb
def get_all_annotations(image_names, voc_path):
annotations = []
for i in range(np.size(image_names)):
image_name = image_names[0][i]
annotations.append(get_bb_of_gt_from_pascal_xml_annotation(image_name, voc_path))
return annotations
def generate_bounding_box_from_annotation(annotation, image_shape):
length_annotation = annotation.shape[0]
masks = np.zeros([image_shape[0], image_shape[1], length_annotation])
for i in range(0, length_annotation):
masks[annotation[i, 3]:annotation[i, 4], annotation[i, 1]:annotation[i, 2], i] = 1
return masks
def get_ids_objects_from_annotation(annotation):
return annotation[:, 0]
def get_id_of_class_name (class_name):
if class_name == 'aeroplane':
return 1
elif class_name == 'bicycle':
return 2
elif class_name == 'bird':
return 3
elif class_name == 'boat':
return 4
elif class_name == 'bottle':
return 5
elif class_name == 'bus':
return 6
elif class_name == 'car':
return 7
elif class_name == 'cat':
return 8
elif class_name == 'chair':
return 9
elif class_name == 'cow':
return 10
elif class_name == 'diningtable':
return 11
elif class_name == 'dog':
return 12
elif class_name == 'horse':
return 13
elif class_name == 'motorbike':
return 14
elif class_name == 'person':
return 15
elif class_name == 'pottedplant':
return 16
elif class_name == 'sheep':
return 17
elif class_name == 'sofa':
return 18
elif class_name == 'train':
return 19
elif class_name == 'tvmonitor':
return 20
#visualizations
from PIL import Image, ImageDraw, ImageFont
path_font = "/usr/share/fonts/liberation/LiberationMono-Regular.ttf"
font = ImageFont.truetype(path_font, 24)
def string_for_action(action):
if action == 0:
return "START"
if action == 1:
return 'up-left'
elif action == 2:
return 'up-right'
elif action == 3:
return 'down-left'
elif action == 4:
return 'down-right'
elif action == 5:
return 'center'
elif action == 6:
return 'TRIGGER'
def draw_sequences(i, k, step, action, draw, region_image, background, path_testing_folder, iou, reward,
gt_mask, region_mask, image_name, save_boolean):
mask = Image.fromarray(255 * gt_mask)
mask_img = Image.fromarray(255 * region_mask)
image_offset = (1000 * step, 70)
text_offset = (1000 * step, 550)
masked_image_offset = (1000 * step, 1400)
mask_offset = (1000 * step, 700)
action_string = string_for_action(action)
footnote = 'action: ' + action_string + ' ' + 'reward: ' + str(reward) + ' Iou:' + str(iou)
draw.text(text_offset, str(footnote), (0, 0, 0), font=font)
img_for_paste = Image.fromarray(region_image)
background.paste(img_for_paste, image_offset)
background.paste(mask, mask_offset)
background.paste(mask_img, masked_image_offset)
file_name = path_testing_folder + '/' + image_name + str(i) + '_object_' + str(k) + '.png'
if save_boolean == 1:
background.save(file_name)
return background
def draw_sequences_test(step, action, qval, draw, region_image, background, path_testing_folder,
region_mask, image_name, save_boolean):
aux = np.asarray(region_image, np.uint8)
img_offset = (1000 * step, 70)
footnote_offset = (1000 * step, 550)
q_predictions_offset = (1000 * step, 500)
mask_img_offset = (1000 * step, 700)
img_for_paste = Image.fromarray(aux)
background.paste(img_for_paste, img_offset)
mask_img = Image.fromarray(255 * region_mask)
background.paste(mask_img, mask_img_offset)
footnote = 'action: ' + str(action)
q_val_predictions_text = str(qval)
draw.text(footnote_offset, footnote, (0, 0, 0), font=font)
draw.text(q_predictions_offset, q_val_predictions_text, (0, 0, 0), font=font)
file_name = path_testing_folder + image_name + '.png'
if save_boolean == 1:
background.save(file_name)
return background