MCPcopy Create free account
hub / github.com/CAMMA-public/SurgLatentGraph / SAMQueryVisualizer

Class SAMQueryVisualizer

visualizer/SAMQueryVisualizer.py:12–50  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10
11@VISUALIZERS.register_module()
12class SAMQueryVisualizer(DetLocalVisualizer):
13 def __init__(self, name: str, prefix: str = 'endoscapes', draw: bool = False, **kwargs):
14 super().__init__(**kwargs)
15 self.prefix = prefix
16 self.draw = draw
17
18 def add_datasample(self, name: str, image: np.ndarray,
19 data_sample: Optional['DetDataSample'] = None,
20 out_file: Optional[str] = None, **kwargs):
21
22 if self.draw:
23 super().add_datasample(name, image, data_sample, out_file=out_file, **kwargs)
24
25 save_dir = os.path.join('sam_queries', self.prefix)
26 if not os.path.exists(save_dir):
27 os.makedirs(save_dir)
28 query_filename = str(data_sample.img_id) + '.npz'
29
30 mask_anns = self.encode_masks(data_sample.pred_instances.masks)
31 data_subsample = dict(
32 img_path=data_sample.img_path,
33 bboxes=data_sample.pred_instances.bboxes.cpu().numpy(),
34 feats=data_sample.pred_instances.feats.cpu().numpy(),
35 graph_feats=data_sample.pred_instances.graph_feats.cpu().numpy(),
36 masks=mask_anns,
37 )
38 np.savez(os.path.join(save_dir, query_filename), data_subsample)
39
40 def encode_masks(self, raw_masks):
41 mask_rles = mask_to_rle_pytorch(raw_masks)
42 coco_rles = [coco_encode_rle(m) for m in mask_rles]
43 mask_anns = []
44 for idx in range(len(coco_rles)):
45 ann = {
46 "segmentation": coco_rles[idx],
47 }
48 mask_anns.append(ann)
49
50 return mask_anns

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected