|
|
|
import numpy as np |
|
import torch |
|
|
|
from transformers.tools.base import Tool, get_default_device |
|
from transformers.utils import ( |
|
is_accelerate_available, |
|
is_diffusers_available, |
|
is_opencv_available, |
|
is_vision_available, |
|
) |
|
|
|
|
|
if is_vision_available(): |
|
from PIL import Image |
|
|
|
if is_diffusers_available(): |
|
from diffusers import ControlNetModel, StableDiffusionControlNetPipeline, UniPCMultistepScheduler |
|
|
|
if is_opencv_available(): |
|
import cv2 |
|
|
|
|
|
IMAGE_TRANSFORMATION_DESCRIPTION = ( |
|
"This is a tool that transforms an image according to a prompt. It takes two inputs: `image`, which should be " |
|
"the image to transform, and `prompt`, which should be the prompt to use to change it. It returns the " |
|
"modified image." |
|
) |
|
|
|
|
|
class ImageTransformationTool(Tool): |
|
default_stable_diffusion_checkpoint = "runwayml/stable-diffusion-v1-5" |
|
default_controlnet_checkpoint = "lllyasviel/control_v11p_sd15_canny" |
|
description = IMAGE_TRANSFORMATION_DESCRIPTION |
|
inputs = ['image', 'text'] |
|
outputs = ['image'] |
|
|
|
def __init__(self, device=None, controlnet=None, stable_diffusion=None, **hub_kwargs) -> None: |
|
if not is_accelerate_available(): |
|
raise ImportError("Accelerate should be installed in order to use tools.") |
|
if not is_diffusers_available(): |
|
raise ImportError("Diffusers should be installed in order to use the StableDiffusionTool.") |
|
if not is_vision_available(): |
|
raise ImportError("Pillow should be installed in order to use the StableDiffusionTool.") |
|
if not is_opencv_available(): |
|
raise ImportError("opencv should be installed in order to use the StableDiffusionTool.") |
|
|
|
super().__init__() |
|
|
|
if controlnet is None: |
|
controlnet = self.default_controlnet_checkpoint |
|
self.controlnet_checkpoint = controlnet |
|
|
|
if stable_diffusion is None: |
|
stable_diffusion = self.default_stable_diffusion_checkpoint |
|
self.stable_diffusion_checkpoint = stable_diffusion |
|
|
|
self.device = device |
|
self.hub_kwargs = hub_kwargs |
|
|
|
def setup(self): |
|
if self.device is None: |
|
self.device = get_default_device() |
|
|
|
self.controlnet = ControlNetModel.from_pretrained(self.controlnet_checkpoint) |
|
self.pipeline = StableDiffusionControlNetPipeline.from_pretrained( |
|
self.stable_diffusion_checkpoint, controlnet=self.controlnet |
|
) |
|
self.pipeline.scheduler = UniPCMultistepScheduler.from_config(self.pipeline.scheduler.config) |
|
|
|
self.pipeline.to(device=self.device) |
|
if self.device.type == "cuda": |
|
self.pipeline.to(torch_dtype=torch.float16) |
|
|
|
self.is_initialized = True |
|
|
|
def __call__(self, image, prompt, negative_prompt="low quality, bad quality, deformed, low resolution", added_prompt=" , highest quality, highly realistic, very high resolution"): |
|
if not self.is_initialized: |
|
self.setup() |
|
|
|
initial_prompt = "super-hero character, best quality, extremely detailed" |
|
prompt = initial_prompt + prompt |
|
|
|
low_threshold = 100 |
|
high_threshold = 200 |
|
|
|
image = np.array(image) |
|
image = cv2.Canny(image, low_threshold, high_threshold) |
|
image = image[:, :, None] |
|
image = np.concatenate([image, image, image], axis=2) |
|
canny_image = Image.fromarray(image) |
|
|
|
return self.pipeline( |
|
prompt + added_prompt, |
|
canny_image, |
|
negative_prompt=negative_prompt, |
|
num_inference_steps=25, |
|
).images[0] |
|
|