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

Class SegText2Image

visual_chatgpt.py:611–645  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

609 return updated_image_path
610
611class SegText2Image:
612 def __init__(self, device):
613 print(f"Initializing SegText2Image to {device}")
614 self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
615 self.controlnet = ControlNetModel.from_pretrained("fusing/stable-diffusion-v1-5-controlnet-seg",
616 torch_dtype=self.torch_dtype)
617 self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
618 "runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=StableDiffusionSafetyChecker.from_pretrained('CompVis/stable-diffusion-safety-checker'),
619 torch_dtype=self.torch_dtype)
620 self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)
621 self.pipe.to(device)
622 self.seed = -1
623 self.a_prompt = 'best quality, extremely detailed'
624 self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit,' \
625 ' fewer digits, cropped, worst quality, low quality'
626
627 @prompts(name="Generate Image Condition On Segmentations",
628 description="useful when you want to generate a new real image from both the user description and segmentations. "
629 "like: generate a real image of a object or something from this segmentation image, "
630 "or generate a new real image of a object or something from these segmentations. "
631 "The input to this tool should be a comma separated string of two, "
632 "representing the image_path and the user description")
633 def inference(self, inputs):
634 image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
635 image = Image.open(image_path)
636 self.seed = random.randint(0, 65535)
637 seed_everything(self.seed)
638 prompt = f'{instruct_text}, {self.a_prompt}'
639 image = self.pipe(prompt, image, num_inference_steps=20, eta=0.0, negative_prompt=self.n_prompt,
640 guidance_scale=9.0).images[0]
641 updated_image_path = get_new_image_name(image_path, func_name="segment2image")
642 image.save(updated_image_path)
643 print(f"\nProcessed SegText2Image, Input Seg: {image_path}, Input Text: {instruct_text}, "
644 f"Output Image: {updated_image_path}")
645 return updated_image_path
646
647
648class Image2Depth:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected