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

Class NormalText2Image

visual_chatgpt.py:741–775  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

739
740
741class NormalText2Image:
742 def __init__(self, device):
743 print(f"Initializing NormalText2Image to {device}")
744 self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
745 self.controlnet = ControlNetModel.from_pretrained(
746 "fusing/stable-diffusion-v1-5-controlnet-normal", torch_dtype=self.torch_dtype)
747 self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
748 "runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=StableDiffusionSafetyChecker.from_pretrained('CompVis/stable-diffusion-safety-checker'),
749 torch_dtype=self.torch_dtype)
750 self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)
751 self.pipe.to(device)
752 self.seed = -1
753 self.a_prompt = 'best quality, extremely detailed'
754 self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit,' \
755 ' fewer digits, cropped, worst quality, low quality'
756
757 @prompts(name="Generate Image Condition On Normal Map",
758 description="useful when you want to generate a new real image from both the user description and normal map. "
759 "like: generate a real image of a object or something from this normal map, "
760 "or generate a new real image of a object or something from the normal map. "
761 "The input to this tool should be a comma separated string of two, "
762 "representing the image_path and the user description")
763 def inference(self, inputs):
764 image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
765 image = Image.open(image_path)
766 self.seed = random.randint(0, 65535)
767 seed_everything(self.seed)
768 prompt = f'{instruct_text}, {self.a_prompt}'
769 image = self.pipe(prompt, image, num_inference_steps=20, eta=0.0, negative_prompt=self.n_prompt,
770 guidance_scale=9.0).images[0]
771 updated_image_path = get_new_image_name(image_path, func_name="normal2image")
772 image.save(updated_image_path)
773 print(f"\nProcessed NormalText2Image, Input Normal: {image_path}, Input Text: {instruct_text}, "
774 f"Output Image: {updated_image_path}")
775 return updated_image_path
776
777
778class VisualQuestionAnswering:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected