-
Notifications
You must be signed in to change notification settings - Fork 3
/
week2.py
246 lines (184 loc) · 9.15 KB
/
week2.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
import os
import numpy as np
import cv2
from tqdm import trange
import imageio
import time
from src.utils.aicity_reader import AICityChallengeAnnotationReader
from src.segmentation.background_estimation import SingleGaussianBackgroundModel, sota_bg_subtractor
from src.utils.processing import postprocess, bounding_boxes
from src.evaluation.average_precision import mean_average_precision
def task1_2(adaptive, random_search, model_frac=0.25, min_width=120, max_width=800, min_height=100, max_height=600,
debug=0, save_path=None):
reader = AICityChallengeAnnotationReader(path='data/AICity_data/train/S03/c010/gt/gt.txt')
gt = reader.get_annotations(classes=['car'], only_not_parked=True)
roi = cv2.imread('data/AICity_data/train/S03/c010/roi.jpg', cv2.IMREAD_GRAYSCALE)
bg_model = SingleGaussianBackgroundModel(video_path='data/AICity_data/train/S03/c010/vdo.avi')
video_length = bg_model.length
bg_model.fit(start=0, length=int(video_length * model_frac))
start_frame = int(video_length * model_frac)
end_frame = video_length
# hyperparameter search
if random_search:
alphas = np.random.choice(np.linspace(2, 4, 50), 25)
rhos = np.random.choice(np.linspace(0.001, 0.1, 50), 25) if adaptive else [0]
combinations = [(alpha, rho) for alpha, rho in zip(alphas, rhos)]
else:
alphas = [2, 2.5, 3, 3.5, 4]
rhos = [0.005, 0.01, 0.025, 0.05, 0.1] if adaptive else [0]
combinations = [(alpha, rho) for alpha in alphas for rho in rhos]
for alpha, rho in combinations:
if save_path:
writer = imageio.get_writer(os.path.join(save_path, f'task1_2_alpha{alpha:.1f}_rho{rho:.3f}.gif'), fps=10)
y_true = []
y_pred = []
for frame in trange(start_frame, end_frame, desc='evaluating frames'):
_, mask, _ = bg_model.evaluate(frame=frame, alpha=alpha, rho=rho)
mask = mask & roi
mask = postprocess(mask)
detections = bounding_boxes(mask, min_height, max_height, min_width, max_width, frame)
annotations = gt.get(frame, [])
if debug >= 1 or save_path:
img = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
for det in detections:
cv2.rectangle(img, (det.xtl, det.ytl), (det.xbr, det.ybr), (0, 255, 0), 2)
for det in annotations:
cv2.rectangle(img, (int(det.xtl), int(det.ytl)), (int(det.xbr), int(det.ybr)), (0, 0, 255), 2)
if save_path:
writer.append_data(img)
if debug >= 1:
cv2.imshow('result', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
y_pred.append(detections)
y_true.append(annotations)
cv2.destroyAllWindows()
if save_path:
writer.close()
ap, prec, rec = mean_average_precision(y_true, y_pred, classes=['car'])
print(f'alpha: {alpha:.1f}, rho: {rho:.3f}, AP: {ap:.4f}')
def task1(debug=0, save_path=None):
"""
Gaussian modelling
"""
task1_2(False, False, debug=debug, save_path=save_path)
def task2(debug=0, save_path=None):
"""
Adaptive modelling
"""
task1_2(True, True, debug=debug, save_path=save_path)
def task3(methods, model_frac=0.25, min_width=120, max_width=800, min_height=100, max_height=600, save_path=None, debug=0):
"""
Comparison with the state of the art
"""
reader = AICityChallengeAnnotationReader(path='data/AICity_data/train/S03/c010/gt/gt.txt')
gt = reader.get_annotations(classes=['car'], only_not_parked=True)
roi = cv2.imread('data/AICity_data/train/S03/c010/roi.jpg', cv2.IMREAD_GRAYSCALE)
cap = cv2.VideoCapture('data/AICity_data/train/S03/c010/vdo.avi')
video_length = cap.get(cv2.CAP_PROP_FRAME_COUNT)
start_frame = int(video_length * model_frac)
end_frame = int(video_length)
for method in methods:
backSub = sota_bg_subtractor(method)
for _ in trange(start_frame, desc='modelling background'):
ret, img = cap.read()
backSub.apply(img)
if save_path:
writer = imageio.get_writer(os.path.join(save_path, f'task3_method_'+method+'.gif'), fps=10)
y_pred = []
y_true = []
for frame in trange(start_frame, end_frame, desc='evaluating frames'):
cap.set(cv2.CAP_PROP_POS_FRAMES, frame)
ret, img = cap.read()
mask = backSub.apply(img)
mask = mask & roi
mask = postprocess(mask)
detections = bounding_boxes(mask, min_height, max_height, min_width, max_width, frame)
annotations = gt.get(frame, [])
if debug >= 1 or save_path:
img = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
for det in detections:
cv2.rectangle(img, (det.xtl, det.ytl), (det.xbr, det.ybr), (0, 255, 0), 2)
for det in annotations:
cv2.rectangle(img, (int(det.xtl), int(det.ytl)), (int(det.xbr), int(det.ybr)), (0, 0, 255), 2)
if save_path:
writer.append_data(img)
elif debug == 1:
cv2.imshow('result', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
y_pred.append(detections)
y_true.append(annotations)
cv2.destroyAllWindows()
if save_path:
writer.close()
ap, prec, rec = mean_average_precision(y_true, y_pred, classes=['car'])
print(f'Method: {method}, AP: {ap:.4f}, Precision: {prec:.4f}, Recall: {rec:.4f}')
def task4(adaptive, random_search, color_space, channels, model_frac=0.25, save_path=None, min_width=120, max_width=800, min_height=100, max_height=600, debug=0):
"""
Color modelling
"""
n_ch = len(channels)
# Read information
reader = AICityChallengeAnnotationReader(path='data/AICity_data/train/S03/c010/gt/gt.txt')
gt = reader.get_annotations(classes=['car'], only_not_parked=True)
roi = cv2.imread('data/AICity_data/train/S03/c010/roi.jpg', cv2.IMREAD_GRAYSCALE)
# Model Background
bg_model = SingleGaussianBackgroundModel(video_path='data/AICity_data/train/S03/c010/vdo.avi',
color_space=color_space, channels=channels, resize=None)
video_length = bg_model.length
bg_model.fit(start=0, length=int(video_length * 0.25))
# Video length
start_frame = int(video_length * model_frac)
end_frame = int(video_length )
# hyperparameter search
if random_search:
alphas = np.random.choice(np.linspace(2, 4, 50), 25)
rhos = np.random.choice(np.linspace(0.001, 0.1, 50), 25) if adaptive else [0]
combinations = [(alpha, rho) for alpha, rho in zip(alphas, rhos)]
else:
alphas = [3.5]
rhos = [0.005] if adaptive else [0]
combinations = [(alpha, rho) for alpha in alphas for rho in rhos]
for alpha, rho in combinations:
y_true = []
y_pred = []
if save_path:
gif_name = f'100_task3_alpha_{str(alpha)}_rho_{str(rho)}_color_{color_space}_channels_{str(n_ch)}_{time.time()}.gif'
writer = imageio.get_writer(os.path.join(save_path, gif_name), fps=10)
for frame in trange(start_frame, end_frame, desc=f'obtaining foreground and detecting objects. Alpha {alpha} Rho {rho}'):
if frame == 635:
break
frame_img, mask, _ = bg_model.evaluate(frame=frame, alpha=alpha)
mask = mask & roi
non_post_mask = mask
mask = postprocess(mask)
detections = bounding_boxes(mask, min_height, max_height, min_width, max_width, frame)
annotations = gt.get(frame, [])
if save_path:
img = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
for det in detections:
cv2.rectangle(img, (det.xtl, det.ytl), (det.xbr, det.ybr), (0, 255, 0), 3)
for det in annotations:
cv2.rectangle(img, (int(det.xtl), int(det.ytl)), (int(det.xbr), int(det.ybr )), (0, 0, 255), 2)
writer.append_data(img)
if debug >=1:
shape = (480, 270)
cv2.imshow(f'BGR Image', cv2.resize(img, shape))
cv2.imshow(f'Segmentation using {color_space}', cv2.resize(non_post_mask, shape))
cv2.imshow(f'Segmentation Morphed using {color_space}', cv2.resize(mask, shape))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
y_pred.append(detections)
y_true.append(annotations)
cv2.destroyAllWindows()
if save_path:
writer.close()
ap, prec, rec = mean_average_precision(y_true, y_pred, classes=['car'])
print(f'alpha: {alpha:.1f}, rho: {rho:.3f}, AP: {ap:.4f}')
print(f'prec: {prec:.4f}, Recall {rec:.4f}')
if __name__ == '__main__':
#task1(debug=1)
task2(debug=1)
#task3(['MOG', 'MOG2', 'LSBP', 'GMG', 'KNN', 'GSOC', 'CNT'], debug=1)
task4(adaptive=True, random_search=False, color_space='rgb', channels=(0,1,2), save_path='results/', debug=0)