| 399 | |
| 400 | |
| 401 | class LineText2Image: |
| 402 | def __init__(self, device): |
| 403 | print(f"Initializing LineText2Image to {device}") |
| 404 | self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32 |
| 405 | self.controlnet = ControlNetModel.from_pretrained("fusing/stable-diffusion-v1-5-controlnet-mlsd", |
| 406 | torch_dtype=self.torch_dtype) |
| 407 | self.pipe = StableDiffusionControlNetPipeline.from_pretrained( |
| 408 | "runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=StableDiffusionSafetyChecker.from_pretrained('CompVis/stable-diffusion-safety-checker'), |
| 409 | torch_dtype=self.torch_dtype |
| 410 | ) |
| 411 | self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config) |
| 412 | self.pipe.to(device) |
| 413 | self.seed = -1 |
| 414 | self.a_prompt = 'best quality, extremely detailed' |
| 415 | self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, ' \ |
| 416 | 'fewer digits, cropped, worst quality, low quality' |
| 417 | |
| 418 | @prompts(name="Generate Image Condition On Line Image", |
| 419 | description="useful when you want to generate a new real image from both the user description " |
| 420 | "and a straight line image. " |
| 421 | "like: generate a real image of a object or something from this straight line image, " |
| 422 | "or generate a new real image of a object or something from this straight lines. " |
| 423 | "The input to this tool should be a comma separated string of two, " |
| 424 | "representing the image_path and the user description. ") |
| 425 | def inference(self, inputs): |
| 426 | image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:]) |
| 427 | image = Image.open(image_path) |
| 428 | self.seed = random.randint(0, 65535) |
| 429 | seed_everything(self.seed) |
| 430 | prompt = f'{instruct_text}, {self.a_prompt}' |
| 431 | image = self.pipe(prompt, image, num_inference_steps=20, eta=0.0, negative_prompt=self.n_prompt, |
| 432 | guidance_scale=9.0).images[0] |
| 433 | updated_image_path = get_new_image_name(image_path, func_name="line2image") |
| 434 | image.save(updated_image_path) |
| 435 | print(f"\nProcessed LineText2Image, Input Line: {image_path}, Input Text: {instruct_text}, " |
| 436 | f"Output Text: {updated_image_path}") |
| 437 | return updated_image_path |
| 438 | |
| 439 | |
| 440 | class Image2Hed: |
nothing calls this directly
no outgoing calls
no test coverage detected