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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Beta V0.72
|
2 |
+
import numpy as np
|
3 |
+
from tqdm import trange
|
4 |
+
from PIL import Image, ImageSequence, ImageDraw
|
5 |
+
import math
|
6 |
+
|
7 |
+
import modules.scripts as scripts
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
from modules import processing, shared, sd_samplers, images
|
11 |
+
from modules.processing import Processed
|
12 |
+
from modules.sd_samplers import samplers
|
13 |
+
from modules.shared import opts, cmd_opts, state
|
14 |
+
from modules import deepbooru
|
15 |
+
|
16 |
+
|
17 |
+
class Script(scripts.Script):
|
18 |
+
def title(self):
|
19 |
+
return "(Beta) Multi-frame Video rendering - V0.72"
|
20 |
+
|
21 |
+
def show(self, is_img2img):
|
22 |
+
return is_img2img
|
23 |
+
|
24 |
+
def ui(self, is_img2img):
|
25 |
+
first_denoise = gr.Slider(
|
26 |
+
minimum=0,
|
27 |
+
maximum=1,
|
28 |
+
step=0.05,
|
29 |
+
label="Initial Denoise Strength",
|
30 |
+
value=1,
|
31 |
+
elem_id=self.elem_id("first_denoise"),
|
32 |
+
)
|
33 |
+
append_interrogation = gr.Dropdown(
|
34 |
+
label="Append interrogated prompt at each iteration",
|
35 |
+
choices=["None", "CLIP", "DeepBooru"],
|
36 |
+
value="None",
|
37 |
+
)
|
38 |
+
third_frame_image = gr.Dropdown(
|
39 |
+
label="Third Frame Image",
|
40 |
+
choices=["None", "FirstGen", "GuideImg", "Historical"],
|
41 |
+
value="None",
|
42 |
+
)
|
43 |
+
# reference_imgs = gr.UploadButton(label="Upload Guide Frames", file_types = ['.png','.jpg','.jpeg'], live=True, file_count = "multiple")
|
44 |
+
reference_imgs = gr.File(
|
45 |
+
file_count="multiple",
|
46 |
+
file_types=[".png", ".jpg", ".jpeg"],
|
47 |
+
label="Upload Guide Frames",
|
48 |
+
show_label=True,
|
49 |
+
live=True,
|
50 |
+
)
|
51 |
+
color_correction_enabled = gr.Checkbox(
|
52 |
+
label="Enable Color Correction",
|
53 |
+
value=False,
|
54 |
+
elem_id=self.elem_id("color_correction_enabled"),
|
55 |
+
)
|
56 |
+
unfreeze_seed = gr.Checkbox(
|
57 |
+
label="Unfreeze Seed", value=False, elem_id=self.elem_id("unfreeze_seed")
|
58 |
+
)
|
59 |
+
loopback_source = gr.Dropdown(
|
60 |
+
label="Loopback Source",
|
61 |
+
choices=["PreviousFrame", "InputFrame", "FirstGen"],
|
62 |
+
value="PreviousFrame",
|
63 |
+
)
|
64 |
+
|
65 |
+
return [
|
66 |
+
append_interrogation,
|
67 |
+
reference_imgs,
|
68 |
+
first_denoise,
|
69 |
+
third_frame_image,
|
70 |
+
color_correction_enabled,
|
71 |
+
unfreeze_seed,
|
72 |
+
loopback_source,
|
73 |
+
]
|
74 |
+
|
75 |
+
def run(
|
76 |
+
self,
|
77 |
+
p,
|
78 |
+
append_interrogation,
|
79 |
+
reference_imgs,
|
80 |
+
first_denoise,
|
81 |
+
third_frame_image,
|
82 |
+
color_correction_enabled,
|
83 |
+
unfreeze_seed,
|
84 |
+
loopback_source,
|
85 |
+
):
|
86 |
+
freeze_seed = not unfreeze_seed
|
87 |
+
|
88 |
+
loops = len(reference_imgs)
|
89 |
+
|
90 |
+
processing.fix_seed(p)
|
91 |
+
batch_count = p.n_iter
|
92 |
+
|
93 |
+
p.batch_size = 1
|
94 |
+
p.n_iter = 1
|
95 |
+
|
96 |
+
output_images, info = None, None
|
97 |
+
initial_seed = None
|
98 |
+
initial_info = None
|
99 |
+
|
100 |
+
initial_width = p.width
|
101 |
+
initial_img = p.init_images[0]
|
102 |
+
|
103 |
+
grids = []
|
104 |
+
all_images = []
|
105 |
+
original_init_image = p.init_images
|
106 |
+
original_prompt = p.prompt
|
107 |
+
original_denoise = p.denoising_strength
|
108 |
+
state.job_count = loops * batch_count
|
109 |
+
|
110 |
+
initial_color_corrections = [
|
111 |
+
processing.setup_color_correction(p.init_images[0])
|
112 |
+
]
|
113 |
+
|
114 |
+
for n in range(batch_count):
|
115 |
+
history = []
|
116 |
+
frames = []
|
117 |
+
third_image = None
|
118 |
+
third_image_index = 0
|
119 |
+
frame_color_correction = None
|
120 |
+
|
121 |
+
# Reset to original init image at the start of each batch
|
122 |
+
p.init_images = original_init_image
|
123 |
+
p.width = initial_width
|
124 |
+
|
125 |
+
for i in range(loops):
|
126 |
+
p.n_iter = 1
|
127 |
+
p.batch_size = 1
|
128 |
+
p.do_not_save_grid = True
|
129 |
+
p.control_net_input_image = (
|
130 |
+
Image.open(reference_imgs[i].name)
|
131 |
+
.convert("RGB")
|
132 |
+
.resize((initial_width, p.height), Image.ANTIALIAS)
|
133 |
+
)
|
134 |
+
|
135 |
+
if i > 0:
|
136 |
+
loopback_image = p.init_images[0]
|
137 |
+
if loopback_source == "InputFrame":
|
138 |
+
loopback_image = p.control_net_input_image
|
139 |
+
elif loopback_source == "FirstGen":
|
140 |
+
loopback_image = history[0]
|
141 |
+
|
142 |
+
if third_frame_image != "None" and i > 1:
|
143 |
+
p.width = initial_width * 3
|
144 |
+
img = Image.new("RGB", (initial_width * 3, p.height))
|
145 |
+
img.paste(p.init_images[0], (0, 0))
|
146 |
+
# img.paste(p.init_images[0], (initial_width, 0))
|
147 |
+
img.paste(loopback_image, (initial_width, 0))
|
148 |
+
img.paste(third_image, (initial_width * 2, 0))
|
149 |
+
p.init_images = [img]
|
150 |
+
if color_correction_enabled:
|
151 |
+
p.color_corrections = [
|
152 |
+
processing.setup_color_correction(img)
|
153 |
+
]
|
154 |
+
|
155 |
+
msk = Image.new("RGB", (initial_width * 3, p.height))
|
156 |
+
msk.paste(
|
157 |
+
Image.open(reference_imgs[i - 1].name)
|
158 |
+
.convert("RGB")
|
159 |
+
.resize((initial_width, p.height), Image.ANTIALIAS),
|
160 |
+
(0, 0),
|
161 |
+
)
|
162 |
+
msk.paste(p.control_net_input_image, (initial_width, 0))
|
163 |
+
msk.paste(
|
164 |
+
Image.open(reference_imgs[third_image_index].name)
|
165 |
+
.convert("RGB")
|
166 |
+
.resize((initial_width, p.height), Image.ANTIALIAS),
|
167 |
+
(initial_width * 2, 0),
|
168 |
+
)
|
169 |
+
p.control_net_input_image = msk
|
170 |
+
|
171 |
+
latent_mask = Image.new(
|
172 |
+
"RGB", (initial_width * 3, p.height), "black"
|
173 |
+
)
|
174 |
+
latent_draw = ImageDraw.Draw(latent_mask)
|
175 |
+
latent_draw.rectangle(
|
176 |
+
(initial_width, 0, initial_width * 2, p.height),
|
177 |
+
fill="white",
|
178 |
+
)
|
179 |
+
p.image_mask = latent_mask
|
180 |
+
p.denoising_strength = original_denoise
|
181 |
+
else:
|
182 |
+
p.width = initial_width * 2
|
183 |
+
img = Image.new("RGB", (initial_width * 2, p.height))
|
184 |
+
img.paste(p.init_images[0], (0, 0))
|
185 |
+
# img.paste(p.init_images[0], (initial_width, 0))
|
186 |
+
img.paste(loopback_image, (initial_width, 0))
|
187 |
+
p.init_images = [img]
|
188 |
+
if color_correction_enabled:
|
189 |
+
p.color_corrections = [
|
190 |
+
processing.setup_color_correction(img)
|
191 |
+
]
|
192 |
+
|
193 |
+
msk = Image.new("RGB", (initial_width * 2, p.height))
|
194 |
+
msk.paste(
|
195 |
+
Image.open(reference_imgs[i - 1].name)
|
196 |
+
.convert("RGB")
|
197 |
+
.resize((initial_width, p.height), Image.ANTIALIAS),
|
198 |
+
(0, 0),
|
199 |
+
)
|
200 |
+
msk.paste(p.control_net_input_image, (initial_width, 0))
|
201 |
+
p.control_net_input_image = msk
|
202 |
+
frames.append(msk)
|
203 |
+
|
204 |
+
# latent_mask = Image.new("RGB", (initial_width*2, p.height), "white")
|
205 |
+
# latent_draw = ImageDraw.Draw(latent_mask)
|
206 |
+
# latent_draw.rectangle((0,0,initial_width,p.height), fill="black")
|
207 |
+
latent_mask = Image.new(
|
208 |
+
"RGB", (initial_width * 2, p.height), "black"
|
209 |
+
)
|
210 |
+
latent_draw = ImageDraw.Draw(latent_mask)
|
211 |
+
latent_draw.rectangle(
|
212 |
+
(initial_width, 0, initial_width * 2, p.height),
|
213 |
+
fill="white",
|
214 |
+
)
|
215 |
+
|
216 |
+
# p.latent_mask = latent_mask
|
217 |
+
p.image_mask = latent_mask
|
218 |
+
p.denoising_strength = original_denoise
|
219 |
+
else:
|
220 |
+
latent_mask = Image.new("RGB", (initial_width, p.height), "white")
|
221 |
+
# p.latent_mask = latent_mask
|
222 |
+
p.image_mask = latent_mask
|
223 |
+
p.denoising_strength = first_denoise
|
224 |
+
p.control_net_input_image = p.control_net_input_image.resize(
|
225 |
+
(initial_width, p.height)
|
226 |
+
)
|
227 |
+
frames.append(p.control_net_input_image)
|
228 |
+
|
229 |
+
if append_interrogation != "None":
|
230 |
+
p.prompt = original_prompt + ", " if original_prompt != "" else ""
|
231 |
+
if append_interrogation == "CLIP":
|
232 |
+
p.prompt += shared.interrogator.interrogate(p.init_images[0])
|
233 |
+
elif append_interrogation == "DeepBooru":
|
234 |
+
p.prompt += deepbooru.model.tag(p.init_images[0])
|
235 |
+
|
236 |
+
state.job = f"Iteration {i + 1}/{loops}, batch {n + 1}/{batch_count}"
|
237 |
+
|
238 |
+
processed = processing.process_images(p)
|
239 |
+
|
240 |
+
if initial_seed is None:
|
241 |
+
initial_seed = processed.seed
|
242 |
+
initial_info = processed.info
|
243 |
+
|
244 |
+
init_img = processed.images[0]
|
245 |
+
if i > 0:
|
246 |
+
init_img = init_img.crop(
|
247 |
+
(initial_width, 0, initial_width * 2, p.height)
|
248 |
+
)
|
249 |
+
|
250 |
+
if third_frame_image != "None":
|
251 |
+
if third_frame_image == "FirstGen" and i == 0:
|
252 |
+
third_image = init_img
|
253 |
+
third_image_index = 0
|
254 |
+
elif third_frame_image == "GuideImg" and i == 0:
|
255 |
+
third_image = original_init_image[0]
|
256 |
+
third_image_index = 0
|
257 |
+
elif third_frame_image == "Historical":
|
258 |
+
third_image = processed.images[0].crop(
|
259 |
+
(0, 0, initial_width, p.height)
|
260 |
+
)
|
261 |
+
third_image_index = i - 1
|
262 |
+
|
263 |
+
p.init_images = [init_img]
|
264 |
+
if freeze_seed:
|
265 |
+
p.seed = processed.seed
|
266 |
+
else:
|
267 |
+
p.seed = processed.seed + 1
|
268 |
+
|
269 |
+
history.append(init_img)
|
270 |
+
if opts.samples_save:
|
271 |
+
images.save_image(
|
272 |
+
init_img,
|
273 |
+
p.outpath_samples,
|
274 |
+
"Frame",
|
275 |
+
p.seed,
|
276 |
+
p.prompt,
|
277 |
+
opts.grid_format,
|
278 |
+
info=info,
|
279 |
+
short_filename=not opts.grid_extended_filename,
|
280 |
+
grid=True,
|
281 |
+
p=p,
|
282 |
+
)
|
283 |
+
|
284 |
+
frames.append(processed.images[0])
|
285 |
+
|
286 |
+
grid = images.image_grid(history, rows=1)
|
287 |
+
if opts.grid_save:
|
288 |
+
images.save_image(
|
289 |
+
grid,
|
290 |
+
p.outpath_grids,
|
291 |
+
"grid",
|
292 |
+
initial_seed,
|
293 |
+
p.prompt,
|
294 |
+
opts.grid_format,
|
295 |
+
info=info,
|
296 |
+
short_filename=not opts.grid_extended_filename,
|
297 |
+
grid=True,
|
298 |
+
p=p,
|
299 |
+
)
|
300 |
+
|
301 |
+
grids.append(grid)
|
302 |
+
# all_images += history + frames
|
303 |
+
all_images += history
|
304 |
+
|
305 |
+
p.seed = p.seed + 1
|
306 |
+
|
307 |
+
if opts.return_grid:
|
308 |
+
all_images = grids + all_images
|
309 |
+
|
310 |
+
processed = Processed(p, all_images, initial_seed, initial_info)
|
311 |
+
|
312 |
+
return processed
|