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

Class HedText2Image

visual_chatgpt.py:459–495  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

457
458
459class HedText2Image:
460 def __init__(self, device):
461 print(f"Initializing HedText2Image to {device}")
462 self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
463 self.controlnet = ControlNetModel.from_pretrained("fusing/stable-diffusion-v1-5-controlnet-hed",
464 torch_dtype=self.torch_dtype)
465 self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
466 "runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=StableDiffusionSafetyChecker.from_pretrained('CompVis/stable-diffusion-safety-checker'),
467 torch_dtype=self.torch_dtype
468 )
469 self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)
470 self.pipe.to(device)
471 self.seed = -1
472 self.a_prompt = 'best quality, extremely detailed'
473 self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, ' \
474 'fewer digits, cropped, worst quality, low quality'
475
476 @prompts(name="Generate Image Condition On Soft Hed Boundary Image",
477 description="useful when you want to generate a new real image from both the user description "
478 "and a soft hed boundary image. "
479 "like: generate a real image of a object or something from this soft hed boundary image, "
480 "or generate a new real image of a object or something from this hed boundary. "
481 "The input to this tool should be a comma separated string of two, "
482 "representing the image_path and the user description")
483 def inference(self, inputs):
484 image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
485 image = Image.open(image_path)
486 self.seed = random.randint(0, 65535)
487 seed_everything(self.seed)
488 prompt = f'{instruct_text}, {self.a_prompt}'
489 image = self.pipe(prompt, image, num_inference_steps=20, eta=0.0, negative_prompt=self.n_prompt,
490 guidance_scale=9.0).images[0]
491 updated_image_path = get_new_image_name(image_path, func_name="hed2image")
492 image.save(updated_image_path)
493 print(f"\nProcessed HedText2Image, Input Hed: {image_path}, Input Text: {instruct_text}, "
494 f"Output Image: {updated_image_path}")
495 return updated_image_path
496
497
498class Image2Scribble:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected