-
Notifications
You must be signed in to change notification settings - Fork 0
/
seg_track_anything.py
299 lines (254 loc) · 10.6 KB
/
seg_track_anything.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
import os
import cv2
from model_args import segtracker_args,sam_args,aot_args
from PIL import Image
from aot_tracker import _palette
import numpy as np
import torch
import gc
import imageio
from scipy.ndimage import binary_dilation
def save_prediction(pred_mask,output_dir,file_name):
save_mask = Image.fromarray(pred_mask.astype(np.uint8))
save_mask = save_mask.convert(mode='P')
save_mask.putpalette(_palette)
save_mask.save(os.path.join(output_dir,file_name))
def colorize_mask(pred_mask):
save_mask = Image.fromarray(pred_mask.astype(np.uint8))
save_mask = save_mask.convert(mode='P')
save_mask.putpalette(_palette)
save_mask = save_mask.convert(mode='RGB')
return np.array(save_mask)
def draw_mask(img, mask, alpha=0.5, id_countour=False):
img_mask = np.zeros_like(img)
img_mask = img
if id_countour:
# very slow ~ 1s per image
obj_ids = np.unique(mask)
obj_ids = obj_ids[obj_ids!=0]
for id in obj_ids:
# Overlay color on binary mask
if id <= 255:
color = _palette[id*3:id*3+3]
else:
color = [0,0,0]
foreground = img * (1-alpha) + np.ones_like(img) * alpha * np.array(color)
binary_mask = (mask == id)
# Compose image
img_mask[binary_mask] = foreground[binary_mask]
countours = binary_dilation(binary_mask,iterations=1) ^ binary_mask
img_mask[countours, :] = 0
else:
binary_mask = (mask!=0)
countours = binary_dilation(binary_mask,iterations=1) ^ binary_mask
foreground = img*(1-alpha)+colorize_mask(mask)*alpha
img_mask[binary_mask] = foreground[binary_mask]
img_mask[countours,:] = 0
return img_mask.astype(img.dtype)
"""aot_model2ckpt = {
"deaotb": "./ckpt/DeAOTB_PRE_YTB_DAV.pth",
"deaotl": "./ckpt/DeAOTL_PRE_YTB_DAV",
"r50_deaotl": "./ckpt/R50_DeAOTL_PRE_YTB_DAV.pth",
}"""
aot_model2ckpt = {
"deaotb": f'{os.path.join(os.path.abspath(__file__), "ckpt")}/DeAOTB_PRE_YTB_DAV.pth',
"deaotl": f'{os.path.join(os.path.abspath(__file__), "ckpt")}/DeAOTL_PRE_YTB_DAV',
"r50_deaotl": f'{os.path.join(os.path.abspath(__file__), "ckpt")}/R50_DeAOTL_PRE_YTB_DAV.pth',
}
def seg_track_anything(input_video_file, aot_model, sam_gap, max_obj_num, points_per_side):
video_name = os.path.basename(input_video_file).split('.')[0]
io_args = {
'input_video': f'{input_video_file}',
'output_mask_dir': f'./assets/{video_name}_masks',
'output_video': f'./assets/{video_name}_seg.mp4', # keep same format as input video
'output_gif': f'./assets/{video_name}_seg.gif',
}
# reset aot args
aot_args["model"] = aot_model
aot_args["model_path"] = aot_model2ckpt[aot_model]
# reset sam args
segtracker_args["sam_gap"] = sam_gap
segtracker_args["max_obj_num"] = max_obj_num
sam_args["generator_args"]["points_per_side"] = points_per_side
output_dir = io_args['output_mask_dir']
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# source video to segment
cap = cv2.VideoCapture(io_args['input_video'])
fps = cap.get(cv2.CAP_PROP_FPS)
# output masks
output_dir = io_args['output_mask_dir']
if not os.path.exists(output_dir):
os.makedirs(output_dir)
pred_list = []
# start to track
torch.cuda.empty_cache()
gc.collect()
sam_gap = segtracker_args['sam_gap']
frame_idx = 0
segtracker = SegTracker(segtracker_args,sam_args,aot_args)
segtracker.restart_tracker()
with torch.cuda.amp.autocast():
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
if frame_idx == 0:
pred_mask = segtracker.seg(frame)
torch.cuda.empty_cache()
gc.collect()
segtracker.add_reference(frame, pred_mask)
elif (frame_idx % sam_gap) == 0:
seg_mask = segtracker.seg(frame)
torch.cuda.empty_cache()
gc.collect()
track_mask = segtracker.track(frame)
# find new objects, and update tracker with new objects
new_obj_mask = segtracker.find_new_objs(track_mask,seg_mask)
save_prediction(new_obj_mask,output_dir,str(frame_idx)+'_new.png')
pred_mask = track_mask + new_obj_mask
# segtracker.restart_tracker()
segtracker.add_reference(frame, pred_mask)
else:
pred_mask = segtracker.track(frame,update_memory=True)
torch.cuda.empty_cache()
gc.collect()
save_prediction(pred_mask,output_dir,str(frame_idx)+'.png')
pred_list.append(pred_mask)
print("processed frame {}, obj_num {}".format(frame_idx,segtracker.get_obj_num()),end='\r')
frame_idx += 1
cap.release()
print('\nfinished')
######################
# Visualization
######################
# draw pred mask on frame and save as a video
cap = cv2.VideoCapture(io_args['input_video'])
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
num_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))
out = cv2.VideoWriter(io_args['output_video'], fourcc, fps, (width, height))
# for .mp4
frame_idx = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
pred_mask = pred_list[frame_idx]
masked_frame = draw_mask(frame,pred_mask)
# masked_frame = masked_pred_list[frame_idx]
masked_frame = cv2.cvtColor(masked_frame,cv2.COLOR_RGB2BGR)
out.write(masked_frame)
print('frame {} writed'.format(frame_idx),end='\r')
frame_idx += 1
out.release()
cap.release()
print("\n{} saved".format(io_args['output_video']))
print('\nfinished')
# save colorized masks as a gif
imageio.mimsave(io_args['output_gif'],pred_list,fps=fps)
print("{} saved".format(io_args['output_gif']))
# zip predicted mask
os.system(f"zip -r ./assets/{video_name}_pred_mask.zip {io_args['output_mask_dir']}")
return io_args["output_video"], f"./assets/{video_name}_pred_mask.zip"
def tracking_objects_in_video(SegTracker, input_video):
video_name = os.path.basename(input_video).split('.')[0]
io_args = {
'input_video': f'{input_video}',
'output_mask_dir': f'{os.path.join(os.path.abspath(__file__))}/../results/{video_name}_masks',
'output_video': f'{os.path.join(os.path.abspath(__file__))}/../results/{video_name}_track.mp4',
'output_gif': f'{os.path.join(os.path.abspath(__file__))}/../results/{video_name}_track.gif',
}
# source video to segment
cap = cv2.VideoCapture(input_video)
fps = cap.get(cv2.CAP_PROP_FPS)
# output masks
output_dir = io_args['output_mask_dir']
if not os.path.exists(output_dir):
os.makedirs(output_dir)
pred_list = []
masked_pred_list = []
torch.cuda.empty_cache()
gc.collect()
sam_gap = SegTracker.sam_gap
frame_idx = 0
with torch.cuda.amp.autocast():
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
if frame_idx == 0:
pred_list.append(SegTracker.refined_merged_mask)
frame_idx += 1
continue
elif (frame_idx % sam_gap) == 0:
seg_mask = SegTracker.seg(frame)
torch.cuda.empty_cache()
gc.collect()
track_mask = SegTracker.track(frame)
# find new objects, and update tracker with new objects
new_obj_mask = SegTracker.find_new_objs(track_mask,seg_mask)
save_prediction(new_obj_mask,output_dir,str(frame_idx)+'_new.png')
pred_mask = track_mask + new_obj_mask
# segtracker.restart_tracker()
SegTracker.add_reference(frame, pred_mask)
else:
pred_mask = SegTracker.track(frame,update_memory=True)
torch.cuda.empty_cache()
gc.collect()
save_prediction(pred_mask,output_dir,str(frame_idx)+'.png')
pred_list.append(pred_mask)
print("processed frame {}, obj_num {}".format(frame_idx, SegTracker.get_obj_num()),end='\r')
frame_idx += 1
cap.release()
print('\nfinished detection.')
##################
# Visualization
##################
# draw pred mask on frame and save as a video
cap = cv2.VideoCapture(io_args['input_video'])
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
num_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
# if io_args['input_video'][-3:]=='mp4':
# fourcc = cv2.VideoWriter_fourcc(*"mp4v")
# elif io_args['input_video'][-3:] == 'avi':
# fourcc = cv2.VideoWriter_fourcc(*"MJPG")
# # fourcc = cv2.VideoWriter_fourcc(*"XVID")
# else:
# fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))
out = cv2.VideoWriter(io_args['output_video'], fourcc, fps, (width, height))
frame_idx = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
pred_mask = pred_list[frame_idx]
masked_frame = draw_mask(frame, pred_mask)
masked_pred_list.append(masked_frame)
masked_frame = cv2.cvtColor(masked_frame,cv2.COLOR_RGB2BGR)
out.write(masked_frame)
print('frame {} writed'.format(frame_idx),end='\r')
frame_idx += 1
out.release()
cap.release()
print("\n{} saved".format(io_args['output_video']))
print('\nfinished track.')
# save colorized masks as a gif
imageio.mimsave(io_args['output_gif'], masked_pred_list, fps=fps)
print("{} saved".format(io_args['output_gif']))
# zip predicted mask
os.system(f"zip -r ./assets/{video_name}_pred_mask.zip {io_args['output_mask_dir']}")
# manually release memory (after cuda out of memory)
del SegTracker
torch.cuda.empty_cache()
gc.collect()
return io_args['output_video'], f"./assets/{video_name}_pred_mask.zip"