|
--- |
|
language: |
|
- en |
|
--- |
|
```python |
|
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") |
|
|
|
# 或者 # custom_pipeline="junnyu/webui_controlnet_ppdiffusers") |
|
|
|
# 加载图片 |
|
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)) |
|
|
|
# 选择sampler |
|
# Please choose in ['pndm', 'lms', 'euler', 'euler-ancestral', 'dpm-multi', 'dpm-single', 'unipc-multi', 'ddim', 'ddpm', 'deis-multi', 'heun', 'kdpm2-ancestral', 'kdpm2']! |
|
pipe.switch_scheduler('euler-ancestral') |
|
|
|
# propmpt 和 negative_prompt |
|
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)) |
|
``` |
|
|
|
![image](https://user-images.githubusercontent.com/50394665/231669030-ae9302f0-3110-4021-83a9-66a7218dfcc9.png) |