Skip to content

Commit db8ed5f

Browse files
committed
Focal crop UI elements
1 parent 6629446 commit db8ed5f

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

modules/textual_inversion/preprocess.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import modules.deepbooru as deepbooru
1414

1515

16-
def preprocess(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_entropy_focus=False):
16+
def preprocess(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):
1717
try:
1818
if process_caption:
1919
shared.interrogator.load()
@@ -23,7 +23,7 @@ def preprocess(process_src, process_dst, process_width, process_height, preproce
2323
db_opts[deepbooru.OPT_INCLUDE_RANKS] = False
2424
deepbooru.create_deepbooru_process(opts.interrogate_deepbooru_score_threshold, db_opts)
2525

26-
preprocess_work(process_src, process_dst, process_width, process_height, preprocess_txt_action, process_flip, process_split, process_caption, process_caption_deepbooru, split_threshold, overlap_ratio, process_entropy_focus)
26+
preprocess_work(process_src, process_dst, process_width, process_height, preprocess_txt_action, process_flip, process_split, process_caption, process_caption_deepbooru, split_threshold, overlap_ratio, process_focal_crop, process_focal_crop_face_weight, process_focal_crop_entropy_weight, process_focal_crop_edges_weight, process_focal_crop_debug)
2727

2828
finally:
2929

@@ -35,7 +35,7 @@ def preprocess(process_src, process_dst, process_width, process_height, preproce
3535

3636

3737

38-
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_entropy_focus=False):
38+
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):
3939
width = process_width
4040
height = process_height
4141
src = os.path.abspath(process_src)
@@ -139,27 +139,27 @@ def split_pic(image, inverse_xy):
139139
ratio = (img.height * width) / (img.width * height)
140140
inverse_xy = True
141141

142-
processing_option_ran = False
142+
process_default_resize = True
143143

144144
if process_split and ratio < 1.0 and ratio <= split_threshold:
145145
for splitted in split_pic(img, inverse_xy):
146146
save_pic(splitted, index, existing_caption=existing_caption)
147-
processing_option_ran = True
147+
process_default_resize = False
148148

149149
if process_entropy_focus and img.height != img.width:
150150
autocrop_settings = autocrop.Settings(
151151
crop_width = width,
152152
crop_height = height,
153-
face_points_weight = 0.9,
154-
entropy_points_weight = 0.7,
155-
corner_points_weight = 0.5,
156-
annotate_image = False
153+
face_points_weight = process_focal_crop_face_weight,
154+
entropy_points_weight = process_focal_crop_entropy_weight,
155+
corner_points_weight = process_focal_crop_edges_weight,
156+
annotate_image = process_focal_crop_debug
157157
)
158-
focal = autocrop.crop_image(img, autocrop_settings)
159-
save_pic(focal, index, existing_caption=existing_caption)
160-
processing_option_ran = True
158+
for focal in autocrop.crop_image(img, autocrop_settings):
159+
save_pic(focal, index, existing_caption=existing_caption)
160+
process_default_resize = False
161161

162-
if not processing_option_ran:
162+
if process_default_resize:
163163
img = images.resize_image(1, img, width, height)
164164
save_pic(img, index, existing_caption=existing_caption)
165165

modules/ui.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,14 +1260,20 @@ def create_ui(wrap_gradio_gpu_call):
12601260
with gr.Row():
12611261
process_flip = gr.Checkbox(label='Create flipped copies')
12621262
process_split = gr.Checkbox(label='Split oversized images')
1263-
process_entropy_focus = gr.Checkbox(label='Create auto focal point crop')
1263+
process_focal_crop = gr.Checkbox(label='Auto focal point crop')
12641264
process_caption = gr.Checkbox(label='Use BLIP for caption')
12651265
process_caption_deepbooru = gr.Checkbox(label='Use deepbooru for caption', visible=True if cmd_opts.deepdanbooru else False)
12661266

12671267
with gr.Row(visible=False) as process_split_extra_row:
12681268
process_split_threshold = gr.Slider(label='Split image threshold', value=0.5, minimum=0.0, maximum=1.0, step=0.05)
12691269
process_overlap_ratio = gr.Slider(label='Split image overlap ratio', value=0.2, minimum=0.0, maximum=0.9, step=0.05)
12701270

1271+
with gr.Row(visible=False) as process_focal_crop_row:
1272+
process_focal_crop_face_weight = gr.Slider(label='Focal point face weight', value=0.9, minimum=0.0, maximum=1.0, step=0.05)
1273+
process_focal_crop_entropy_weight = gr.Slider(label='Focal point entropy weight', value=0.3, minimum=0.0, maximum=1.0, step=0.05)
1274+
process_focal_crop_edges_weight = gr.Slider(label='Focal point edges weight', value=0.5, minimum=0.0, maximum=1.0, step=0.05)
1275+
process_focal_crop_debug = gr.Checkbox(label='Create debug image')
1276+
12711277
with gr.Row():
12721278
with gr.Column(scale=3):
12731279
gr.HTML(value="")
@@ -1281,6 +1287,12 @@ def create_ui(wrap_gradio_gpu_call):
12811287
outputs=[process_split_extra_row],
12821288
)
12831289

1290+
process_focal_crop.change(
1291+
fn=lambda show: gr_show(show),
1292+
inputs=[process_focal_crop],
1293+
outputs=[process_focal_crop_row],
1294+
)
1295+
12841296
with gr.Tab(label="Train"):
12851297
gr.HTML(value="<p style='margin-bottom: 0.7em'>Train an embedding or Hypernetwork; you must specify a directory with a set of 1:1 ratio images <a href=\"https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Textual-Inversion\" style=\"font-weight:bold;\">[wiki]</a></p>")
12861298
with gr.Row():
@@ -1368,7 +1380,11 @@ def create_ui(wrap_gradio_gpu_call):
13681380
process_caption_deepbooru,
13691381
process_split_threshold,
13701382
process_overlap_ratio,
1371-
process_entropy_focus,
1383+
process_focal_crop,
1384+
process_focal_crop_face_weight,
1385+
process_focal_crop_entropy_weight,
1386+
process_focal_crop_edges_weight,
1387+
process_focal_crop_debug,
13721388
],
13731389
outputs=[
13741390
ti_output,

0 commit comments

Comments
 (0)