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

Method inference_replace_sam

visual_chatgpt.py:1391–1416  ·  view source on GitHub ↗
(self,inputs)

Source from the content-addressed store, hash-verified

1389 "The input to this tool should be a comma separated string of three, "
1390 "representing the image_path, the object to be replaced, the object to be replaced with ")
1391 def inference_replace_sam(self,inputs):
1392 image_path, to_be_replaced_txt, replace_with_txt = inputs.split(",")
1393
1394 print(f"image_path={image_path}, to_be_replaced_txt={to_be_replaced_txt}")
1395 image_pil, image = self.grounding.load_image(image_path)
1396 boxes_filt, pred_phrases = self.grounding.get_grounding_boxes(image, to_be_replaced_txt)
1397 image = cv2.imread(image_path)
1398 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
1399 self.sam.sam_predictor.set_image(image)
1400 masks = self.sam.get_mask_with_boxes(image_pil, image, boxes_filt)
1401 mask = torch.sum(masks, dim=0).unsqueeze(0)
1402 mask = torch.where(mask > 0, True, False)
1403 mask = mask.squeeze(0).squeeze(0).cpu() #tensor
1404
1405 mask = self.pad_edge(mask,padding=20) #numpy
1406 mask_image = Image.fromarray(mask)
1407
1408 updated_image = self.inpaint(prompt=replace_with_txt, image=image_pil,
1409 mask_image=mask_image)
1410 updated_image_path = get_new_image_name(image_path, func_name="replace-something")
1411 updated_image = updated_image.resize(image_pil.size)
1412 updated_image.save(updated_image_path)
1413 print(
1414 f"\nProcessed ImageEditing, Input Image: {image_path}, Replace {to_be_replaced_txt} to {replace_with_txt}, "
1415 f"Output Image: {updated_image_path}")
1416 return updated_image_path
1417
1418class BackgroundRemoving:
1419 '''

Callers 1

inference_removeMethod · 0.95

Calls 6

pad_edgeMethod · 0.95
get_new_image_nameFunction · 0.85
load_imageMethod · 0.80
get_grounding_boxesMethod · 0.80
set_imageMethod · 0.80
get_mask_with_boxesMethod · 0.80

Tested by

no test coverage detected