| | import gradio as gr
|
| | import subprocess
|
| | from .common_gui import (
|
| | get_folder_path,
|
| | add_pre_postfix,
|
| | find_replace,
|
| | scriptdir,
|
| | list_dirs,
|
| | setup_environment,
|
| | )
|
| | import os
|
| | import sys
|
| |
|
| | from .custom_logging import setup_logging
|
| |
|
| |
|
| | log = setup_logging()
|
| |
|
| | PYTHON = sys.executable
|
| |
|
| |
|
| | def caption_images(
|
| | caption_text: str,
|
| | images_dir: str,
|
| | overwrite: bool,
|
| | caption_ext: str,
|
| | prefix: str,
|
| | postfix: str,
|
| | find_text: str,
|
| | replace_text: str,
|
| | ):
|
| | """
|
| | Captions images in a given directory with a given caption text.
|
| |
|
| | Args:
|
| | caption_text (str): The text to be used as the caption.
|
| | images_dir (str): The directory containing the images to be captioned.
|
| | overwrite (bool): Whether to overwrite existing captions.
|
| | caption_ext (str): The file extension for the caption files.
|
| | prefix (str): Text to be added before the caption text.
|
| | postfix (str): Text to be added after the caption text.
|
| | find_text (str): Text to be replaced in the caption files.
|
| | replace_text (str): Text to replace the found text in the caption files.
|
| |
|
| | Returns:
|
| | None
|
| | """
|
| |
|
| | missing_parameters = []
|
| | if not images_dir:
|
| | missing_parameters.append("image directory")
|
| | if not caption_ext:
|
| | missing_parameters.append("caption file extension")
|
| |
|
| | if missing_parameters:
|
| | log.info(
|
| | "The following parameter(s) are missing: {}. "
|
| | "Please provide these to proceed with captioning the images.".format(", ".join(missing_parameters))
|
| | )
|
| | return
|
| |
|
| |
|
| | if caption_text:
|
| | log.info(f"Captioning files in {images_dir} with {caption_text}...")
|
| |
|
| |
|
| | run_cmd = [
|
| | rf"{PYTHON}",
|
| | rf"{scriptdir}/tools/caption.py",
|
| | "--caption_text",
|
| | caption_text,
|
| | ]
|
| |
|
| |
|
| | if overwrite:
|
| | run_cmd.append("--overwrite")
|
| | if caption_ext:
|
| | run_cmd.append("--caption_file_ext")
|
| | run_cmd.append(caption_ext)
|
| |
|
| | run_cmd.append(rf"{images_dir}")
|
| |
|
| |
|
| | command_to_run = " ".join(run_cmd)
|
| | log.info(f"Executing command: {command_to_run}")
|
| |
|
| |
|
| | env = setup_environment()
|
| |
|
| |
|
| | subprocess.run(run_cmd, env=env, shell=False)
|
| |
|
| |
|
| | if overwrite:
|
| |
|
| | if prefix or postfix or find_text:
|
| |
|
| | add_pre_postfix(
|
| | folder=images_dir,
|
| | caption_file_ext=caption_ext,
|
| | prefix=prefix,
|
| | postfix=postfix,
|
| | )
|
| |
|
| | if find_text and replace_text:
|
| | find_replace(
|
| | folder_path=images_dir,
|
| | caption_file_ext=caption_ext,
|
| | search_text=find_text,
|
| | replace_text=replace_text,
|
| | )
|
| | else:
|
| |
|
| | if prefix or postfix:
|
| | log.info(
|
| | 'Could not modify caption files with requested change because the "Overwrite existing captions in folder" option is not selected.'
|
| | )
|
| |
|
| |
|
| | log.info("Captioning done.")
|
| |
|
| |
|
| |
|
| | def gradio_basic_caption_gui_tab(headless=False, default_images_dir=None):
|
| | """
|
| | Creates a Gradio tab for basic image captioning.
|
| |
|
| | Args:
|
| | headless (bool, optional): If True, the GUI will be headless (no visible elements). Defaults to False.
|
| | default_images_dir (str, optional): The default directory to use for image selection. If not provided,
|
| | it defaults to the 'data' directory in the script directory.
|
| |
|
| | Returns:
|
| | None
|
| | """
|
| | from .common_gui import create_refresh_button
|
| |
|
| |
|
| | default_images_dir = (
|
| | default_images_dir
|
| | if default_images_dir is not None
|
| | else os.path.join(scriptdir, "data")
|
| | )
|
| | current_images_dir = default_images_dir
|
| |
|
| |
|
| | def list_images_dirs(path):
|
| | """
|
| | Lists directories within a specified path and updates the current image directory.
|
| |
|
| | Parameters:
|
| | path (str): The directory path to list image directories from.
|
| |
|
| | Returns:
|
| | list: A list of directories within the specified path.
|
| | """
|
| |
|
| | nonlocal current_images_dir
|
| | current_images_dir = path
|
| | return list(list_dirs(path))
|
| |
|
| |
|
| | with gr.Tab("Basic Captioning"):
|
| |
|
| | gr.Markdown(
|
| | "This utility allows you to create simple caption files for each image in a folder."
|
| | )
|
| |
|
| | with gr.Group(), gr.Row():
|
| |
|
| | images_dir = gr.Dropdown(
|
| | label="Image folder to caption (containing the images to caption)",
|
| | choices=[""] + list_images_dirs(default_images_dir),
|
| | value="",
|
| | interactive=True,
|
| | allow_custom_value=True,
|
| | )
|
| |
|
| | create_refresh_button(
|
| | images_dir,
|
| | lambda: None,
|
| | lambda: {"choices": list_images_dirs(current_images_dir)},
|
| | "open_folder_small",
|
| | )
|
| |
|
| | folder_button = gr.Button(
|
| | "📂",
|
| | elem_id="open_folder_small",
|
| | elem_classes=["tool"],
|
| | visible=(not headless),
|
| | )
|
| |
|
| | folder_button.click(
|
| | get_folder_path,
|
| | outputs=images_dir,
|
| | show_progress=False,
|
| | )
|
| |
|
| | caption_ext = gr.Dropdown(
|
| | label="Caption file extension",
|
| | choices=[".cap", ".caption", ".txt"],
|
| | value=".txt",
|
| | interactive=True,
|
| | allow_custom_value=True,
|
| | )
|
| |
|
| | overwrite = gr.Checkbox(
|
| | label="Overwrite existing captions in folder",
|
| | interactive=True,
|
| | value=False,
|
| | )
|
| |
|
| | with gr.Row():
|
| |
|
| | prefix = gr.Textbox(
|
| | label="Prefix to add to caption",
|
| | placeholder="(Optional)",
|
| | interactive=True,
|
| | )
|
| |
|
| | caption_text = gr.Textbox(
|
| | label="Caption text",
|
| | placeholder='e.g., "by some artist". Leave empty if you only want to add a prefix or postfix.',
|
| | interactive=True,
|
| | lines=2,
|
| | )
|
| |
|
| | postfix = gr.Textbox(
|
| | label="Postfix to add to caption",
|
| | placeholder="(Optional)",
|
| | interactive=True,
|
| | )
|
| |
|
| | with gr.Group(), gr.Row():
|
| |
|
| | find_text = gr.Textbox(
|
| | label="Find text",
|
| | placeholder='e.g., "by some artist". Leave empty if you only want to add a prefix or postfix.',
|
| | interactive=True,
|
| | lines=2,
|
| | )
|
| |
|
| | replace_text = gr.Textbox(
|
| | label="Replacement text",
|
| | placeholder='e.g., "by some artist". Leave empty if you want to replace with nothing.',
|
| | interactive=True,
|
| | lines=2,
|
| | )
|
| |
|
| | caption_button = gr.Button("Caption images")
|
| |
|
| | caption_button.click(
|
| | caption_images,
|
| | inputs=[
|
| | caption_text,
|
| | images_dir,
|
| | overwrite,
|
| | caption_ext,
|
| | prefix,
|
| | postfix,
|
| | find_text,
|
| | replace_text,
|
| | ],
|
| | show_progress=False,
|
| | )
|
| |
|
| |
|
| | images_dir.change(
|
| | fn=lambda path: gr.Dropdown(choices=[""] + list_images_dirs(path)),
|
| | inputs=images_dir,
|
| | outputs=images_dir,
|
| | show_progress=False,
|
| | )
|
| |
|