MCPcopy Create free account
hub / github.com/chenfei-wu/TaskMatrix / BackgroundRemoving

Class BackgroundRemoving

visual_chatgpt.py:1418–1462  ·  view source on GitHub ↗

using to remove the background of the given picture

Source from the content-addressed store, hash-verified

1416 return updated_image_path
1417
1418class BackgroundRemoving:
1419 '''
1420 using to remove the background of the given picture
1421 '''
1422 template_model = True
1423 def __init__(self,VisualQuestionAnswering:VisualQuestionAnswering, Text2Box:Text2Box, Segmenting:Segmenting):
1424 self.vqa = VisualQuestionAnswering
1425 self.obj_segmenting = ObjectSegmenting(Text2Box,Segmenting)
1426
1427 @prompts(name="Remove the background",
1428 description="useful when you want to extract the object or remove the background,"
1429 "the input should be a string image_path"
1430 )
1431 def inference(self, image_path):
1432 '''
1433 given a image, return the picture only contains the extracted main object
1434 '''
1435 updated_image_path = None
1436
1437 mask = self.get_mask(image_path)
1438
1439 image = Image.open(image_path)
1440 mask = Image.fromarray(mask)
1441 image.putalpha(mask)
1442
1443 updated_image_path = get_new_image_name(image_path, func_name="detect-something")
1444 image.save(updated_image_path)
1445
1446 return updated_image_path
1447
1448 def get_mask(self, image_path):
1449 '''
1450 Description:
1451 given an image path, return the mask of the main object.
1452 Args:
1453 image_path (string): the file path of the image
1454 Outputs:
1455 mask (numpy.ndarray): H x W
1456 '''
1457 vqa_input = f"{image_path}, what is the main object in the image?"
1458 text_prompt = self.vqa.inference(vqa_input)
1459
1460 mask = self.obj_segmenting.get_mask(image_path,text_prompt)
1461
1462 return mask
1463
1464
1465class ConversationBot:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected