File size: 24,467 Bytes
194b4ef |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 |
# Original Xanthius (https://xanthius.itch.io/multi-frame-rendering-for-stablediffusion)
# Modified OedoSoldier [大江户战士] (https://space.bilibili.com/55123)
import numpy as np
from tqdm import trange
from PIL import Image, ImageSequence, ImageDraw, ImageFilter, PngImagePlugin
import modules.scripts as scripts
import gradio as gr
from scripts.ei_utils import *
from modules import processing, shared, sd_samplers, images
from modules.processing import Processed
from modules.sd_samplers import samplers
from modules.shared import opts, cmd_opts, state
from modules import deepbooru
from modules.script_callbacks import ImageSaveParams, before_image_saved_callback
from modules.shared import opts, cmd_opts, state
from modules.sd_hijack import model_hijack
import pandas as pd
import piexif
import piexif.helper
import os
import re
re_findidx = re.compile(
r'(?=\S)(\d+)\.(?:[P|p][N|n][G|g]?|[J|j][P|p][G|g]?|[J|j][P|p][E|e][G|g]?|[W|w][E|e][B|b][P|p]?)\b')
re_findname = re.compile(r'[\w-]+?(?=\.)')
class Script(scripts.Script):
def title(self):
return "Multi-frame rendering"
def show(self, is_img2img):
return is_img2img
def ui(self, is_img2img):
with gr.Row():
input_dir = gr.Textbox(label='Input directory', lines=1)
output_dir = gr.Textbox(label='Output directory', lines=1)
# reference_imgs = gr.UploadButton(label="Upload Guide Frames", file_types = ['.png','.jpg','.jpeg'], live=True, file_count = "multiple")
with gr.Row():
mask_dir = gr.Textbox(label='Mask directory', placeholder="Keep blank if you don't have mask", lines=1)
first_denoise = gr.Slider(
minimum=0,
maximum=1,
step=0.05,
label='Initial denoising strength',
value=1,
elem_id=self.elem_id("first_denoise"))
append_interrogation = gr.Dropdown(
label="Append interrogated prompt at each iteration", choices=[
"None", "CLIP", "DeepBooru"], value="None")
third_frame_image = gr.Dropdown(
label="Third column (reference) image",
choices=[
"None",
"FirstGen",
"OriginalImg",
"Historical"],
value="FirstGen")
color_correction_enabled = gr.Checkbox(
label="Enable color correction",
value=False,
elem_id=self.elem_id("color_correction_enabled"))
unfreeze_seed = gr.Checkbox(
label="Unfreeze seed",
value=False,
elem_id=self.elem_id("unfreeze_seed"))
loopback_source = gr.Dropdown(
label="Loopback source",
choices=[
"Previous",
"Current",
"First"],
value="Current")
with gr.Row():
given_file = gr.Checkbox(
label='Process given file(s) under the input folder, seperate by comma')
specified_filename = gr.Textbox(
label='Files to process', lines=1, visible=False)
self.max_models = opts.data.get("control_net_max_models_num", 1)
with gr.Row():
use_cn_inpaint = gr.Checkbox(
label='Use Control Net inpaint model')
cn_inpaint_num = gr.Dropdown(
[f"Control Model - {i}" for i in range(self.max_models)], label="ControlNet inpaint model index", visible=False)
use_cn = gr.Checkbox(label='Use another image as ControlNet input')
with gr.Row(visible=False) as cn_options:
cn_dirs = []
with gr.Group():
with gr.Tabs():
for i in range(self.max_models):
with gr.Tab(f"ControlNet-{i}", open=False):
cn_dirs.append(gr.Textbox(label='ControlNet input directory', lines=1))
with gr.Row():
use_txt = gr.Checkbox(label='Read tags from text files')
with gr.Row():
txt_path = gr.Textbox(
label='Text files directory (Optional, will load from input dir if not specified)',
lines=1)
with gr.Row():
use_csv = gr.Checkbox(label='Read tabular commands')
csv_path = gr.File(
label='.csv or .xlsx',
file_types=['file'],
visible=False)
with gr.Row():
with gr.Column():
table_content = gr.Dataframe(visible=False, wrap=True)
use_csv.change(
fn=lambda x: [gr_show_value_none(x), gr_show_value_none(False)],
inputs=[use_csv],
outputs=[csv_path, table_content],
)
csv_path.change(
fn=lambda x: gr_show_and_load(x),
inputs=[csv_path],
outputs=[table_content],
)
given_file.change(
fn=lambda x: gr_show(x),
inputs=[given_file],
outputs=[specified_filename],
)
use_cn_inpaint.change(
fn=lambda x: gr_show(x),
inputs=[use_cn_inpaint],
outputs=[cn_inpaint_num]
)
use_cn.change(
fn=lambda x: gr_show(x),
inputs=[use_cn],
outputs=[cn_options],
)
return [
append_interrogation,
input_dir,
output_dir,
mask_dir,
first_denoise,
third_frame_image,
color_correction_enabled,
unfreeze_seed,
loopback_source,
use_csv,
table_content,
given_file,
specified_filename,
use_txt,
txt_path,
use_cn_inpaint,
cn_inpaint_num,
use_cn,
*cn_dirs,]
def run(
self,
p,
append_interrogation,
input_dir,
output_dir,
mask_dir,
first_denoise,
third_frame_image,
color_correction_enabled,
unfreeze_seed,
loopback_source,
use_csv,
table_content,
given_file,
specified_filename,
use_txt,
txt_path,
use_cn_inpaint,
cn_inpaint_num,
use_cn,
*cn_dirs,):
freeze_seed = not unfreeze_seed
if use_csv:
prompt_list = [i[0] for i in table_content.values.tolist()]
prompt_list.insert(0, prompt_list.pop())
history_imgs = None
if given_file:
if specified_filename == '':
images = [os.path.join(
input_dir,
f) for f in os.listdir(input_dir) if re.match(
r'.+\.(jpg|png)$',
f)]
else:
images = []
masks = []
images_in_folder = [os.path.join(
input_dir,
f) for f in os.listdir(input_dir) if re.match(
r'.+\.(jpg|png)$',
f)]
try:
images_idx = [int(re.findall(re_findidx, j)[0])
for j in images_in_folder]
except BaseException:
images_idx = [re.findall(re_findname, j)[0]
for j in images_in_folder]
images_in_folder_dict = dict(zip(images_idx, images_in_folder))
sep = ',' if ',' in specified_filename else ' '
for i in specified_filename.split(sep):
if i in images_in_folder:
images.append(i)
start = end = i
else:
try:
match = re.search(r'(^\d*)-(\d*$)', i)
if match:
start, end = match.groups()
if start == '':
start = images_idx[0]
if end == '':
end = images_idx[-1]
images += [images_in_folder_dict[j]
for j in list(range(int(start), int(end) + 1))]
except BaseException:
images.append(images_in_folder_dict[int(i)])
if len(images) == 0:
raise FileNotFoundError
reference_imgs = [images_in_folder_dict[images_idx[0]], images_in_folder_dict[max(0, int(start) - 1)]] + images
history_imgs = [images_in_folder_dict[images_idx[0]], images_in_folder_dict[max(images_idx[0], int(start) - 2)], images_in_folder_dict[max(0, int(start) - 1)]]
history_imgs = [images_in_folder_dict[images_idx[0]]] + [os.path.join(output_dir, os.path.basename(f)) for f in history_imgs]
else:
reference_imgs = [
os.path.join(
input_dir,
f) for f in os.listdir(input_dir) if re.match(
r'.+\.(jpg|png)$',
f)]
reference_imgs = sort_images(reference_imgs)
print(f'Will process following files: {", ".join(reference_imgs)}')
if use_txt:
if txt_path == "":
files = [re.sub(r'\.(jpg|png)$', '.txt', path)
for path in reference_imgs]
else:
files = [
os.path.join(
txt_path,
os.path.basename(
re.sub(
r'\.(jpg|png)$',
'.txt',
path))) for path in reference_imgs]
prompt_list = [open(file, 'r').read().rstrip('\n')
for file in files]
if use_cn:
cn_dirs = [input_dir if cn_dir=="" else cn_dir for cn_dir in cn_dirs]
cn_images = [[os.path.join(
cn_dir,
os.path.basename(path)) for path in reference_imgs] for cn_dir in cn_dirs]
loops = len(reference_imgs)
processing.fix_seed(p)
batch_count = p.n_iter
p.batch_size = 1
p.n_iter = 1
output_images, info = None, None
initial_seed = None
initial_info = None
initial_width = p.width
initial_img = reference_imgs[0] # p.init_images[0]
p.init_images = [
Image.open(initial_img).resize(
(initial_width, p.height), Image.ANTIALIAS)]
# grids = []
# all_images = []
# original_init_image = p.init_images
original_prompt = p.prompt
if original_prompt != "":
original_prompt = original_prompt.rstrip(
', ') + ', ' if not original_prompt.rstrip().endswith(',') else original_prompt.rstrip() + ' '
original_denoise = p.denoising_strength
state.job_count = (loops - 2) * batch_count if given_file else loops * batch_count
initial_color_corrections = [
processing.setup_color_correction(
p.init_images[0])]
# for n in range(batch_count):
history = None
# frames = []
third_image = None
third_image_index = 0
frame_color_correction = None
# Reset to original init image at the start of each batch
p.width = initial_width
p.control_net_resize_mode = "Just Resize"
for i in range(loops):
if state.interrupted:
break
if given_file and i < 2:
p.init_images[0] = Image.open(
history_imgs[-1]).resize(
(initial_width, p.height), Image.ANTIALIAS)
history = p.init_images[0]
if third_frame_image != "None":
if third_frame_image == "FirstGen" and i == 0:
third_image = Image.open(
history_imgs[1]).resize(
(initial_width, p.height), Image.ANTIALIAS)
third_image_index = 0
elif third_frame_image == "OriginalImg" and i == 0:
third_image = Image.open(
history_imgs[0]).resize(
(initial_width, p.height), Image.ANTIALIAS)
third_image_index = 0
elif third_frame_image == "Historical":
third_image = Image.open(
history_imgs[2]).resize(
(initial_width, p.height), Image.ANTIALIAS)
third_image_index = (i - 1)
continue
filename = os.path.basename(reference_imgs[i])
print(f'Processing: {reference_imgs[i]}')
p.n_iter = 1
p.batch_size = 1
p.do_not_save_grid = True
p.control_net_input_image = Image.open(
reference_imgs[i]).resize(
(initial_width, p.height), Image.ANTIALIAS)
if(i > 0):
loopback_image = p.init_images[0]
if loopback_source == "Current":
loopback_image = p.control_net_input_image
elif loopback_source == "First":
loopback_image = history
if third_frame_image != "None":
p.width = initial_width * 3
img = Image.new("RGB", (initial_width * 3, p.height))
img.paste(p.init_images[0], (0, 0))
# img.paste(p.init_images[0], (initial_width, 0))
img.paste(loopback_image, (initial_width, 0))
if i == 1:
third_image = p.init_images[0]
img.paste(third_image, (initial_width * 2, 0))
p.init_images = [img]
if color_correction_enabled:
p.color_corrections = [
processing.setup_color_correction(img)]
if use_cn:
msk = []
for cn_image in cn_images:
m = Image.new("RGB", (initial_width * 3, p.height))
m.paste(Image.open(cn_image[i - 1]).resize(
(initial_width, p.height), Image.ANTIALIAS), (0, 0))
m.paste(Image.open(cn_image[i]).resize(
(initial_width, p.height), Image.ANTIALIAS), (initial_width, 0))
m.paste(Image.open(cn_image[third_image_index]).resize(
(initial_width, p.height), Image.ANTIALIAS), (initial_width * 2, 0))
msk.append(m)
else:
msk = Image.new("RGB", (initial_width * 3, p.height))
msk.paste(Image.open(reference_imgs[i - 1]).resize(
(initial_width, p.height), Image.ANTIALIAS), (0, 0))
msk.paste(p.control_net_input_image, (initial_width, 0))
msk.paste(Image.open(reference_imgs[third_image_index]).resize(
(initial_width, p.height), Image.ANTIALIAS), (initial_width * 2, 0))
p.control_net_input_image = msk
latent_mask = Image.new(
"RGB", (initial_width * 3, p.height), "black")
if mask_dir == '':
latent_draw = ImageDraw.Draw(latent_mask)
latent_draw.rectangle(
(initial_width, 0, initial_width * 2, p.height), fill="white")
else:
latent_mask.paste(Image.open(os.path.join(mask_dir, os.path.basename(filename))).resize(
(initial_width, p.height), Image.ANTIALIAS).convert("L"), (initial_width, 0))
p.image_mask = latent_mask
p.denoising_strength = original_denoise
else:
p.width = initial_width * 2
img = Image.new("RGB", (initial_width * 2, p.height))
img.paste(p.init_images[0], (0, 0))
# img.paste(p.init_images[0], (initial_width, 0))
img.paste(loopback_image, (initial_width, 0))
p.init_images = [img]
if color_correction_enabled:
p.color_corrections = [
processing.setup_color_correction(img)]
if use_cn:
msk = []
for cn_image in cn_images:
m = Image.new("RGB", (initial_width * 2, p.height))
m.paste(Image.open(cn_image[i - 1]).resize(
(initial_width, p.height), Image.ANTIALIAS), (0, 0))
m.paste(Image.open(cn_image[i]).resize(
(initial_width, p.height), Image.ANTIALIAS), (initial_width, 0))
else:
msk = Image.new("RGB", (initial_width * 2, p.height))
msk.paste(Image.open(reference_imgs[i - 1]).resize(
(initial_width, p.height), Image.ANTIALIAS), (0, 0))
msk.paste(p.control_net_input_image, (initial_width, 0))
p.control_net_input_image = msk
# frames.append(msk)
# latent_mask = Image.new("RGB", (initial_width*2, p.height), "white")
# latent_draw = ImageDraw.Draw(latent_mask)
# latent_draw.rectangle((0,0,initial_width,p.height), fill="black")
latent_mask = Image.new(
"RGB", (initial_width * 2, p.height), "black")
if mask_dir == '':
latent_draw = ImageDraw.Draw(latent_mask)
latent_draw.rectangle(
(initial_width, 0, initial_width * 2, p.height), fill="white")
else:
latent_mask.paste(Image.open(os.path.join(mask_dir, os.path.basename(filename))).resize(
(initial_width, p.height), Image.ANTIALIAS).convert("L"), (initial_width, 0))
# p.latent_mask = latent_mask
p.image_mask = latent_mask
p.denoising_strength = original_denoise
else:
p.init_images = [p.init_images[0].resize((initial_width, p.height), Image.ANTIALIAS)]
if mask_dir == '':
latent_mask = Image.new(
"RGB", (initial_width, p.height), "white")
else:
latent_mask = Image.open(os.path.join(mask_dir, os.path.basename(filename))).resize(
(initial_width, p.height), Image.ANTIALIAS).convert("L")
# p.latent_mask = latent_mask
p.image_mask = latent_mask
p.denoising_strength = first_denoise
if use_cn:
p.control_net_input_image = [Image.open(cn_image[0]).resize((initial_width, p.height), Image.ANTIALIAS) for cn_image in cn_images]
else:
p.control_net_input_image = p.control_net_input_image.resize((initial_width, p.height), Image.ANTIALIAS)
# frames.append(p.control_net_input_image)
# if opts.img2img_color_correction:
# p.color_corrections = initial_color_corrections
if append_interrogation != "None":
p.prompt = original_prompt
if append_interrogation == "CLIP":
p.prompt += shared.interrogator.interrogate(
p.init_images[0])
elif append_interrogation == "DeepBooru":
p.prompt += deepbooru.model.tag(p.init_images[0])
if use_csv or use_txt:
p.prompt = original_prompt + prompt_list[i]
# state.job = f"Iteration {i + 1}/{loops}, batch {n + 1}/{batch_count}"
if use_cn_inpaint:
p.control_net_input_image = [p.control_net_input_image] * self.max_models
p.control_net_input_image[int(cn_inpaint_num[-1])] = {"image": p.init_images[0], "mask": p.image_mask.convert("L")}
processed = processing.process_images(p)
if initial_seed is None:
initial_seed = processed.seed
initial_info = processed.info
init_img = processed.images[0]
if(i > 0):
init_img = init_img.crop(
(initial_width, 0, initial_width * 2, p.height))
comments = {}
if len(model_hijack.comments) > 0:
for comment in model_hijack.comments:
comments[comment] = 1
info = processing.create_infotext(
p,
p.all_prompts,
p.all_seeds,
p.all_subseeds,
comments,
0,
0)
pnginfo = {}
if info is not None:
pnginfo['parameters'] = info
params = ImageSaveParams(init_img, p, filename, pnginfo)
before_image_saved_callback(params)
fullfn_without_extension, extension = os.path.splitext(
filename)
info = params.pnginfo.get('parameters', None)
def exif_bytes():
return piexif.dump({
'Exif': {
piexif.ExifIFD.UserComment: piexif.helper.UserComment.dump(info or '', encoding='unicode')
},
})
if extension.lower() == '.png':
pnginfo_data = PngImagePlugin.PngInfo()
for k, v in params.pnginfo.items():
pnginfo_data.add_text(k, str(v))
init_img.save(
os.path.join(
output_dir,
filename),
pnginfo=pnginfo_data)
elif extension.lower() in ('.jpg', '.jpeg', '.webp'):
init_img.save(os.path.join(output_dir, filename))
if opts.enable_pnginfo and info is not None:
piexif.insert(
exif_bytes(), os.path.join(
output_dir, filename))
else:
init_img.save(os.path.join(output_dir, filename))
if third_frame_image != "None":
if third_frame_image == "FirstGen" and i == 0:
third_image = init_img
third_image_index = 0
elif third_frame_image == "OriginalImg" and i == 0:
third_image = initial_img[0]
third_image_index = 0
elif third_frame_image == "Historical":
third_image = processed.images[0].crop(
(0, 0, initial_width, p.height))
third_image_index = (i - 1)
p.init_images = [init_img]
if(freeze_seed):
p.seed = processed.seed
else:
p.seed = processed.seed + 1
# p.seed = processed.seed
if i == 0:
history = init_img
# history.append(processed.images[0])
# frames.append(processed.images[0])
# grid = images.image_grid(history, rows=1)
# if opts.grid_save:
# 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)
# grids.append(grid)
# # all_images += history + frames
# all_images += history
# p.seed = p.seed+1
# if opts.return_grid:
# all_images = grids + all_images
processed = Processed(p, [], initial_seed, initial_info)
return processed
|