CoolFace
Modelpublic

diffusers/tools

sourceHugging Facecreativeml-openrail-mupdated 3y agoView on Hugging Face
11likes28downloads
control_net_depth.py55 linesDownload Raw Back to root
1#!/usr/bin/env python32import torch3import os4from huggingface_hub import HfApi5from pathlib import Path6from diffusers.utils import load_image7from PIL import Image8import numpy as np9from transformers import pipeline10 11from diffusers import (12    ControlNetModel,13    StableDiffusionControlNetPipeline,14    UniPCMultistepScheduler,15)16import sys17 18checkpoint = sys.argv[1]19 20image = load_image("https://huggingface.co/lllyasviel/sd-controlnet-depth/resolve/main/images/stormtrooper.png")21 22prompt = "Stormtrooper's lecture in beautiful lecture hall"23 24 25depth_estimator = pipeline('depth-estimation')26image = depth_estimator(image)['depth']27image = np.array(image)28image = image[:, :, None]29image = np.concatenate([image, image, image], axis=2)30image = Image.fromarray(image)31 32controlnet = ControlNetModel.from_pretrained(checkpoint, torch_dtype=torch.float16)33pipe = StableDiffusionControlNetPipeline.from_pretrained(34    "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float1635)36 37pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)38pipe.enable_model_cpu_offload()39 40generator = torch.manual_seed(0)41out_image = pipe(prompt, num_inference_steps=40, generator=generator, image=image).images[0]42 43path = os.path.join(Path.home(), "images", "aa.png")44out_image.save(path)45 46api = HfApi()47 48api.upload_file(49    path_or_fileobj=path,50    path_in_repo=path.split("/")[-1],51    repo_id="patrickvonplaten/images",52    repo_type="dataset",53)54print("https://huggingface.co/datasets/patrickvonplaten/images/blob/main/aa.png")55