Skip to content

Commit 8011be3

Browse files
committed
move functions out of main body for image preprocessing for easier hijacking
1 parent c5334fc commit 8011be3

File tree

1 file changed

+93
-69
lines changed

1 file changed

+93
-69
lines changed

modules/textual_inversion/preprocess.py

Lines changed: 93 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,84 @@ def preprocess(process_src, process_dst, process_width, process_height, preproce
3535
deepbooru.release_process()
3636

3737

38+
def listfiles(dirname):
39+
return os.listdir(dirname)
40+
41+
42+
class PreprocessParams:
43+
src = None
44+
dstdir = None
45+
subindex = 0
46+
flip = False
47+
process_caption = False
48+
process_caption_deepbooru = False
49+
preprocess_txt_action = None
50+
51+
52+
def save_pic_with_caption(image, index, params: PreprocessParams, existing_caption=None):
53+
caption = ""
54+
55+
if params.process_caption:
56+
caption += shared.interrogator.generate_caption(image)
57+
58+
if params.process_caption_deepbooru:
59+
if len(caption) > 0:
60+
caption += ", "
61+
caption += deepbooru.get_tags_from_process(image)
62+
63+
filename_part = params.src
64+
filename_part = os.path.splitext(filename_part)[0]
65+
filename_part = os.path.basename(filename_part)
66+
67+
basename = f"{index:05}-{params.subindex}-{filename_part}"
68+
image.save(os.path.join(params.dstdir, f"{basename}.png"))
69+
70+
if params.preprocess_txt_action == 'prepend' and existing_caption:
71+
caption = existing_caption + ' ' + caption
72+
elif params.preprocess_txt_action == 'append' and existing_caption:
73+
caption = caption + ' ' + existing_caption
74+
elif params.preprocess_txt_action == 'copy' and existing_caption:
75+
caption = existing_caption
76+
77+
caption = caption.strip()
78+
79+
if len(caption) > 0:
80+
with open(os.path.join(params.dstdir, f"{basename}.txt"), "w", encoding="utf8") as file:
81+
file.write(caption)
82+
83+
params.subindex += 1
84+
85+
86+
def save_pic(image, index, params, existing_caption=None):
87+
save_pic_with_caption(image, index, params, existing_caption=existing_caption)
88+
89+
if params.flip:
90+
save_pic_with_caption(ImageOps.mirror(image), index, params, existing_caption=existing_caption)
91+
92+
93+
def split_pic(image, inverse_xy, width, height, overlap_ratio):
94+
if inverse_xy:
95+
from_w, from_h = image.height, image.width
96+
to_w, to_h = height, width
97+
else:
98+
from_w, from_h = image.width, image.height
99+
to_w, to_h = width, height
100+
h = from_h * to_w // from_w
101+
if inverse_xy:
102+
image = image.resize((h, to_w))
103+
else:
104+
image = image.resize((to_w, h))
105+
106+
split_count = math.ceil((h - to_h * overlap_ratio) / (to_h * (1.0 - overlap_ratio)))
107+
y_step = (h - to_h) / (split_count - 1)
108+
for i in range(split_count):
109+
y = int(y_step * i)
110+
if inverse_xy:
111+
splitted = image.crop((y, 0, y + to_h, to_w))
112+
else:
113+
splitted = image.crop((0, y, to_w, y + to_h))
114+
yield splitted
115+
38116

39117
def preprocess_work(process_src, process_dst, process_width, process_height, preprocess_txt_action, process_flip, process_split, process_caption, process_caption_deepbooru=False, split_threshold=0.5, overlap_ratio=0.2, process_focal_crop=False, process_focal_crop_face_weight=0.9, process_focal_crop_entropy_weight=0.3, process_focal_crop_edges_weight=0.5, process_focal_crop_debug=False):
40118
width = process_width
@@ -48,82 +126,28 @@ def preprocess_work(process_src, process_dst, process_width, process_height, pre
48126

49127
os.makedirs(dst, exist_ok=True)
50128

51-
files = os.listdir(src)
129+
files = listfiles(src)
52130

53131
shared.state.textinfo = "Preprocessing..."
54132
shared.state.job_count = len(files)
55133

56-
def save_pic_with_caption(image, index, existing_caption=None):
57-
caption = ""
58-
59-
if process_caption:
60-
caption += shared.interrogator.generate_caption(image)
61-
62-
if process_caption_deepbooru:
63-
if len(caption) > 0:
64-
caption += ", "
65-
caption += deepbooru.get_tags_from_process(image)
66-
67-
filename_part = filename
68-
filename_part = os.path.splitext(filename_part)[0]
69-
filename_part = os.path.basename(filename_part)
70-
71-
basename = f"{index:05}-{subindex[0]}-{filename_part}"
72-
image.save(os.path.join(dst, f"{basename}.png"))
73-
74-
if preprocess_txt_action == 'prepend' and existing_caption:
75-
caption = existing_caption + ' ' + caption
76-
elif preprocess_txt_action == 'append' and existing_caption:
77-
caption = caption + ' ' + existing_caption
78-
elif preprocess_txt_action == 'copy' and existing_caption:
79-
caption = existing_caption
80-
81-
caption = caption.strip()
82-
83-
if len(caption) > 0:
84-
with open(os.path.join(dst, f"{basename}.txt"), "w", encoding="utf8") as file:
85-
file.write(caption)
86-
87-
subindex[0] += 1
88-
89-
def save_pic(image, index, existing_caption=None):
90-
save_pic_with_caption(image, index, existing_caption=existing_caption)
91-
92-
if process_flip:
93-
save_pic_with_caption(ImageOps.mirror(image), index, existing_caption=existing_caption)
94-
95-
def split_pic(image, inverse_xy):
96-
if inverse_xy:
97-
from_w, from_h = image.height, image.width
98-
to_w, to_h = height, width
99-
else:
100-
from_w, from_h = image.width, image.height
101-
to_w, to_h = width, height
102-
h = from_h * to_w // from_w
103-
if inverse_xy:
104-
image = image.resize((h, to_w))
105-
else:
106-
image = image.resize((to_w, h))
107-
108-
split_count = math.ceil((h - to_h * overlap_ratio) / (to_h * (1.0 - overlap_ratio)))
109-
y_step = (h - to_h) / (split_count - 1)
110-
for i in range(split_count):
111-
y = int(y_step * i)
112-
if inverse_xy:
113-
splitted = image.crop((y, 0, y + to_h, to_w))
114-
else:
115-
splitted = image.crop((0, y, to_w, y + to_h))
116-
yield splitted
117-
134+
params = PreprocessParams()
135+
params.dstdir = dst
136+
params.flip = process_flip
137+
params.process_caption = process_caption
138+
params.process_caption_deepbooru = process_caption_deepbooru
139+
params.preprocess_txt_action = preprocess_txt_action
118140

119141
for index, imagefile in enumerate(tqdm.tqdm(files)):
120-
subindex = [0]
142+
params.subindex = 0
121143
filename = os.path.join(src, imagefile)
122144
try:
123145
img = Image.open(filename).convert("RGB")
124146
except Exception:
125147
continue
126148

149+
params.src = filename
150+
127151
existing_caption = None
128152
existing_caption_filename = os.path.splitext(filename)[0] + '.txt'
129153
if os.path.exists(existing_caption_filename):
@@ -143,8 +167,8 @@ def split_pic(image, inverse_xy):
143167
process_default_resize = True
144168

145169
if process_split and ratio < 1.0 and ratio <= split_threshold:
146-
for splitted in split_pic(img, inverse_xy):
147-
save_pic(splitted, index, existing_caption=existing_caption)
170+
for splitted in split_pic(img, inverse_xy, width, height, overlap_ratio):
171+
save_pic(splitted, index, params, existing_caption=existing_caption)
148172
process_default_resize = False
149173

150174
if process_focal_crop and img.height != img.width:
@@ -165,11 +189,11 @@ def split_pic(image, inverse_xy):
165189
dnn_model_path = dnn_model_path,
166190
)
167191
for focal in autocrop.crop_image(img, autocrop_settings):
168-
save_pic(focal, index, existing_caption=existing_caption)
192+
save_pic(focal, index, params, existing_caption=existing_caption)
169193
process_default_resize = False
170194

171195
if process_default_resize:
172196
img = images.resize_image(1, img, width, height)
173-
save_pic(img, index, existing_caption=existing_caption)
197+
save_pic(img, index, params, existing_caption=existing_caption)
174198

175-
shared.state.nextjob()
199+
shared.state.nextjob()

0 commit comments

Comments
 (0)