(self, image_path, text_prompt)
| 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 | |
| 1357 | class ImageEditing: |
no test coverage detected