MCPcopy Create free account
hub / github.com/UVA-Computer-Vision-Lab/FrameINO / SAM2_Refine

Function SAM2_Refine

preprocess/track_FrameINO.py:215–398  ·  view source on GitHub ↗
(sam2_predictor, all_frames, GPU_offset, visual_store_folder, obj_Track_Traj, obj_Track_Visibility, 
                    original_start_frame_idx, potential_useful_region_box, min_area_required, debug)

Source from the content-addressed store, hash-verified

213
214
215def SAM2_Refine(sam2_predictor, all_frames, GPU_offset, visual_store_folder, obj_Track_Traj, obj_Track_Visibility,
216 original_start_frame_idx, potential_useful_region_box, min_area_required, debug):
217
218
219 # Prepare the folder needed
220 tmp_store_folder = "tmp_SAM2/process" + str(GPU_offset)
221 if os.path.exists(tmp_store_folder):
222 shutil.rmtree(tmp_store_folder)
223 os.makedirs(tmp_store_folder)
224 if os.path.exists(visual_store_folder):
225 shutil.rmtree(visual_store_folder)
226 os.makedirs(visual_store_folder)
227
228
229 # Init the tracking
230 panoptic_track_points_start = obj_Track_Traj[original_start_frame_idx]
231 point_valid_status = [True for _ in range(len(panoptic_track_points_start))]
232
233
234 # Write to a temp storage available
235 sam_frames = all_frames[original_start_frame_idx : ]
236 for frame_idx, frame in enumerate(sam_frames):
237 store_img_path = os.path.join(tmp_store_folder, str(frame_idx).zfill(4) + ".jpg")
238 cv2.imwrite(store_img_path, cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
239
240
241 # SAM Process
242 info, bbox_info = [], []
243 with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
244
245 # Init the state
246 state = sam2_predictor.init_state(tmp_store_folder)
247 sam2_predictor.reset_state(state)
248
249 # Add new prompts and instantly get the output on the same frame
250 labels = np.array([1] * len(panoptic_track_points_start), np.int32) # All are the same label
251 frame_idx, object_ids, masks = sam2_predictor.add_new_points_or_box(
252 state,
253 frame_idx = 0,
254 obj_id = 1, # Only consider single isntance now
255 points = panoptic_track_points_start, # Use points in the first frame
256 labels = labels,
257 )
258
259 # Iterate all frames and Recognize multiple masks
260 for frame_idx, object_ids, masks in sam2_predictor.propagate_in_video(state, start_frame_idx=0):
261 for obj_idx, out_obj_id in enumerate(object_ids):
262
263 # Convert to boolean mask and 3 channels
264 segmentation_mask_raw = (masks[obj_idx] > 0.0).cpu().numpy().astype(np.uint8)
265 _, height, width = segmentation_mask_raw.shape
266 segmentation_mask_cat = np.stack([segmentation_mask_raw, segmentation_mask_raw, segmentation_mask_raw], axis=-1)[0]
267
268 # Prepare masked segmentation images
269 segmentation_img = segmentation_mask_cat * cv2.cvtColor(sam_frames[frame_idx], cv2.COLOR_BGR2RGB)
270 segmentation_mask = segmentation_mask_raw * 255
271
272

Callers 1

single_processFunction · 0.85

Calls 2

mask_to_bboxFunction · 0.85
sampleMethod · 0.45

Tested by

no test coverage detected