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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
Hugging Face's logo Hugging Face
|
| 3 |
+
|
| 4 |
+
Models
|
| 5 |
+
Datasets
|
| 6 |
+
Spaces
|
| 7 |
+
Docs
|
| 8 |
+
Pricing
|
| 9 |
+
Log In
|
| 10 |
+
Spaces:
|
| 11 |
+
diffusers
|
| 12 |
+
/
|
| 13 |
+
controlnet-openpose
|
| 14 |
+
App
|
| 15 |
+
Files and versions
|
| 16 |
+
Community
|
| 17 |
+
controlnet-openpose
|
| 18 |
+
/ app.py
|
| 19 |
+
patrickvonplaten's picture
|
| 20 |
+
patrickvonplaten
|
| 21 |
+
HF staff
|
| 22 |
+
Update app.py
|
| 23 |
+
4564155
|
| 24 |
+
9 days ago
|
| 25 |
+
raw
|
| 26 |
+
history
|
| 27 |
+
blame
|
| 28 |
+
contribute
|
| 29 |
+
delete
|
| 30 |
+
No virus
|
| 31 |
+
1.96 kB
|
| 32 |
+
from controlnet_aux import OpenposeDetector
|
| 33 |
+
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
|
| 34 |
+
from diffusers import UniPCMultistepScheduler
|
| 35 |
+
import gradio as gr
|
| 36 |
+
import torch
|
| 37 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 38 |
+
import os
|
| 39 |
+
import cv2
|
| 40 |
+
from PIL import Image
|
| 41 |
+
import numpy as np
|
| 42 |
+
from diffusers.utils import load_image
|
| 43 |
+
import random
|
| 44 |
+
|
| 45 |
+
# Constants
|
| 46 |
+
low_threshold = 100
|
| 47 |
+
high_threshold = 200
|
| 48 |
+
|
| 49 |
+
# Models
|
| 50 |
+
pose_model = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
|
| 51 |
+
controlnet = ControlNetModel.from_pretrained(
|
| 52 |
+
"lllyasviel/sd-controlnet-openpose", torch_dtype=torch.float16
|
| 53 |
+
)
|
| 54 |
+
pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
| 55 |
+
"runwayml/stable-diffusion-v1-5", controlnet=controlnet, safety_checker=None, torch_dtype=torch.float16
|
| 56 |
+
)
|
| 57 |
+
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
|
| 58 |
+
|
| 59 |
+
# This command loads the individual model components on GPU on-demand. So, we don't
|
| 60 |
+
# need to explicitly call pipe.to("cuda").
|
| 61 |
+
pipe.enable_model_cpu_offload()
|
| 62 |
+
|
| 63 |
+
# xformers
|
| 64 |
+
pipe.enable_xformers_memory_efficient_attention()
|
| 65 |
+
|
| 66 |
+
# Generator seed,
|
| 67 |
+
generator = torch.manual_seed(0)
|
| 68 |
+
|
| 69 |
+
def get_pose(image):
|
| 70 |
+
return pose_model(image)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def generate_an_image_from_text(text, text_size_, width, lenght):
|
| 75 |
+
# Create a blank image
|
| 76 |
+
image = Image.new('RGB', (width, lenght), color = (255, 255, 255))
|
| 77 |
+
|
| 78 |
+
# Create a drawing object
|
| 79 |
+
draw = ImageDraw.Draw(image)
|
| 80 |
+
|
| 81 |
+
# font def
|
| 82 |
+
font_dir = '/usr/share/fonts/truetype/liberation/'
|
| 83 |
+
|
| 84 |
+
# Get a list of all the font files in the directory
|
| 85 |
+
font_files = [os.path.join(font_dir, f) for f in os.listdir(font_dir) if os.path.isfile(os.path.join(font_dir, f))]
|
| 86 |
+
|
| 87 |
+
# Select a random font
|
| 88 |
+
font_path = random.choice(font_files)
|
| 89 |
+
print(font_path)
|
| 90 |
+
|
| 91 |
+
font = ImageFont.truetype(font_path, text_size_)
|
| 92 |
+
|
| 93 |
+
# Get the text size
|
| 94 |
+
text_size = draw.textsize(text, font)
|
| 95 |
+
|
| 96 |
+
# Calculate the x and y positions for the text
|
| 97 |
+
x = (image.width - text_size[0]) / 2
|
| 98 |
+
y = (image.height - text_size[1]) / 2
|
| 99 |
+
|
| 100 |
+
# Draw the text on the image
|
| 101 |
+
draw.text((x, y), text, fill=(0, 0, 0), font=font)
|
| 102 |
+
return image
|
| 103 |
+
|
| 104 |
+
def to_Canny(image):
|
| 105 |
+
# Let's load the popular vermeer image
|
| 106 |
+
image = np.array(image)
|
| 107 |
+
|
| 108 |
+
low_threshold = 100
|
| 109 |
+
high_threshold = 200
|
| 110 |
+
|
| 111 |
+
image = cv2.Canny(image, low_threshold, high_threshold)
|
| 112 |
+
image = image[:, :, None]
|
| 113 |
+
image = np.concatenate([image, image, image], axis=2)
|
| 114 |
+
canny_image = Image.fromarray(image)
|
| 115 |
+
return canny_image
|
| 116 |
+
|
| 117 |
+
def inference(prompt,canny_image,number,seed ):
|
| 118 |
+
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
|
| 119 |
+
pipe.to("cuda")
|
| 120 |
+
#pipe.enable_model_cpu_offload()
|
| 121 |
+
pipe.enable_xformers_memory_efficient_attention()
|
| 122 |
+
generator = torch.manual_seed(seed)
|
| 123 |
+
|
| 124 |
+
image_ = canny_image
|
| 125 |
+
prompt = prompt
|
| 126 |
+
out_image = pipe(
|
| 127 |
+
prompt, num_inference_steps=20, generator=generator, image=image_, num_images_per_prompt=number)
|
| 128 |
+
return out_image
|
| 129 |
+
|
| 130 |
+
def generate_images(image, prompt):
|
| 131 |
+
pose = get_pose(image)
|
| 132 |
+
output = pipe(
|
| 133 |
+
prompt,
|
| 134 |
+
pose,
|
| 135 |
+
generator=generator,
|
| 136 |
+
num_images_per_prompt=3,
|
| 137 |
+
num_inference_steps=20,
|
| 138 |
+
)
|
| 139 |
+
all_outputs = []
|
| 140 |
+
all_outputs.append(pose)
|
| 141 |
+
for image in output.images:
|
| 142 |
+
all_outputs.append(image)
|
| 143 |
+
return all_outputs
|
| 144 |
+
|
| 145 |
+
def generation(prompt,text,seed,police_size, lenght, width,number):
|
| 146 |
+
img = generate_an_image_from_text(text,police_size,lenght,width)
|
| 147 |
+
img = to_Canny(img)
|
| 148 |
+
output = inference(prompt,img, number,seed)
|
| 149 |
+
all_outputs = []
|
| 150 |
+
for image in output.images:
|
| 151 |
+
all_outputs.append(image)
|
| 152 |
+
return all_outputs
|
| 153 |
+
|
| 154 |
+
gr.Interface(fn=generation,
|
| 155 |
+
inputs=["text",
|
| 156 |
+
"text",
|
| 157 |
+
gr.Slider(0, 200),
|
| 158 |
+
gr.Slider(0, 200),
|
| 159 |
+
gr.Slider(0, 1024),
|
| 160 |
+
gr.Slider(0, 1024),
|
| 161 |
+
gr.Slider(0, 7)],
|
| 162 |
+
outputs=gr.Gallery().style(grid=[2], height="auto")),
|
| 163 |
+
title="Generate a logo using Text ",
|
| 164 |
+
examples=[["A steampunk Alphabetic Logo, steampunk style, with glowing mecha parts, mecha alphabets, high quality, high res, ultra HD", "Logo",60,90,512,512,2]],
|
| 165 |
+
).launch(enable_queue=True)
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
|