DmitrMakeev
commited on
Commit
•
cafa4f8
1
Parent(s):
1beb2ae
Upload multi_frame_render.py
Browse files- script/multi_frame_render.py +201 -0
script/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
|