MCPcopy
hub / github.com/chenfei-wu/TaskMatrix / get_mask

Method get_mask

visual_chatgpt.py:1329–1354  ·  view source on GitHub ↗
(self, image_path, text_prompt)

Source from the content-addressed store, hash-verified

1327 return new_mask
1328
1329 def get_mask(self, image_path, text_prompt):
1330
1331 print(f"image_path={image_path}, text_prompt={text_prompt}")
1332 # image_pil (PIL.Image.Image) -> size: W x H
1333 # image (numpy.ndarray) -> H x W x 3
1334 image_pil, image = self.grounding.load_image(image_path)
1335
1336 boxes_filt, pred_phrases = self.grounding.get_grounding_boxes(image, text_prompt)
1337 image = cv2.imread(image_path)
1338 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
1339 self.sam.sam_predictor.set_image(image)
1340
1341 # masks (torch.tensor) -> N x 1 x H x W
1342 masks = self.sam.get_mask_with_boxes(image_pil, image, boxes_filt)
1343
1344 # merged_mask -> H x W
1345 merged_mask = self.merge_masks(masks)
1346 # draw output image
1347
1348 for mask in masks:
1349 image = self.sam.show_mask(mask[0].cpu().numpy(), image, random_color=True, transparency=0.3)
1350
1351
1352 merged_mask_image = Image.fromarray(merged_mask)
1353
1354 return merged_mask
1355
1356
1357class ImageEditing:

Callers 1

get_maskMethod · 0.45

Calls 6

merge_masksMethod · 0.95
load_imageMethod · 0.80
get_grounding_boxesMethod · 0.80
set_imageMethod · 0.80
get_mask_with_boxesMethod · 0.80
show_maskMethod · 0.80

Tested by

no test coverage detected