import paddle
from ppdiffusers import DiffusionPipeline, ControlNetModel
from ppdiffusers.utils import load_image, image_grid
import numpy as np
from PIL import Image
import cv2
class CannyDetector:
def __call__(self, img, low_threshold, high_threshold):
return cv2.Canny(img, low_threshold, high_threshold)
apply_canny = CannyDetector()
controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", paddle_dtype=paddle.float16)
pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5",
controlnet=controlnet,
safety_checker=None,
feature_extractor=None,
requires_safety_checker=False,
paddle_dtype=paddle.float16,
custom_pipeline="webui_stable_diffusion_controlnet",
custom_revision="9aa0fcae034d99a796c3077ec6fea84808fc5875")
raw_image = load_image("https://paddlenlp.bj.bcebos.com/models/community/junnyu/develop/control_bird_canny_demo.png")
canny_image = Image.fromarray(apply_canny(np.array(raw_image), low_threshold=100, high_threshold=200))
pipe.switch_scheduler('euler-ancestral')
prompt = "a (blue:1.5) bird"
negative_prompt = ""
num = 4
clip_skip = 2
controlnet_conditioning_scale = 1.
num_inference_steps = 50
all_images = []
print("raw_image vs canny_image")
display(image_grid([raw_image, canny_image], 1, 2))
for i in range(num):
img = pipe(
prompt=prompt,
negative_prompt = negative_prompt,
image=canny_image,
num_inference_steps=num_inference_steps,
controlnet_conditioning_scale=controlnet_conditioning_scale,
clip_skip= clip_skip,
).images[0]
all_images.append(img)
display(image_grid(all_images, 1, num))