Spaces:
Running
on
Zero
Running
on
Zero
Fabrice-TIERCELIN
commited on
Commit
•
3816ea0
1
Parent(s):
4d128e4
New version, extremely detailed, not blur, fine options
Browse filesHi,
I suggest this version that is already running on [this space](https://huggingface.co/spaces/Fabrice-TIERCELIN/Uncrop). I have adapted the title, the description and the example. The size can be set pixel per pixel.
Fabrice
app.py
CHANGED
@@ -1,307 +1,482 @@
|
|
1 |
-
import cv2
|
2 |
-
import numpy as np
|
3 |
-
import torch
|
4 |
import gradio as gr
|
|
|
|
|
|
|
5 |
import random
|
6 |
-
import
|
7 |
-
|
8 |
-
from diffusers import DPMSolverMultistepScheduler, StableDiffusionXLPipeline
|
9 |
-
from diffusers.utils import load_image
|
10 |
-
|
11 |
-
DESCRIPTION='''
|
12 |
-
This uses code lifted almost verbatim from
|
13 |
-
[Outpainting II - Differential Diffusion](https://huggingface.co/blog/OzzyGT/outpainting-differential-diffusion). This only works well on blurry edges.
|
14 |
-
'''
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
and licensed under CC-BY-SA 4.0 International.
|
19 |
-
'''
|
20 |
|
21 |
-
|
22 |
-
'custom_pipeline': 'pipeline_stable_diffusion_xl_differential_img2img'
|
23 |
-
}
|
24 |
|
25 |
if torch.cuda.is_available():
|
26 |
-
device =
|
27 |
-
|
28 |
-
|
29 |
else:
|
30 |
-
device =
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
inpaint_mask, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
|
158 |
-
inpaint = cv2.inpaint(new_image, mask_np, 3, cv2.INPAINT_TELEA)
|
159 |
-
|
160 |
-
# convert image to tensor
|
161 |
-
inpaint = cv2.cvtColor(inpaint, cv2.COLOR_BGR2RGB)
|
162 |
-
inpaint = torch.from_numpy(inpaint).permute(2, 0, 1).float()
|
163 |
-
inpaint = inpaint / 127.5 - 1
|
164 |
-
inpaint = inpaint.unsqueeze(0).to(device)
|
165 |
-
|
166 |
-
# convert mask to tensor
|
167 |
-
mask = torch.from_numpy(mask)
|
168 |
-
mask = mask.unsqueeze(0).float() / 255.0
|
169 |
-
mask = mask.to(device)
|
170 |
-
|
171 |
-
return inpaint, mask
|
172 |
-
|
173 |
-
|
174 |
-
def image_resize(image, new_size=1024):
|
175 |
-
height, width = image.shape[:2]
|
176 |
-
|
177 |
-
aspect_ratio = width / height
|
178 |
-
new_width = new_size
|
179 |
-
new_height = new_size
|
180 |
-
|
181 |
-
if aspect_ratio != 1:
|
182 |
-
if width > height:
|
183 |
-
new_height = int(new_size / aspect_ratio)
|
184 |
-
else:
|
185 |
-
new_width = int(new_size * aspect_ratio)
|
186 |
|
187 |
-
|
188 |
-
|
189 |
|
190 |
-
|
|
|
191 |
|
|
|
|
|
192 |
|
193 |
-
|
194 |
-
|
195 |
-
if torch.cuda.is_available():
|
196 |
-
torch.cuda.empty_cache()
|
197 |
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
).to(device)
|
202 |
-
pipeline.scheduler = DPMSolverMultistepScheduler.from_config(
|
203 |
-
pipeline.scheduler.config, use_karras_sigmas=True)
|
204 |
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
],
|
211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
212 |
)
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
)
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
image, mask = process_image(
|
253 |
-
image, expand_pixels=expand_pixels_to_square, direction=direction, inpaint_mask_color=inpaint_mask_color, blur_radius=blur_radius
|
254 |
)
|
255 |
|
256 |
-
|
257 |
-
for index, part in enumerate(slice_image(original)):
|
258 |
-
ip_adapter_image.append(part)
|
259 |
-
|
260 |
-
generated = generate_image(
|
261 |
-
prompt, negative_prompt, image, mask, ip_adapter_image)
|
262 |
-
final_image = generated
|
263 |
-
|
264 |
-
for i in range(times_to_expand):
|
265 |
-
image, mask = process_image(
|
266 |
-
final_image, direction=direction, expand_pixels=expand_pixels, inpaint_mask_color=inpaint_mask_color, blur_radius=blur_radius
|
267 |
-
)
|
268 |
-
|
269 |
-
ip_adapter_image = []
|
270 |
-
for index, part in enumerate(slice_image(generated)):
|
271 |
-
ip_adapter_image.append(part)
|
272 |
-
|
273 |
-
generated = generate_image(
|
274 |
-
prompt, negative_prompt, image, mask, ip_adapter_image)
|
275 |
-
final_image = merge_images(final_image, generated, 256, direction)
|
276 |
-
|
277 |
-
color_converted = cv2.cvtColor(final_image, cv2.COLOR_BGR2RGB)
|
278 |
-
return color_converted
|
279 |
-
|
280 |
-
example_image=load_image('examples/Coucang.jpg')
|
281 |
-
|
282 |
-
gradio_app = gr.Interface(
|
283 |
-
outpaint,
|
284 |
-
inputs=[
|
285 |
-
gr.Image(label="Select start image", sources=[
|
286 |
-
'upload', 'clipboard'], type='pil'),
|
287 |
-
gr.Radio(["left", "right", "top", 'bottom'], label="Direction",
|
288 |
-
info="Outward from which edge to paint?", value='right'),
|
289 |
-
gr.Slider(2, 4, step=1, value=4, label="Times to expand",
|
290 |
-
info="Choose between 2 and 4"),
|
291 |
-
gr.Slider(1, 12, step=0.1, value=4, label="Guidance scale",
|
292 |
-
info="Choose between 1 and 12"),
|
293 |
-
gr.Slider(250, 500, step=1, value=500, label="Mask blur radius",
|
294 |
-
info="Choose between 250 and 500"),
|
295 |
-
],
|
296 |
-
outputs=[gr.Image(label="Processed Image")],
|
297 |
-
examples=[
|
298 |
-
[example_image, 'right', 4, 5, 500],
|
299 |
-
[example_image, 'left', 4, 6, 500],
|
300 |
-
],
|
301 |
-
title="Outpainting with differential diffusion demo",
|
302 |
-
description=DESCRIPTION,
|
303 |
-
article=ARTICLE
|
304 |
-
)
|
305 |
-
|
306 |
-
if __name__ == "__main__":
|
307 |
-
gradio_app.queue(max_size=20).launch()
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import time
|
4 |
+
import math
|
5 |
import random
|
6 |
+
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
from diffusers import StableDiffusionXLInpaintPipeline
|
9 |
+
from PIL import Image, ImageFilter
|
|
|
|
|
10 |
|
11 |
+
max_64_bit_int = 2**63 - 1
|
|
|
|
|
12 |
|
13 |
if torch.cuda.is_available():
|
14 |
+
device = "cuda"
|
15 |
+
floatType = torch.float16
|
16 |
+
variant = "fp16"
|
17 |
else:
|
18 |
+
device = "cpu"
|
19 |
+
floatType = torch.float32
|
20 |
+
variant = None
|
21 |
+
|
22 |
+
pipe = StableDiffusionXLInpaintPipeline.from_pretrained("diffusers/stable-diffusion-xl-1.0-inpainting-0.1", torch_dtype = floatType, variant = variant)
|
23 |
+
pipe = pipe.to(device)
|
24 |
+
|
25 |
+
def update_seed(is_randomize_seed, seed):
|
26 |
+
if is_randomize_seed:
|
27 |
+
return random.randint(0, max_64_bit_int)
|
28 |
+
return seed
|
29 |
+
|
30 |
+
def toggle_debug(is_debug_mode):
|
31 |
+
return [gr.update(visible = is_debug_mode)] * 3
|
32 |
+
|
33 |
+
def noise_color(color, noise):
|
34 |
+
return color + random.randint(- noise, noise)
|
35 |
+
|
36 |
+
def check(
|
37 |
+
input_image,
|
38 |
+
enlarge_top,
|
39 |
+
enlarge_right,
|
40 |
+
enlarge_bottom,
|
41 |
+
enlarge_left,
|
42 |
+
prompt,
|
43 |
+
negative_prompt,
|
44 |
+
smooth_border,
|
45 |
+
num_inference_steps,
|
46 |
+
guidance_scale,
|
47 |
+
image_guidance_scale,
|
48 |
+
strength,
|
49 |
+
denoising_steps,
|
50 |
+
is_randomize_seed,
|
51 |
+
seed,
|
52 |
+
debug_mode,
|
53 |
+
progress = gr.Progress()):
|
54 |
+
if input_image is None:
|
55 |
+
raise gr.Error("Please provide an image.")
|
56 |
+
|
57 |
+
if prompt is None or prompt == "":
|
58 |
+
raise gr.Error("Please provide a prompt input.")
|
59 |
+
|
60 |
+
if (not (enlarge_top is None)) and enlarge_top < 0:
|
61 |
+
raise gr.Error("Please provide positive top margin.")
|
62 |
+
|
63 |
+
if (not (enlarge_right is None)) and enlarge_right < 0:
|
64 |
+
raise gr.Error("Please provide positive right margin.")
|
65 |
+
|
66 |
+
if (not (enlarge_bottom is None)) and enlarge_bottom < 0:
|
67 |
+
raise gr.Error("Please provide positive bottom margin.")
|
68 |
+
|
69 |
+
if (not (enlarge_left is None)) and enlarge_left < 0:
|
70 |
+
raise gr.Error("Please provide positive left margin.")
|
71 |
+
|
72 |
+
if (
|
73 |
+
(enlarge_top is None or enlarge_top == 0)
|
74 |
+
and (enlarge_right is None or enlarge_right == 0)
|
75 |
+
and (enlarge_bottom is None or enlarge_bottom == 0)
|
76 |
+
and (enlarge_left is None or enlarge_left == 0)
|
77 |
+
):
|
78 |
+
raise gr.Error("At least one border must be enlarged.")
|
79 |
+
|
80 |
+
def uncrop(
|
81 |
+
input_image,
|
82 |
+
enlarge_top,
|
83 |
+
enlarge_right,
|
84 |
+
enlarge_bottom,
|
85 |
+
enlarge_left,
|
86 |
+
prompt,
|
87 |
+
negative_prompt,
|
88 |
+
smooth_border,
|
89 |
+
num_inference_steps,
|
90 |
+
guidance_scale,
|
91 |
+
image_guidance_scale,
|
92 |
+
strength,
|
93 |
+
denoising_steps,
|
94 |
+
is_randomize_seed,
|
95 |
+
seed,
|
96 |
+
debug_mode,
|
97 |
+
progress = gr.Progress()):
|
98 |
+
check(
|
99 |
+
input_image,
|
100 |
+
enlarge_top,
|
101 |
+
enlarge_right,
|
102 |
+
enlarge_bottom,
|
103 |
+
enlarge_left,
|
104 |
+
prompt,
|
105 |
+
negative_prompt,
|
106 |
+
smooth_border,
|
107 |
+
num_inference_steps,
|
108 |
+
guidance_scale,
|
109 |
+
image_guidance_scale,
|
110 |
+
strength,
|
111 |
+
denoising_steps,
|
112 |
+
is_randomize_seed,
|
113 |
+
seed,
|
114 |
+
debug_mode
|
115 |
+
)
|
116 |
+
start = time.time()
|
117 |
+
progress(0, desc = "Preparing data...")
|
118 |
+
|
119 |
+
if enlarge_top is None or enlarge_top == "":
|
120 |
+
enlarge_top = 0
|
121 |
+
|
122 |
+
if enlarge_right is None or enlarge_right == "":
|
123 |
+
enlarge_right = 0
|
124 |
+
|
125 |
+
if enlarge_bottom is None or enlarge_bottom == "":
|
126 |
+
enlarge_bottom = 0
|
127 |
+
|
128 |
+
if enlarge_left is None or enlarge_left == "":
|
129 |
+
enlarge_left = 0
|
130 |
+
|
131 |
+
if negative_prompt is None:
|
132 |
+
negative_prompt = ""
|
133 |
+
|
134 |
+
if smooth_border is None:
|
135 |
+
smooth_border = 0
|
136 |
+
|
137 |
+
if num_inference_steps is None:
|
138 |
+
num_inference_steps = 50
|
139 |
+
|
140 |
+
if guidance_scale is None:
|
141 |
+
guidance_scale = 7
|
142 |
+
|
143 |
+
if image_guidance_scale is None:
|
144 |
+
image_guidance_scale = 1.5
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
|
146 |
+
if strength is None:
|
147 |
+
strength = 0.99
|
148 |
|
149 |
+
if denoising_steps is None:
|
150 |
+
denoising_steps = 1000
|
151 |
|
152 |
+
if seed is None:
|
153 |
+
seed = random.randint(0, max_64_bit_int)
|
154 |
|
155 |
+
random.seed(seed)
|
156 |
+
torch.manual_seed(seed)
|
|
|
|
|
157 |
|
158 |
+
original_height, original_width, original_channel = np.array(input_image).shape
|
159 |
+
output_width = enlarge_left + original_width + enlarge_right
|
160 |
+
output_height = enlarge_top + original_height + enlarge_bottom
|
|
|
|
|
|
|
161 |
|
162 |
+
# Enlarged image
|
163 |
+
enlarged_image = Image.new(mode = input_image.mode, size = (original_width, original_height), color = "black")
|
164 |
+
enlarged_image.paste(input_image, (0, 0))
|
165 |
+
enlarged_image = enlarged_image.resize((output_width, output_height))
|
166 |
+
enlarged_image = enlarged_image.filter(ImageFilter.BoxBlur(20))
|
167 |
+
|
168 |
+
enlarged_image.paste(input_image, (enlarge_left, enlarge_top))
|
169 |
+
|
170 |
+
horizontally_mirrored_input_image = input_image.transpose(Image.FLIP_LEFT_RIGHT).resize((original_width * 2, original_height))
|
171 |
+
enlarged_image.paste(horizontally_mirrored_input_image, (enlarge_left - (original_width * 2), enlarge_top))
|
172 |
+
enlarged_image.paste(horizontally_mirrored_input_image, (enlarge_left + original_width, enlarge_top))
|
173 |
+
|
174 |
+
vertically_mirrored_input_image = input_image.transpose(Image.FLIP_TOP_BOTTOM).resize((original_width, original_height * 2))
|
175 |
+
enlarged_image.paste(vertically_mirrored_input_image, (enlarge_left, enlarge_top - (original_height * 2)))
|
176 |
+
enlarged_image.paste(vertically_mirrored_input_image, (enlarge_left, enlarge_top + original_height))
|
177 |
+
|
178 |
+
returned_input_image = input_image.transpose(Image.ROTATE_180).resize((original_width * 2, original_height * 2))
|
179 |
+
enlarged_image.paste(returned_input_image, (enlarge_left - (original_width * 2), enlarge_top - (original_height * 2)))
|
180 |
+
enlarged_image.paste(returned_input_image, (enlarge_left - (original_width * 2), enlarge_top + original_height))
|
181 |
+
enlarged_image.paste(returned_input_image, (enlarge_left + original_width, enlarge_top - (original_height * 2)))
|
182 |
+
enlarged_image.paste(returned_input_image, (enlarge_left + original_width, enlarge_top + original_height))
|
183 |
+
|
184 |
+
enlarged_image = enlarged_image.filter(ImageFilter.BoxBlur(20))
|
185 |
+
|
186 |
+
# Noise image
|
187 |
+
noise_image = Image.new(mode = input_image.mode, size = (output_width, output_height), color = "black")
|
188 |
+
enlarged_pixels = enlarged_image.load()
|
189 |
+
|
190 |
+
for i in range(output_width):
|
191 |
+
for j in range(output_height):
|
192 |
+
enlarged_pixel = enlarged_pixels[i, j]
|
193 |
+
noise = min(max(enlarge_left - i, i - (enlarge_left + original_width), enlarge_top - j, j - (enlarge_top + original_height), 0), 255)
|
194 |
+
noise_image.putpixel((i, j), (noise_color(enlarged_pixel[0], noise), noise_color(enlarged_pixel[1], noise), noise_color(enlarged_pixel[2], noise), 255))
|
195 |
+
|
196 |
+
enlarged_image.paste(noise_image, (0, 0))
|
197 |
+
enlarged_image.paste(input_image, (enlarge_left, enlarge_top))
|
198 |
+
|
199 |
+
# Mask
|
200 |
+
mask_image = Image.new(mode = input_image.mode, size = (output_width, output_height), color = (255, 255, 255, 0))
|
201 |
+
black_mask = Image.new(mode = input_image.mode, size = (original_width - smooth_border, original_height - smooth_border), color = (0, 0, 0, 0))
|
202 |
+
mask_image.paste(black_mask, (enlarge_left + (smooth_border // 2), enlarge_top + (smooth_border // 2)))
|
203 |
+
mask_image = mask_image.filter(ImageFilter.BoxBlur((smooth_border // 2)))
|
204 |
+
|
205 |
+
# Limited to 1 million pixels
|
206 |
+
if 1024 * 1024 < output_width * output_height:
|
207 |
+
factor = ((1024 * 1024) / (output_width * output_height))**0.5
|
208 |
+
process_width = math.floor(output_width * factor)
|
209 |
+
process_height = math.floor(output_height * factor)
|
210 |
+
|
211 |
+
limitation = " Due to technical limitation, the image have been downscaled and then upscaled.";
|
212 |
+
else:
|
213 |
+
process_width = output_width
|
214 |
+
process_height = output_height
|
215 |
+
|
216 |
+
limitation = "";
|
217 |
+
|
218 |
+
# Width and height must be multiple of 8
|
219 |
+
if (process_width % 8) != 0 or (process_height % 8) != 0:
|
220 |
+
if ((process_width - (process_width % 8) + 8) * (process_height - (process_height % 8) + 8)) <= (1024 * 1024):
|
221 |
+
process_width = process_width - (process_width % 8) + 8
|
222 |
+
process_height = process_height - (process_height % 8) + 8
|
223 |
+
elif (process_height % 8) <= (process_width % 8) and ((process_width - (process_width % 8) + 8) * process_height) <= (1024 * 1024):
|
224 |
+
process_width = process_width - (process_width % 8) + 8
|
225 |
+
process_height = process_height - (process_height % 8)
|
226 |
+
elif (process_width % 8) <= (process_height % 8) and (process_width * (process_height - (process_height % 8) + 8)) <= (1024 * 1024):
|
227 |
+
process_width = process_width - (process_width % 8)
|
228 |
+
process_height = process_height - (process_height % 8) + 8
|
229 |
+
else:
|
230 |
+
process_width = process_width - (process_width % 8)
|
231 |
+
process_height = process_height - (process_height % 8)
|
232 |
+
|
233 |
+
progress(None, desc = "Processing...")
|
234 |
+
|
235 |
+
output_image = pipe(
|
236 |
+
seeds = [seed],
|
237 |
+
width = process_width,
|
238 |
+
height = process_height,
|
239 |
+
prompt = prompt,
|
240 |
+
negative_prompt = negative_prompt,
|
241 |
+
image = enlarged_image,
|
242 |
+
mask_image = mask_image,
|
243 |
+
num_inference_steps = num_inference_steps,
|
244 |
+
guidance_scale = guidance_scale,
|
245 |
+
image_guidance_scale = image_guidance_scale,
|
246 |
+
strength = strength,
|
247 |
+
denoising_steps = denoising_steps,
|
248 |
+
show_progress_bar = True
|
249 |
+
).images[0]
|
250 |
+
|
251 |
+
if limitation != "":
|
252 |
+
output_image = output_image.resize((output_width, output_height))
|
253 |
+
|
254 |
+
if debug_mode == False:
|
255 |
+
input_image = None
|
256 |
+
enlarged_image = None
|
257 |
+
mask_image = None
|
258 |
+
|
259 |
+
end = time.time()
|
260 |
+
secondes = int(end - start)
|
261 |
+
minutes = math.floor(secondes / 60)
|
262 |
+
secondes = secondes - (minutes * 60)
|
263 |
+
hours = math.floor(minutes / 60)
|
264 |
+
minutes = minutes - (hours * 60)
|
265 |
+
return [
|
266 |
+
output_image,
|
267 |
+
("Start again to get a different result. " if is_randomize_seed else "") + "The new image is " + str(output_width) + " pixels large and " + str(output_height) + " pixels high, so an image of " + f'{output_width * output_height:,}' + " pixels. The image has been generated in " + ((str(hours) + " h, ") if hours != 0 else "") + ((str(minutes) + " min, ") if hours != 0 or minutes != 0 else "") + str(secondes) + " sec." + limitation,
|
268 |
+
input_image,
|
269 |
+
enlarged_image,
|
270 |
+
mask_image
|
271 |
+
]
|
272 |
+
|
273 |
+
with gr.Blocks() as interface:
|
274 |
+
gr.HTML(
|
275 |
+
"""
|
276 |
+
<h1 style="text-align: center;">Outpainting demo</h1>
|
277 |
+
<p style="text-align: center;">Enlarges the point of view of your image, freely, without account, without watermark, without installation, which can be downloaded</p>
|
278 |
+
<br/>
|
279 |
+
<br/>
|
280 |
+
✨ Powered by <i>SDXL 1.0</i> artificial intellingence.
|
281 |
+
<br/>
|
282 |
+
💻 Your computer must <u>not</u> enter into standby mode.<br/>You can duplicate this space on a free account, it works on CPU and CUDA.<br/>
|
283 |
+
<a href='https://huggingface.co/spaces/clinteroni/outpainting-with-differential-diffusion-demo?duplicate=true'><img src='https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14'></a>
|
284 |
+
<br/>
|
285 |
+
⚖️ You can use, modify and share the generated images but not for commercial uses.
|
286 |
+
|
287 |
+
"""
|
288 |
+
)
|
289 |
+
with gr.Row():
|
290 |
+
with gr.Column():
|
291 |
+
dummy_1 = gr.Label(visible = False)
|
292 |
+
with gr.Column():
|
293 |
+
enlarge_top = gr.Number(minimum = 0, value = 64, precision = 0, label = "Uncrop on top ⬆️", info = "in pixels")
|
294 |
+
with gr.Column():
|
295 |
+
dummy_2 = gr.Label(visible = False)
|
296 |
+
with gr.Row():
|
297 |
+
with gr.Column():
|
298 |
+
enlarge_left = gr.Number(minimum = 0, value = 64, precision = 0, label = "Uncrop on left ⬅️", info = "in pixels")
|
299 |
+
with gr.Column():
|
300 |
+
input_image = gr.Image(label = "Your image", sources = ["upload", "webcam", "clipboard"], type = "pil")
|
301 |
+
with gr.Column():
|
302 |
+
enlarge_right = gr.Number(minimum = 0, value = 64, precision = 0, label = "Uncrop on right ➡️", info = "in pixels")
|
303 |
+
with gr.Row():
|
304 |
+
with gr.Column():
|
305 |
+
dummy_3 = gr.Label(visible = False)
|
306 |
+
with gr.Column():
|
307 |
+
enlarge_bottom = gr.Number(minimum = 0, value = 64, precision = 0, label = "Uncrop on bottom ⬇️", info = "in pixels")
|
308 |
+
with gr.Column():
|
309 |
+
dummy_4 = gr.Label(visible = False)
|
310 |
+
with gr.Row():
|
311 |
+
prompt = gr.Textbox(label = "Prompt", info = "Describe the subject, the background and the style of image; 77 token limit", placeholder = "Describe what you want to see in the entire image", lines = 2)
|
312 |
+
with gr.Row():
|
313 |
+
with gr.Accordion("Advanced options", open = False):
|
314 |
+
negative_prompt = gr.Textbox(label = "Negative prompt", placeholder = "Describe what you do NOT want to see in the entire image", value = 'Border, frame, painting, scribbling, smear, noise, blur, watermark')
|
315 |
+
smooth_border = gr.Slider(minimum = 0, maximum = 1024, value = 0, step = 2, label = "Smooth border", info = "lower=preserve original, higher=seamless")
|
316 |
+
num_inference_steps = gr.Slider(minimum = 10, maximum = 100, value = 50, step = 1, label = "Number of inference steps", info = "lower=faster, higher=image quality")
|
317 |
+
guidance_scale = gr.Slider(minimum = 1, maximum = 13, value = 7, step = 0.1, label = "Classifier-Free Guidance Scale", info = "lower=image quality, higher=follow the prompt")
|
318 |
+
image_guidance_scale = gr.Slider(minimum = 1, value = 1.5, step = 0.1, label = "Image Guidance Scale", info = "lower=image quality, higher=follow the image")
|
319 |
+
strength = gr.Slider(value = 0.99, minimum = 0.01, maximum = 1.0, step = 0.01, label = "Strength", info = "lower=follow the original area (discouraged), higher=redraw from scratch")
|
320 |
+
denoising_steps = gr.Number(minimum = 0, value = 1000, step = 1, label = "Denoising", info = "lower=irrelevant result, higher=relevant result")
|
321 |
+
randomize_seed = gr.Checkbox(label = "\U0001F3B2 Randomize seed", value = True, info = "If checked, result is always different")
|
322 |
+
seed = gr.Slider(minimum = 0, maximum = max_64_bit_int, step = 1, randomize = True, label = "Seed")
|
323 |
+
debug_mode = gr.Checkbox(label = "Debug mode", value = False, info = "Show intermediate results")
|
324 |
+
|
325 |
+
with gr.Row():
|
326 |
+
submit = gr.Button("🚀 Outpaint", variant = "primary")
|
327 |
+
|
328 |
+
with gr.Row():
|
329 |
+
uncropped_image = gr.Image(label = "Outpainted image")
|
330 |
+
with gr.Row():
|
331 |
+
information = gr.HTML()
|
332 |
+
with gr.Row():
|
333 |
+
original_image = gr.Image(label = "Original image", visible = False)
|
334 |
+
with gr.Row():
|
335 |
+
enlarged_image = gr.Image(label = "Enlarged image", visible = False)
|
336 |
+
with gr.Row():
|
337 |
+
mask_image = gr.Image(label = "Mask image", visible = False)
|
338 |
+
|
339 |
+
submit.click(fn = update_seed, inputs = [
|
340 |
+
randomize_seed,
|
341 |
+
seed
|
342 |
+
], outputs = [
|
343 |
+
seed
|
344 |
+
], queue = False, show_progress = False).then(toggle_debug, debug_mode, [
|
345 |
+
original_image,
|
346 |
+
enlarged_image,
|
347 |
+
mask_image
|
348 |
+
], queue = False, show_progress = False).then(check, inputs = [
|
349 |
+
input_image,
|
350 |
+
enlarge_top,
|
351 |
+
enlarge_right,
|
352 |
+
enlarge_bottom,
|
353 |
+
enlarge_left,
|
354 |
+
prompt,
|
355 |
+
negative_prompt,
|
356 |
+
smooth_border,
|
357 |
+
num_inference_steps,
|
358 |
+
guidance_scale,
|
359 |
+
image_guidance_scale,
|
360 |
+
strength,
|
361 |
+
denoising_steps,
|
362 |
+
randomize_seed,
|
363 |
+
seed,
|
364 |
+
debug_mode
|
365 |
+
], outputs = [], queue = False,
|
366 |
+
show_progress = False).success(uncrop, inputs = [
|
367 |
+
input_image,
|
368 |
+
enlarge_top,
|
369 |
+
enlarge_right,
|
370 |
+
enlarge_bottom,
|
371 |
+
enlarge_left,
|
372 |
+
prompt,
|
373 |
+
negative_prompt,
|
374 |
+
smooth_border,
|
375 |
+
num_inference_steps,
|
376 |
+
guidance_scale,
|
377 |
+
image_guidance_scale,
|
378 |
+
strength,
|
379 |
+
denoising_steps,
|
380 |
+
randomize_seed,
|
381 |
+
seed,
|
382 |
+
debug_mode
|
383 |
+
], outputs = [
|
384 |
+
uncropped_image,
|
385 |
+
information,
|
386 |
+
original_image,
|
387 |
+
enlarged_image,
|
388 |
+
mask_image
|
389 |
+
], scroll_to_output = True)
|
390 |
+
|
391 |
+
gr.Examples(
|
392 |
+
run_on_click = True,
|
393 |
+
fn = uncrop,
|
394 |
+
inputs = [
|
395 |
+
input_image,
|
396 |
+
enlarge_top,
|
397 |
+
enlarge_right,
|
398 |
+
enlarge_bottom,
|
399 |
+
enlarge_left,
|
400 |
+
prompt,
|
401 |
+
negative_prompt,
|
402 |
+
smooth_border,
|
403 |
+
num_inference_steps,
|
404 |
+
guidance_scale,
|
405 |
+
image_guidance_scale,
|
406 |
+
strength,
|
407 |
+
denoising_steps,
|
408 |
+
randomize_seed,
|
409 |
+
seed,
|
410 |
+
debug_mode
|
411 |
+
],
|
412 |
+
outputs = [
|
413 |
+
uncropped_image,
|
414 |
+
information,
|
415 |
+
original_image,
|
416 |
+
enlarged_image,
|
417 |
+
mask_image
|
418 |
],
|
419 |
+
examples = [
|
420 |
+
[
|
421 |
+
"./examples/Coucang.jpg",
|
422 |
+
1024,
|
423 |
+
1024,
|
424 |
+
1024,
|
425 |
+
1024,
|
426 |
+
"A white Coucang, in a tree, ultrarealistic, realistic, photorealistic, 8k, bokeh",
|
427 |
+
"Border, frame, painting, drawing, cartoon, anime, 3d, scribbling, smear, noise, blur, watermark",
|
428 |
+
0,
|
429 |
+
50,
|
430 |
+
7,
|
431 |
+
1.5,
|
432 |
+
0.99,
|
433 |
+
1000,
|
434 |
+
False,
|
435 |
+
123,
|
436 |
+
False
|
437 |
+
],
|
438 |
+
],
|
439 |
+
cache_examples = False,
|
440 |
)
|
441 |
+
|
442 |
+
gr.Markdown(
|
443 |
+
"""
|
444 |
+
## How to prompt your image
|
445 |
+
|
446 |
+
To easily read your prompt, start with the subject, then describ the pose or action, then secondary elements, then the background, then the graphical style, then the image quality:
|
447 |
+
```
|
448 |
+
A Vietnamese woman, red clothes, walking, smilling, in the street, a car on the left, in a modern city, photorealistic, 8k
|
449 |
+
```
|
450 |
+
|
451 |
+
You can use round brackets to increase the importance of a part:
|
452 |
+
```
|
453 |
+
A Vietnamese woman, (red clothes), walking, smilling, in the street, a car on the left, in a modern city, photorealistic, 8k
|
454 |
+
```
|
455 |
+
|
456 |
+
You can use several levels of round brackets to even more increase the importance of a part:
|
457 |
+
```
|
458 |
+
A Vietnamese woman, ((red clothes)), (walking), smilling, in the street, a car on the left, in a modern city, photorealistic, 8k
|
459 |
+
```
|
460 |
+
|
461 |
+
You can use number instead of several round brackets:
|
462 |
+
```
|
463 |
+
A Vietnamese woman, (red clothes:1.5), (walking), smilling, in the street, a car on the left, in a modern city, photorealistic, 8k
|
464 |
+
```
|
465 |
+
|
466 |
+
You can do the same thing with square brackets to decrease the importance of a part:
|
467 |
+
```
|
468 |
+
A [Vietnamese] woman, (red clothes:1.5), (walking), smilling, in the street, a car on the left, in a modern city, photorealistic, 8k
|
469 |
+
```
|
470 |
+
|
471 |
+
To easily read your negative prompt, organize it the same way as your prompt (not important for the AI):
|
472 |
+
```
|
473 |
+
man, boy, hat, running, tree, bicycle, forest, drawing, painting, cartoon, 3d, monochrome, blurry, noisy, bokeh
|
474 |
+
```
|
475 |
+
|
476 |
+
## Credit
|
477 |
+
The [example image](https://commons.wikimedia.org/wiki/File:Coucang.jpg) is by Aprisonsan
|
478 |
+
and licensed under CC-BY-SA 4.0 International.
|
479 |
+
"""
|
|
|
|
|
480 |
)
|
481 |
|
482 |
+
interface.queue().launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|