| 343 | |
| 344 | |
| 345 | class CannyText2Image: |
| 346 | def __init__(self, device): |
| 347 | print(f"Initializing CannyText2Image to {device}") |
| 348 | self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32 |
| 349 | self.controlnet = ControlNetModel.from_pretrained("fusing/stable-diffusion-v1-5-controlnet-canny", |
| 350 | torch_dtype=self.torch_dtype) |
| 351 | self.pipe = StableDiffusionControlNetPipeline.from_pretrained( |
| 352 | "runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=StableDiffusionSafetyChecker.from_pretrained('CompVis/stable-diffusion-safety-checker'), |
| 353 | torch_dtype=self.torch_dtype) |
| 354 | self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config) |
| 355 | self.pipe.to(device) |
| 356 | self.seed = -1 |
| 357 | self.a_prompt = 'best quality, extremely detailed' |
| 358 | self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, ' \ |
| 359 | 'fewer digits, cropped, worst quality, low quality' |
| 360 | |
| 361 | @prompts(name="Generate Image Condition On Canny Image", |
| 362 | description="useful when you want to generate a new real image from both the user description and a canny image." |
| 363 | " like: generate a real image of a object or something from this canny image," |
| 364 | " or generate a new real image of a object or something from this edge image. " |
| 365 | "The input to this tool should be a comma separated string of two, " |
| 366 | "representing the image_path and the user description. ") |
| 367 | def inference(self, inputs): |
| 368 | image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:]) |
| 369 | image = Image.open(image_path) |
| 370 | self.seed = random.randint(0, 65535) |
| 371 | seed_everything(self.seed) |
| 372 | prompt = f'{instruct_text}, {self.a_prompt}' |
| 373 | image = self.pipe(prompt, image, num_inference_steps=20, eta=0.0, negative_prompt=self.n_prompt, |
| 374 | guidance_scale=9.0).images[0] |
| 375 | updated_image_path = get_new_image_name(image_path, func_name="canny2image") |
| 376 | image.save(updated_image_path) |
| 377 | print(f"\nProcessed CannyText2Image, Input Canny: {image_path}, Input Text: {instruct_text}, " |
| 378 | f"Output Text: {updated_image_path}") |
| 379 | return updated_image_path |
| 380 | |
| 381 | |
| 382 | class Image2Line: |
nothing calls this directly
no outgoing calls
no test coverage detected