-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstereo_image_utils.py
304 lines (227 loc) · 9.92 KB
/
stereo_image_utils.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# utilities .py
# these colours are used to draw boxes.
import copy
import math
import numpy as np
import cv2
import matplotlib.pyplot as plt
import scipy
import scipy.optimize
import torch
import torchvision
import torchvision.transforms.functional as tvtf
from torchvision.models.detection import MaskRCNN_ResNet50_FPN_Weights,MaskRCNN_ResNet50_FPN_V2_Weights
weights=MaskRCNN_ResNet50_FPN_V2_Weights.DEFAULT
COLOURS = [
tuple(int(colour_hex.strip('#')[i:i+2], 16) for i in (0, 2, 4))
for colour_hex in plt.rcParams['axes.prop_cycle'].by_key()['color']
]
# this is the show image function
def show(imgs):
if not isinstance(imgs, list):
imgs = [imgs]
fig, axs = plt.subplots(ncols=len(imgs), squeeze=False)
for i, img in enumerate(imgs):
img = img.detach()
img = tvtf.to_pil_image(img)
axs[0, i].imshow(np.asarray(img))
axs[0, i].set(xticklabels=[], yticklabels=[], xticks=[], yticks=[])
def load_img(filename):
img = cv2.imread(filename)
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
def preprocess_image(image):
image = tvtf.to_tensor(image)
image = image.unsqueeze(dim=0)
return image
def display_image(image):
fig, axes = plt.subplots(figsize=(12, 8))
if image.ndim == 2:
axes.imshow(image, cmap='gray', vmin=0, vmax=255)
else:
axes.imshow(image)
plt.show()
def display_image_pair(first_image, second_image):
#this funciton from Computer vision course notes (Dr. Lydia )
# When using plt.subplots, we can specify how many plottable regions we want to create through nrows and ncols
# Here we are creating a subplot with 2 columns and 1 row (i.e. side-by-side axes)
# When we do this, axes becomes a list of length 2 (Containing both plottable axes)
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(12, 8))
# TODO: Call imshow on each of the axes with the first and second images
# Make sure you handle both RGB and grayscale images
if first_image.ndim == 2:
axes[0].imshow(first_image, cmap='gray', vmin=0, vmax=255)
else:
axes[0].imshow(first_image)
if second_image.ndim == 2:
axes[1].imshow(second_image, cmap='gray', vmin=0, vmax=255)
else:
axes[1].imshow(second_image)
plt.show()
# this functions returns the detections
# det is the boxes, top left and bottom right cooridinates
# lbls are the class labels
# scores are the confidence. We use 0.5 as default
# masks are the segmentation masks.
def get_detections(maskrcnn, imgs, score_threshold=0.4): #person, dog, elephan, zebra, giraffe, toilet
''' Runs maskrcnn over all frames in vid, storing the detections '''
# Record how long the video is (in frames)
det = []
lbls = []
scores = []
masks = []
for img in imgs:
with torch.no_grad():
result = maskrcnn(preprocess_image(img))[0]
mask = result["scores"] > score_threshold
boxes = result["boxes"][mask].detach().cpu().numpy()
if (len(boxes)) > 10:
boxes = boxes[:10]
det.append(boxes)
lbls.append(result["labels"][mask].detach().cpu().numpy())
scores.append(result["scores"][mask].detach().cpu().numpy())
# masks.append(result["masks"][mask].detach().cpu().numpy())
masks.append(result["masks"][mask]) #I want this as a tensor
# det is bounding boxes, lbls is class labels, scores are confidences and masks are segmentation masks
return det, lbls, scores, masks
#det[0] are the bounding boxes in the left image
#det[1] are the bounding boxes in the right image
def draw_detections(img, det, colours=COLOURS, obj_order = None):
for i, (tlx, tly, brx, bry) in enumerate(det):
if obj_order is not None:
i = obj_order[i]
i %= len(colours)
c = colours[i]
cv2.rectangle(img, (tlx, tly), (brx, bry), color=colours[i], thickness=2)
#annotate the class labels
def annotate_class(img, det, lbls, conf=None, colours=COLOURS, class_map=weights.meta["categories"]):
for i, ( tlx, tly, brx, bry) in enumerate(det):
txt = class_map[lbls[i]]
if conf is not None:
txt += f' {conf[i]:1.3f}'
# A box with a border thickness draws half of that thickness to the left of the
# boundaries, while filling fills only within the boundaries, so we expand the filled region to match the border
offset = 1
cv2.rectangle(img,
(tlx-offset, tly-offset+12),
(tlx-offset+len(txt)*12, tly),
color=colours[i%len(colours)],
thickness=cv2.FILLED)
ff = cv2.FONT_HERSHEY_PLAIN
cv2.putText(img, txt, (tlx, tly-1+12), fontFace=ff, fontScale=1.0, color=(255,)*3)
def draw_instance_segmentation_mask(img, masks):
''' Draws segmentation masks over an img '''
seg_colours = np.zeros_like(img, dtype=np.uint8)
for i, mask in enumerate(masks):
col = (mask[0, :, :, None] * COLOURS[i])
seg_colours = np.maximum(seg_colours, col.astype(np.uint8))
cv2.addWeighted(img, 0.75, seg_colours, 0.75, 1.0, dst=img)
#get centr, top left and bottom right of boxes
def tlbr_to_center1(boxes):
points = []
for tlx, tly, brx, bry in boxes:
cx = (tlx+brx)/2
cy = (tly+bry)/2
points.append([cx, cy])
return points
def tlbr_to_corner(boxes):
points = []
for tlx, tly, brx, bry in boxes:
cx = (tlx+tlx)/2
cy = (tly+tly)/2
points.append((cx, cy))
return points
def tlbr_to_corner_br(boxes):
points = []
for tlx, tly, brx, bry in boxes:
cx = (brx+brx)/2
cy = (bry+bry)/2
points.append((cx, cy))
return points
def tlbr_to_area(boxes):
areas = []
for tlx, tly, brx, bry in boxes:
cx = (brx-tlx)
cy = (bry-tly)
areas.append(abs(cx*cy))
return areas
#get all distances from every object box to every other object box
#left image is boxes[0]
#right image is boxes[1]
def get_horiz_dist_centre(boxes):
pnts1 = np.array(tlbr_to_center1(boxes[0]))[:,0]
pnts2 = np.array(tlbr_to_center1(boxes[1]))[:,0]
return pnts1[:,None] - pnts2[None]
def get_horiz_dist_corner_tl(boxes):
pnts1 = np.array(tlbr_to_corner(boxes[0]))[:,0]
pnts2 = np.array(tlbr_to_corner(boxes[1]))[:,0]
return pnts1[:,None] - pnts2[None]
def get_horiz_dist_corner_br(boxes):
pnts1 = np.array(tlbr_to_corner_br(boxes[0]))[:,0]
pnts2 = np.array(tlbr_to_corner_br(boxes[1]))[:,0]
return pnts1[:,None] - pnts2[None]
def get_vertic_dist_centre(boxes):
pnts1 = np.array(tlbr_to_center1(boxes[0]))[:,1]
pnts2 = np.array(tlbr_to_center1(boxes[1]))[:,1]
return pnts1[:,None] - pnts2[None]
def get_area_diffs(boxes):
pnts1 = np.array(tlbr_to_area(boxes[0]))
pnts2 = np.array(tlbr_to_area(boxes[1]))
return abs(pnts1[:,None] - pnts2[None])
## get distance bentween corner and centre
centre=200
def get_dist_to_centre_tl(box, cntr = centre):
pnts = np.array(tlbr_to_corner(box))[:,0]
return abs(pnts - cntr)
def get_dist_to_centre_br(box, cntr = centre):
pnts = np.array(tlbr_to_corner_br(box))[:,0]
return abs(pnts - cntr)
#create the tracking cost function.
#consists of theree parts.
# 1. The move up and down of object centre of mass. Scale this up because we do not expect this to be very much.
# 2. The move left or right by the object. We only expect it to move right (from the left eye image). So penalise if it moves left.
# 3. The difference in area of pixels. Area of image is width x height, so divide by height, there for this will have max value of width
#create the tracking cost function.
#consists of theree parts.
# 1. The vertical move up and down of object centre of mass. Scale this up because we do not expect this to be very much.
# 2. The move left or right by the object. We only expect it to move right (from the left eye image). So penalise if it moves left.
# 3. The difference in area of pixels. Area of image is width x height, so divide by height, there for this will have max value of width
def get_cost(boxes, lbls = None, sz1 = 400):
alpha = sz1; beta = 10; gamma = 5
#vertical_dist, scale by gamma since can't move up or down
vert_dist = gamma*abs(get_vertic_dist_centre(boxes))
#horizonatl distance.
horiz_dist = get_horiz_dist_centre(boxes)
#increase cost if object has moved from right to left.
horiz_dist[horiz_dist<0] = beta*abs(horiz_dist[horiz_dist<0])
#area of box
area_diffs = get_area_diffs(boxes)/alpha
cost = np.array([vert_dist,horiz_dist,area_diffs])
cost=cost.sum(axis=0)
#add penalty term for different object classes
if lbls is not None:
for i in range(cost.shape[0]):
for j in range(cost.shape[1]):
if (lbls[0][i]!=lbls[1][j]):
cost[i,j]+=50500
return cost
def get_tracks(cost):
return scipy.optimize.linear_sum_assignment(cost)
def get_tracks_ij(cost):
tracks = scipy.optimize.linear_sum_assignment(cost)
return [[i,j] for i, j in zip(*tracks)]
#annotate the class labels
def annotate_class2(img, det, lbls,class_map, conf=None, colours=COLOURS):
for i, ( tlx, tly, brx, bry) in enumerate(det):
txt = class_map[i]
if conf is not None:
txt += f' {conf[i]:1.3f}'
# A box with a border thickness draws half of that thickness to the left of the
# boundaries, while filling fills only within the boundaries, so we expand the filled region to match the border
offset = 1
cv2.rectangle(img,
(tlx-offset, tly-offset+12),
(tlx-offset+len(txt)*12, tly),
color=colours[i%len(colours)],
thickness=cv2.FILLED)
ff = cv2.FONT_HERSHEY_PLAIN
cv2.putText(img, txt, (tlx, tly-1+12), fontFace=ff, fontScale=1.0, color=(255,)*3)