Commit
·
68e2801
1
Parent(s):
336d0d9
Upload 3 files
Browse files- scripts/loopback_wave.py +345 -0
- scripts/multi_frame_render.py +201 -0
- scripts/run_n_times.py +24 -0
scripts/loopback_wave.py
ADDED
@@ -0,0 +1,345 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import platform
|
3 |
+
import numpy as np
|
4 |
+
from tqdm import trange
|
5 |
+
import math
|
6 |
+
import subprocess as sp
|
7 |
+
import string
|
8 |
+
import random
|
9 |
+
from functools import reduce
|
10 |
+
import re
|
11 |
+
|
12 |
+
import modules.scripts as scripts
|
13 |
+
import gradio as gr
|
14 |
+
|
15 |
+
from modules import processing, shared, sd_samplers, images
|
16 |
+
from modules.processing import Processed
|
17 |
+
from modules.sd_samplers import samplers
|
18 |
+
from modules.shared import opts, cmd_opts, state
|
19 |
+
import subprocess
|
20 |
+
|
21 |
+
|
22 |
+
wave_completed_regex = r'@wave_completed\(([\-]?[0-9]*\.?[0-9]+), ?([\-]?[0-9]*\.?[0-9]+)\)'
|
23 |
+
wave_remaining_regex = r'@wave_remaining\(([\-]?[0-9]*\.?[0-9]+), ?([\-]?[0-9]*\.?[0-9]+)\)'
|
24 |
+
|
25 |
+
def run_cmd(cmd):
|
26 |
+
cmd = list(map(lambda arg: str(arg), cmd))
|
27 |
+
print("Executing %s" % " ".join(cmd))
|
28 |
+
popen_params = {"stdout": sp.DEVNULL, "stderr": sp.PIPE, "stdin": sp.DEVNULL}
|
29 |
+
|
30 |
+
if os.name == "nt":
|
31 |
+
popen_params["creationflags"] = 0x08000000
|
32 |
+
|
33 |
+
proc = sp.Popen(cmd, **popen_params)
|
34 |
+
out, err = proc.communicate() # proc.wait()
|
35 |
+
proc.stderr.close()
|
36 |
+
|
37 |
+
if proc.returncode:
|
38 |
+
raise IOError(err.decode("utf8"))
|
39 |
+
|
40 |
+
del proc
|
41 |
+
|
42 |
+
def encode_video(input_pattern, starting_number, output_dir, fps, quality, encoding, create_segments, segment_duration, ffmpeg_path):
|
43 |
+
two_pass = (encoding == "VP9 (webm)")
|
44 |
+
alpha_channel = ("webm" in encoding)
|
45 |
+
suffix = "webm" if "webm" in encoding else "mp4"
|
46 |
+
output_location = output_dir + f".{suffix}"
|
47 |
+
|
48 |
+
encoding_lib = {
|
49 |
+
"VP9 (webm)": "libvpx-vp9",
|
50 |
+
"VP8 (webm)": "libvpx",
|
51 |
+
"H.264 (mp4)": "libx264",
|
52 |
+
"H.265 (mp4)": "libx265",
|
53 |
+
}[encoding]
|
54 |
+
|
55 |
+
args = [
|
56 |
+
"-framerate", fps,
|
57 |
+
"-start_number", int(starting_number),
|
58 |
+
"-i", input_pattern,
|
59 |
+
"-c:v", encoding_lib,
|
60 |
+
"-b:v","0",
|
61 |
+
"-crf", quality,
|
62 |
+
]
|
63 |
+
|
64 |
+
if encoding_lib == "libvpx-vp9":
|
65 |
+
args += ["-pix_fmt", "yuva420p"]
|
66 |
+
|
67 |
+
if(ffmpeg_path == ""):
|
68 |
+
ffmpeg_path = "ffmpeg"
|
69 |
+
if(platform.system == "Windows"):
|
70 |
+
ffmpeg_path += ".exe"
|
71 |
+
|
72 |
+
print("\n\n")
|
73 |
+
if two_pass:
|
74 |
+
first_pass_args = args + [
|
75 |
+
"-pass", "1",
|
76 |
+
"-an",
|
77 |
+
"-f", "null",
|
78 |
+
os.devnull
|
79 |
+
]
|
80 |
+
|
81 |
+
second_pass_args = args + [
|
82 |
+
"-pass", "2",
|
83 |
+
output_location
|
84 |
+
]
|
85 |
+
|
86 |
+
print("Running first pass ffmpeg encoding")
|
87 |
+
|
88 |
+
run_cmd([ffmpeg_path] + first_pass_args)
|
89 |
+
print("Running second pass ffmpeg encoding. This could take awhile...")
|
90 |
+
run_cmd([ffmpeg_path] + second_pass_args)
|
91 |
+
else:
|
92 |
+
print("Running ffmpeg encoding. This could take awhile...")
|
93 |
+
run_cmd([ffmpeg_path] + args + [output_location])
|
94 |
+
|
95 |
+
if(create_segments):
|
96 |
+
print("Segmenting video")
|
97 |
+
run_cmd([ffmpeg_path] + [
|
98 |
+
"-i", output_location,
|
99 |
+
"-f", "segment",
|
100 |
+
"-segment_time", segment_duration,
|
101 |
+
"-vcodec", "copy",
|
102 |
+
"-acodec", "copy",
|
103 |
+
f"{output_dir}.%d.{suffix}"
|
104 |
+
])
|
105 |
+
|
106 |
+
def set_weights(match_obj, wave_progress):
|
107 |
+
weight_0 = 0
|
108 |
+
weight_1 = 0
|
109 |
+
if match_obj.group(1) is not None:
|
110 |
+
weight_0 = float(match_obj.group(1))
|
111 |
+
if match_obj.group(2) is not None:
|
112 |
+
weight_1 = float(match_obj.group(2))
|
113 |
+
|
114 |
+
max_weight = max(weight_0, weight_1)
|
115 |
+
min_weight = min(weight_0, weight_1)
|
116 |
+
|
117 |
+
weight_range = max_weight - min_weight
|
118 |
+
weight = min_weight + weight_range * wave_progress
|
119 |
+
return str(weight)
|
120 |
+
|
121 |
+
|
122 |
+
class Script(scripts.Script):
|
123 |
+
def title(self):
|
124 |
+
return "Loopback Wave V1.4.1"
|
125 |
+
|
126 |
+
def show(self, is_img2img):
|
127 |
+
return is_img2img
|
128 |
+
|
129 |
+
def ui(self, is_img2img):
|
130 |
+
frames = gr.Slider(minimum=1, maximum=2048, step=1, label='Frames', value=100)
|
131 |
+
frames_per_wave = gr.Slider(minimum=0, maximum=120, step=1, label='Frames Per Wave', value=20)
|
132 |
+
denoising_strength_change_amplitude = gr.Slider(minimum=0, maximum=1, step=0.01, label='Max additional denoise', value=0.6)
|
133 |
+
denoising_strength_change_offset = gr.Number(minimum=0, maximum=180, step=1, label='Wave offset (ignore this if you don\'t know what it means)', value=0)
|
134 |
+
initial_image_number = gr.Number(minimum=0, label='Initial generated image number', value=0)
|
135 |
+
|
136 |
+
save_prompts = gr.Checkbox(label='Save prompts as text file', value=True)
|
137 |
+
prompts = gr.Textbox(label="Prompt Changes", lines=5, value="")
|
138 |
+
|
139 |
+
save_video = gr.Checkbox(label='Save results as video', value=True)
|
140 |
+
output_dir = gr.Textbox(label="Video Name", lines=1, value="")
|
141 |
+
video_fps = gr.Slider(minimum=1, maximum=120, step=1, label='Frames per second', value=10)
|
142 |
+
video_quality = gr.Slider(minimum=0, maximum=60, step=1, label='Video Quality (crf)', value=40)
|
143 |
+
video_encoding = gr.Dropdown(label='Video encoding', value="VP9 (webm)", choices=["VP9 (webm)", "VP8 (webm)", "H.265 (mp4)", "H.264 (mp4)"])
|
144 |
+
ffmpeg_path = gr.Textbox(label="ffmpeg binary. Only set this if it fails otherwise.", lines=1, value="")
|
145 |
+
|
146 |
+
segment_video = gr.Checkbox(label='Cut video in to segments', value=True)
|
147 |
+
video_segment_duration = gr.Slider(minimum=10, maximum=60, step=1, label='Video Segment Duration (seconds)', value=20)
|
148 |
+
|
149 |
+
|
150 |
+
return [frames, denoising_strength_change_amplitude, frames_per_wave, denoising_strength_change_offset,initial_image_number, prompts, save_prompts, save_video, output_dir, video_fps, video_quality, video_encoding, ffmpeg_path, segment_video, video_segment_duration]
|
151 |
+
|
152 |
+
def run(self, p, frames, denoising_strength_change_amplitude, frames_per_wave, denoising_strength_change_offset, initial_image_number, prompts: str,save_prompts, save_video, output_dir, video_fps, video_quality, video_encoding, ffmpeg_path, segment_video, video_segment_duration):
|
153 |
+
processing.fix_seed(p)
|
154 |
+
batch_count = p.n_iter
|
155 |
+
p.extra_generation_params = {
|
156 |
+
"Max Additional Denoise": denoising_strength_change_amplitude,
|
157 |
+
"Frames per wave": frames_per_wave,
|
158 |
+
"Wave Offset": denoising_strength_change_offset,
|
159 |
+
}
|
160 |
+
|
161 |
+
# We save them ourselves for the sake of ffmpeg
|
162 |
+
p.do_not_save_samples = True
|
163 |
+
|
164 |
+
changes_dict = {}
|
165 |
+
|
166 |
+
|
167 |
+
p.batch_size = 1
|
168 |
+
p.n_iter = 1
|
169 |
+
|
170 |
+
output_images, info = None, None
|
171 |
+
initial_seed = None
|
172 |
+
initial_info = None
|
173 |
+
|
174 |
+
grids = []
|
175 |
+
all_images = []
|
176 |
+
original_init_image = p.init_images
|
177 |
+
state.job_count = frames * batch_count
|
178 |
+
|
179 |
+
initial_color_corrections = [processing.setup_color_correction(p.init_images[0])]
|
180 |
+
initial_denoising_strength = p.denoising_strength
|
181 |
+
|
182 |
+
if(output_dir==""):
|
183 |
+
output_dir = str(p.seed)
|
184 |
+
else:
|
185 |
+
output_dir = output_dir + "-" + str(p.seed)
|
186 |
+
|
187 |
+
loopback_wave_path = os.path.join(p.outpath_samples, "loopback-wave")
|
188 |
+
loopback_wave_images_path = os.path.join(loopback_wave_path, output_dir)
|
189 |
+
|
190 |
+
os.makedirs(loopback_wave_images_path, exist_ok=True)
|
191 |
+
|
192 |
+
p.outpath_samples = loopback_wave_images_path
|
193 |
+
|
194 |
+
prompts = prompts.strip()
|
195 |
+
|
196 |
+
if save_prompts:
|
197 |
+
with open(loopback_wave_images_path + "-prompts.txt", "w") as f:
|
198 |
+
generation_settings = [
|
199 |
+
"Generation Settings",
|
200 |
+
f"Total Frames: {frames}",
|
201 |
+
f"Frames Per Wave: {frames_per_wave}",
|
202 |
+
f"Wave Offset: {denoising_strength_change_offset}",
|
203 |
+
f"Base Denoising Strength: {initial_denoising_strength}",
|
204 |
+
f"Max Additional Denoise: {denoising_strength_change_amplitude}",
|
205 |
+
f"Initial Image Number: {initial_image_number}",
|
206 |
+
"",
|
207 |
+
"Video Encoding Settings",
|
208 |
+
f"Save Video: {save_video}"
|
209 |
+
]
|
210 |
+
|
211 |
+
if save_video:
|
212 |
+
generation_settings = generation_settings + [
|
213 |
+
f"Framerate: {video_fps}",
|
214 |
+
f"Quality: {video_quality}",
|
215 |
+
f"Encoding: {video_encoding}",
|
216 |
+
f"Create Segmented Video: {segment_video}"
|
217 |
+
]
|
218 |
+
|
219 |
+
if segment_video:
|
220 |
+
generation_settings = generation_settings + [f"Segment Duration: {video_segment_duration}"]
|
221 |
+
|
222 |
+
generation_settings = generation_settings + [
|
223 |
+
"",
|
224 |
+
"Prompt Details",
|
225 |
+
"Initial Prompt:" + p.prompt,
|
226 |
+
"",
|
227 |
+
"Negative Prompt:" + p.negative_prompt,
|
228 |
+
"",
|
229 |
+
"Frame change prompts:",
|
230 |
+
prompts
|
231 |
+
]
|
232 |
+
|
233 |
+
|
234 |
+
|
235 |
+
f.write('\n'.join(generation_settings))
|
236 |
+
|
237 |
+
if prompts:
|
238 |
+
lines = prompts.split("\n")
|
239 |
+
for prompt_line in lines:
|
240 |
+
params = prompt_line.split("::")
|
241 |
+
if len(params) == 2:
|
242 |
+
changes_dict[params[0]] = { "prompt": params[1] }
|
243 |
+
elif len(params) == 3:
|
244 |
+
changes_dict[params[0]] = { "seed": params[1], "prompt": params[2] }
|
245 |
+
else:
|
246 |
+
raise IOError(f"Invalid input in prompt line: {prompt_line}")
|
247 |
+
|
248 |
+
raw_prompt = p.prompt
|
249 |
+
|
250 |
+
for n in range(batch_count):
|
251 |
+
history = []
|
252 |
+
|
253 |
+
# Reset to original init image at the start of each batch
|
254 |
+
p.init_images = original_init_image
|
255 |
+
|
256 |
+
seed_state = "adding"
|
257 |
+
current_seed = p.seed
|
258 |
+
|
259 |
+
for i in range(frames):
|
260 |
+
current_seed = p.seed
|
261 |
+
state.job = ""
|
262 |
+
|
263 |
+
if str(i) in changes_dict:
|
264 |
+
raw_prompt = changes_dict[str(i)]["prompt"]
|
265 |
+
state.job = "New prompt: %s\n" % raw_prompt
|
266 |
+
|
267 |
+
if "seed" in changes_dict[str(i)]:
|
268 |
+
current_seed = changes_dict[str(i)]["seed"]
|
269 |
+
|
270 |
+
if current_seed.startswith("+"):
|
271 |
+
seed_state = "adding"
|
272 |
+
current_seed = current_seed.strip("+")
|
273 |
+
elif current_seed.startswith("-"):
|
274 |
+
seed_state = "subtracting"
|
275 |
+
current_seed = current_seed.strip("-")
|
276 |
+
else:
|
277 |
+
seed_state = "constant"
|
278 |
+
|
279 |
+
current_seed = int(current_seed)
|
280 |
+
p.seed = current_seed
|
281 |
+
|
282 |
+
|
283 |
+
|
284 |
+
p.n_iter = 1
|
285 |
+
p.batch_size = 1
|
286 |
+
p.do_not_save_grid = True
|
287 |
+
|
288 |
+
if opts.img2img_color_correction:
|
289 |
+
p.color_corrections = initial_color_corrections
|
290 |
+
|
291 |
+
|
292 |
+
wave_progress = float(1)/(float(frames_per_wave - 1))*float(((float(i)%float(frames_per_wave)) + ((float(1)/float(180))*denoising_strength_change_offset)))
|
293 |
+
print(wave_progress)
|
294 |
+
new_prompt = re.sub(wave_completed_regex, lambda x: set_weights(x, wave_progress), raw_prompt)
|
295 |
+
new_prompt = re.sub(wave_remaining_regex, lambda x: set_weights(x, 1 - wave_progress), new_prompt)
|
296 |
+
p.prompt = new_prompt
|
297 |
+
|
298 |
+
print(new_prompt)
|
299 |
+
|
300 |
+
denoising_strength_change_rate = 180/frames_per_wave
|
301 |
+
|
302 |
+
cos = abs(math.cos(math.radians(i*denoising_strength_change_rate + denoising_strength_change_offset)))
|
303 |
+
p.denoising_strength = initial_denoising_strength + denoising_strength_change_amplitude - (cos * denoising_strength_change_amplitude)
|
304 |
+
|
305 |
+
state.job += f"Iteration {i + 1}/{frames}, batch {n + 1}/{batch_count}. Denoising Strength: {p.denoising_strength}"
|
306 |
+
|
307 |
+
processed = processing.process_images(p)
|
308 |
+
|
309 |
+
if initial_seed is None:
|
310 |
+
initial_seed = processed.seed
|
311 |
+
initial_info = processed.info
|
312 |
+
|
313 |
+
init_img = processed.images[0]
|
314 |
+
|
315 |
+
p.init_images = [init_img]
|
316 |
+
|
317 |
+
if seed_state == "adding":
|
318 |
+
p.seed = processed.seed + 1
|
319 |
+
elif seed_state == "subtracting":
|
320 |
+
p.seed = processed.seed - 1
|
321 |
+
|
322 |
+
image_number = int(initial_image_number + i)
|
323 |
+
images.save_image(init_img, p.outpath_samples, "", processed.seed, processed.prompt, forced_filename=str(image_number))
|
324 |
+
|
325 |
+
history.append(init_img)
|
326 |
+
|
327 |
+
grid = images.image_grid(history, rows=1)
|
328 |
+
if opts.grid_save:
|
329 |
+
images.save_image(grid, p.outpath_grids, "grid", initial_seed, p.prompt, opts.grid_format, info=info, short_filename=not opts.grid_extended_filename, grid=True, p=p)
|
330 |
+
|
331 |
+
grids.append(grid)
|
332 |
+
all_images += history
|
333 |
+
|
334 |
+
if opts.return_grid:
|
335 |
+
all_images = grids + all_images
|
336 |
+
|
337 |
+
if save_video:
|
338 |
+
input_pattern = os.path.join(loopback_wave_images_path, "%d.png")
|
339 |
+
encode_video(input_pattern, initial_image_number, loopback_wave_images_path, video_fps, video_quality, video_encoding, segment_video, video_segment_duration, ffmpeg_path)
|
340 |
+
|
341 |
+
processed = Processed(p, all_images, initial_seed, initial_info)
|
342 |
+
|
343 |
+
return processed
|
344 |
+
|
345 |
+
|
scripts/multi_frame_render.py
ADDED
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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(minimum=0, maximum=1, step=0.05, label='Initial Denoise Strength', value=1, elem_id=self.elem_id("first_denoise"))
|
26 |
+
append_interrogation = gr.Dropdown(label="Append interrogated prompt at each iteration", choices=["None", "CLIP", "DeepBooru"], value="None")
|
27 |
+
third_frame_image = gr.Dropdown(label="Third Frame Image", choices=["None", "FirstGen", "GuideImg", "Historical"], value="None")
|
28 |
+
reference_imgs = gr.UploadButton(label="Upload Guide Frames", file_types = ['.png','.jpg','.jpeg'], live=True, file_count = "multiple")
|
29 |
+
color_correction_enabled = gr.Checkbox(label="Enable Color Correction", value=False, elem_id=self.elem_id("color_correction_enabled"))
|
30 |
+
unfreeze_seed = gr.Checkbox(label="Unfreeze Seed", value=False, elem_id=self.elem_id("unfreeze_seed"))
|
31 |
+
loopback_source = gr.Dropdown(label="Loopback Source", choices=["PreviousFrame", "InputFrame","FirstGen"], value="PreviousFrame")
|
32 |
+
|
33 |
+
return [append_interrogation, reference_imgs, first_denoise, third_frame_image, color_correction_enabled, unfreeze_seed, loopback_source]
|
34 |
+
|
35 |
+
def run(self, p, append_interrogation, reference_imgs, first_denoise, third_frame_image, color_correction_enabled, unfreeze_seed, loopback_source):
|
36 |
+
freeze_seed = not unfreeze_seed
|
37 |
+
|
38 |
+
loops = len(reference_imgs)
|
39 |
+
|
40 |
+
processing.fix_seed(p)
|
41 |
+
batch_count = p.n_iter
|
42 |
+
|
43 |
+
p.batch_size = 1
|
44 |
+
p.n_iter = 1
|
45 |
+
|
46 |
+
output_images, info = None, None
|
47 |
+
initial_seed = None
|
48 |
+
initial_info = None
|
49 |
+
|
50 |
+
initial_width = p.width
|
51 |
+
initial_img = p.init_images[0]
|
52 |
+
|
53 |
+
grids = []
|
54 |
+
all_images = []
|
55 |
+
original_init_image = p.init_images
|
56 |
+
original_prompt = p.prompt
|
57 |
+
original_denoise = p.denoising_strength
|
58 |
+
state.job_count = loops * batch_count
|
59 |
+
|
60 |
+
initial_color_corrections = [processing.setup_color_correction(p.init_images[0])]
|
61 |
+
|
62 |
+
for n in range(batch_count):
|
63 |
+
history = []
|
64 |
+
frames = []
|
65 |
+
third_image = None
|
66 |
+
third_image_index = 0
|
67 |
+
frame_color_correction = None
|
68 |
+
|
69 |
+
# Reset to original init image at the start of each batch
|
70 |
+
p.init_images = original_init_image
|
71 |
+
p.width = initial_width
|
72 |
+
|
73 |
+
for i in range(loops):
|
74 |
+
p.n_iter = 1
|
75 |
+
p.batch_size = 1
|
76 |
+
p.do_not_save_grid = True
|
77 |
+
p.control_net_input_image = Image.open(reference_imgs[i].name).convert("RGB").resize((initial_width, p.height), Image.ANTIALIAS)
|
78 |
+
|
79 |
+
if(i > 0):
|
80 |
+
loopback_image = p.init_images[0]
|
81 |
+
if loopback_source == "InputFrame":
|
82 |
+
loopback_image = p.control_net_input_image
|
83 |
+
elif loopback_source == "FirstGen":
|
84 |
+
loopback_image = history[0]
|
85 |
+
|
86 |
+
|
87 |
+
if third_frame_image != "None" and i > 1:
|
88 |
+
p.width = initial_width * 3
|
89 |
+
img = Image.new("RGB", (initial_width*3, p.height))
|
90 |
+
img.paste(p.init_images[0], (0, 0))
|
91 |
+
# img.paste(p.init_images[0], (initial_width, 0))
|
92 |
+
img.paste(loopback_image, (initial_width, 0))
|
93 |
+
img.paste(third_image, (initial_width*2, 0))
|
94 |
+
p.init_images = [img]
|
95 |
+
if color_correction_enabled:
|
96 |
+
p.color_corrections = [processing.setup_color_correction(img)]
|
97 |
+
|
98 |
+
msk = Image.new("RGB", (initial_width*3, p.height))
|
99 |
+
msk.paste(Image.open(reference_imgs[i-1].name).convert("RGB").resize((initial_width, p.height), Image.ANTIALIAS), (0, 0))
|
100 |
+
msk.paste(p.control_net_input_image, (initial_width, 0))
|
101 |
+
msk.paste(Image.open(reference_imgs[third_image_index].name).convert("RGB").resize((initial_width, p.height), Image.ANTIALIAS), (initial_width*2, 0))
|
102 |
+
p.control_net_input_image = msk
|
103 |
+
|
104 |
+
latent_mask = Image.new("RGB", (initial_width*3, p.height), "black")
|
105 |
+
latent_draw = ImageDraw.Draw(latent_mask)
|
106 |
+
latent_draw.rectangle((initial_width,0,initial_width*2,p.height), fill="white")
|
107 |
+
p.image_mask = latent_mask
|
108 |
+
p.denoising_strength = original_denoise
|
109 |
+
else:
|
110 |
+
p.width = initial_width * 2
|
111 |
+
img = Image.new("RGB", (initial_width*2, p.height))
|
112 |
+
img.paste(p.init_images[0], (0, 0))
|
113 |
+
# img.paste(p.init_images[0], (initial_width, 0))
|
114 |
+
img.paste(loopback_image, (initial_width, 0))
|
115 |
+
p.init_images = [img]
|
116 |
+
if color_correction_enabled:
|
117 |
+
p.color_corrections = [processing.setup_color_correction(img)]
|
118 |
+
|
119 |
+
msk = Image.new("RGB", (initial_width*2, p.height))
|
120 |
+
msk.paste(Image.open(reference_imgs[i-1].name).convert("RGB").resize((initial_width, p.height), Image.ANTIALIAS), (0, 0))
|
121 |
+
msk.paste(p.control_net_input_image, (initial_width, 0))
|
122 |
+
p.control_net_input_image = msk
|
123 |
+
frames.append(msk)
|
124 |
+
|
125 |
+
# latent_mask = Image.new("RGB", (initial_width*2, p.height), "white")
|
126 |
+
# latent_draw = ImageDraw.Draw(latent_mask)
|
127 |
+
# latent_draw.rectangle((0,0,initial_width,p.height), fill="black")
|
128 |
+
latent_mask = Image.new("RGB", (initial_width*2, p.height), "black")
|
129 |
+
latent_draw = ImageDraw.Draw(latent_mask)
|
130 |
+
latent_draw.rectangle((initial_width,0,initial_width*2,p.height), fill="white")
|
131 |
+
|
132 |
+
# p.latent_mask = latent_mask
|
133 |
+
p.image_mask = latent_mask
|
134 |
+
p.denoising_strength = original_denoise
|
135 |
+
else:
|
136 |
+
latent_mask = Image.new("RGB", (initial_width, p.height), "white")
|
137 |
+
# p.latent_mask = latent_mask
|
138 |
+
p.image_mask = latent_mask
|
139 |
+
p.denoising_strength = first_denoise
|
140 |
+
p.control_net_input_image = p.control_net_input_image.resize((initial_width, p.height))
|
141 |
+
frames.append(p.control_net_input_image)
|
142 |
+
|
143 |
+
|
144 |
+
if append_interrogation != "None":
|
145 |
+
p.prompt = original_prompt + ", " if original_prompt != "" else ""
|
146 |
+
if append_interrogation == "CLIP":
|
147 |
+
p.prompt += shared.interrogator.interrogate(p.init_images[0])
|
148 |
+
elif append_interrogation == "DeepBooru":
|
149 |
+
p.prompt += deepbooru.model.tag(p.init_images[0])
|
150 |
+
|
151 |
+
state.job = f"Iteration {i + 1}/{loops}, batch {n + 1}/{batch_count}"
|
152 |
+
|
153 |
+
processed = processing.process_images(p)
|
154 |
+
|
155 |
+
if initial_seed is None:
|
156 |
+
initial_seed = processed.seed
|
157 |
+
initial_info = processed.info
|
158 |
+
|
159 |
+
init_img = processed.images[0]
|
160 |
+
if(i > 0):
|
161 |
+
init_img = init_img.crop((initial_width, 0, initial_width*2, p.height))
|
162 |
+
|
163 |
+
if third_frame_image != "None":
|
164 |
+
if third_frame_image == "FirstGen" and i == 0:
|
165 |
+
third_image = init_img
|
166 |
+
third_image_index = 0
|
167 |
+
elif third_frame_image == "GuideImg" and i == 0:
|
168 |
+
third_image = original_init_image[0]
|
169 |
+
third_image_index = 0
|
170 |
+
elif third_frame_image == "Historical":
|
171 |
+
third_image = processed.images[0].crop((0, 0, initial_width, p.height))
|
172 |
+
third_image_index = (i-1)
|
173 |
+
|
174 |
+
p.init_images = [init_img]
|
175 |
+
if(freeze_seed):
|
176 |
+
p.seed = processed.seed
|
177 |
+
else:
|
178 |
+
p.seed = processed.seed + 1
|
179 |
+
|
180 |
+
history.append(init_img)
|
181 |
+
if opts.samples_save:
|
182 |
+
images.save_image(init_img, p.outpath_samples, "Frame", p.seed, p.prompt, opts.grid_format, info=info, short_filename=not opts.grid_extended_filename, grid=True, p=p)
|
183 |
+
|
184 |
+
frames.append(processed.images[0])
|
185 |
+
|
186 |
+
grid = images.image_grid(history, rows=1)
|
187 |
+
if opts.grid_save:
|
188 |
+
images.save_image(grid, p.outpath_grids, "grid", initial_seed, p.prompt, opts.grid_format, info=info, short_filename=not opts.grid_extended_filename, grid=True, p=p)
|
189 |
+
|
190 |
+
grids.append(grid)
|
191 |
+
# all_images += history + frames
|
192 |
+
all_images += history
|
193 |
+
|
194 |
+
p.seed = p.seed+1
|
195 |
+
|
196 |
+
if opts.return_grid:
|
197 |
+
all_images = grids + all_images
|
198 |
+
|
199 |
+
processed = Processed(p, all_images, initial_seed, initial_info)
|
200 |
+
|
201 |
+
return processed
|
scripts/run_n_times.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
import os
|
3 |
+
import sys
|
4 |
+
import traceback
|
5 |
+
|
6 |
+
import modules.scripts as scripts
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
from modules.processing import Processed, process_images
|
10 |
+
|
11 |
+
class Script(scripts.Script):
|
12 |
+
def title(self):
|
13 |
+
return "Run n times"
|
14 |
+
|
15 |
+
def ui(self, is_img2img):
|
16 |
+
n = gr.Textbox(label="n")
|
17 |
+
return [n]
|
18 |
+
|
19 |
+
def run(self, p, n):
|
20 |
+
for x in range(int(n)):
|
21 |
+
p.seed = -1
|
22 |
+
proc = process_images(p)
|
23 |
+
image = proc.images
|
24 |
+
return Processed(p, image, p.seed, proc.info)
|