Path: blob/master/extensions-builtin/postprocessing-for-training/scripts/postprocessing_focal_crop.py
2448 views
1from modules import scripts_postprocessing, ui_components, errors2import gradio as gr34from modules.textual_inversion import autocrop567class ScriptPostprocessingFocalCrop(scripts_postprocessing.ScriptPostprocessing):8name = "Auto focal point crop"9order = 40101011def ui(self):12with ui_components.InputAccordion(False, label="Auto focal point crop") as enable:13face_weight = gr.Slider(label='Focal point face weight', value=0.9, minimum=0.0, maximum=1.0, step=0.05, elem_id="postprocess_focal_crop_face_weight")14entropy_weight = gr.Slider(label='Focal point entropy weight', value=0.15, minimum=0.0, maximum=1.0, step=0.05, elem_id="postprocess_focal_crop_entropy_weight")15edges_weight = gr.Slider(label='Focal point edges weight', value=0.5, minimum=0.0, maximum=1.0, step=0.05, elem_id="postprocess_focal_crop_edges_weight")16debug = gr.Checkbox(label='Create debug image', elem_id="train_process_focal_crop_debug")1718return {19"enable": enable,20"face_weight": face_weight,21"entropy_weight": entropy_weight,22"edges_weight": edges_weight,23"debug": debug,24}2526def process(self, pp: scripts_postprocessing.PostprocessedImage, enable, face_weight, entropy_weight, edges_weight, debug):27if not enable:28return2930if not pp.shared.target_width or not pp.shared.target_height:31return3233dnn_model_path = None34try:35dnn_model_path = autocrop.download_and_cache_models()36except Exception:37errors.report("Unable to load face detection model for auto crop selection. Falling back to lower quality haar method.", exc_info=True)3839autocrop_settings = autocrop.Settings(40crop_width=pp.shared.target_width,41crop_height=pp.shared.target_height,42face_points_weight=face_weight,43entropy_points_weight=entropy_weight,44corner_points_weight=edges_weight,45annotate_image=debug,46dnn_model_path=dnn_model_path,47)4849result, *others = autocrop.crop_image(pp.image, autocrop_settings)5051pp.image = result52pp.extra_images = [pp.create_copy(x, nametags=["focal-crop-debug"], disable_processing=True) for x in others]53545556