#!/usr/bin/env python3 import torch import os from huggingface_hub import HfApi from pathlib import Path from diffusers.utils import load_image import cv2 from PIL import Image import numpy as np from diffusers import ( ControlNetModel, StableDiffusionControlNetImg2ImgPipeline, StableDiffusionControlNetInpaintPipeline, DiffusionPipeline, UniPCMultistepScheduler, ) import sys checkpoint = sys.argv[1] # image = load_image( # "https://huggingface.co./lllyasviel/sd-controlnet-canny/resolve/main/images/bird.png" # ) img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png" mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png" image = load_image(img_url).resize((512, 512)) mask_image = load_image(mask_url).resize((512, 512)) np_image = np.array(image) low_threshold = 100 high_threshold = 200 np_image = cv2.Canny(np_image, low_threshold, high_threshold) np_image = np_image[:, :, None] np_image = np.concatenate([np_image, np_image, np_image], axis=2) canny_image = Image.fromarray(np_image) controlnet = ControlNetModel.from_pretrained(checkpoint, torch_dtype=torch.float16) # pipe = DiffusionPipeline.from_pretrained( # "runwayml/stable-diffusion-inpainting", controlnet=controlnet, torch_dtype=torch.float16, custom_pipeline="stable_diffusion_controlnet_inpaint" # ) pipe = StableDiffusionControlNetInpaintPipeline.from_pretrained( "runwayml/stable-diffusion-inpainting", controlnet=controlnet, torch_dtype=torch.float16, ) pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config) pipe.enable_model_cpu_offload() generator = torch.manual_seed(0) text_prompt="a blue dog" # out_image = pipe("A blue dog", num_inference_steps=50, generator=generator, image=image, mask_image=mask_image, controlnet_conditioning_image=canny_image).images[0] out_image = pipe( text_prompt, num_inference_steps=20, generator=generator, image=image, mask_image=mask_image, control_image=canny_image, ).images[0] path = os.path.join(Path.home(), "images", "aa.png") out_image.save(path) api = HfApi() api.upload_file( path_or_fileobj=path, path_in_repo=path.split("/")[-1], repo_id="patrickvonplaten/images", repo_type="dataset", ) print("https://huggingface.co./datasets/patrickvonplaten/images/blob/main/aa.png")