KohyaSS/library/basic_caption_gui.py

141 lines
4.4 KiB
Python
Raw Normal View History

2022-12-16 16:04:06 +00:00
import gradio as gr
2022-12-16 18:16:23 +00:00
from easygui import msgbox
2022-12-16 16:04:06 +00:00
import subprocess
from .common_gui import get_folder_path, add_pre_postfix, find_replace
import os
2022-12-17 01:26:26 +00:00
2023-03-04 23:56:22 +00:00
2022-12-17 01:26:26 +00:00
def caption_images(
2023-03-15 23:31:52 +00:00
caption_text,
images_dir,
overwrite,
caption_ext,
prefix,
2023-02-06 01:07:00 +00:00
postfix,
2023-03-15 23:31:52 +00:00
find_text,
replace_text,
2022-12-17 01:26:26 +00:00
):
2023-03-15 23:31:52 +00:00
# Check for images_dir
if not images_dir:
2022-12-17 01:26:26 +00:00
msgbox('Image folder is missing...')
2022-12-16 16:04:06 +00:00
return
2023-02-06 01:07:00 +00:00
2023-03-15 23:31:52 +00:00
if not caption_ext:
msgbox('Please provide an extension for the caption files.')
return
2022-12-17 01:26:26 +00:00
2023-03-15 23:31:52 +00:00
if caption_text:
print(f'Captioning files in {images_dir} with {caption_text}...')
run_cmd = f'python "tools/caption.py"'
2023-03-15 23:31:52 +00:00
run_cmd += f' --caption_text="{caption_text}"'
if overwrite:
run_cmd += f' --overwrite'
2023-03-15 23:31:52 +00:00
if caption_ext:
run_cmd += f' --caption_file_ext="{caption_ext}"'
run_cmd += f' "{images_dir}"'
2022-12-17 01:26:26 +00:00
print(run_cmd)
2022-12-17 01:26:26 +00:00
# Run the command
2023-03-05 16:43:59 +00:00
if os.name == 'posix':
os.system(run_cmd)
else:
subprocess.run(run_cmd)
2023-03-15 23:31:52 +00:00
if overwrite:
if prefix or postfix:
# Add prefix and postfix
add_pre_postfix(
2023-03-15 23:31:52 +00:00
folder=images_dir,
caption_file_ext=caption_ext,
prefix=prefix,
postfix=postfix,
)
2023-03-15 23:31:52 +00:00
if find_text:
find_replace(
2023-03-23 02:53:21 +00:00
folder_path=images_dir,
2023-03-15 23:31:52 +00:00
caption_file_ext=caption_ext,
2023-03-23 02:53:21 +00:00
search_text=find_text,
replace_text=replace_text,
)
else:
2023-03-15 23:31:52 +00:00
if prefix or postfix:
msgbox(
'Could not modify caption files with requested change because the "Overwrite existing captions in folder" option is not selected...'
)
2022-12-17 01:26:26 +00:00
print('...captioning done')
2022-12-17 01:26:26 +00:00
2022-12-16 16:04:06 +00:00
# Gradio UI
def gradio_basic_caption_gui_tab():
with gr.Tab('Basic Captioning'):
2022-12-16 16:04:06 +00:00
gr.Markdown(
2023-03-15 23:31:52 +00:00
'This utility will allow the creation of simple caption files for each image in a folder.'
2022-12-16 16:04:06 +00:00
)
with gr.Row():
2023-03-15 23:31:52 +00:00
images_dir = gr.Textbox(
label='Image folder to caption',
placeholder='Directory containing the images to caption',
interactive=True,
)
2023-03-15 23:31:52 +00:00
folder_button = gr.Button('📂', elem_id='open_folder_small')
folder_button.click(
2023-03-04 23:56:22 +00:00
get_folder_path,
2023-03-15 23:31:52 +00:00
outputs=images_dir,
2023-03-04 23:56:22 +00:00
show_progress=False,
)
2023-03-15 23:31:52 +00:00
caption_ext = gr.Textbox(
label='Caption file extension',
2023-03-15 23:31:52 +00:00
placeholder='Extension for caption file. eg: .caption, .txt',
value='.txt',
interactive=True,
)
2023-03-15 23:31:52 +00:00
overwrite = gr.Checkbox(
label='Overwrite existing captions in folder',
interactive=True,
value=False,
)
with gr.Row():
prefix = gr.Textbox(
2023-01-01 19:14:58 +00:00
label='Prefix to add to caption',
placeholder='(Optional)',
interactive=True,
)
2023-03-15 23:31:52 +00:00
caption_text = gr.Textbox(
2022-12-17 01:26:26 +00:00
label='Caption text',
placeholder='Eg: , by some artist. Leave empty if you just want to add pre or postfix',
2022-12-16 16:04:06 +00:00
interactive=True,
2022-12-17 01:26:26 +00:00
)
postfix = gr.Textbox(
2023-01-01 19:14:58 +00:00
label='Postfix to add to caption',
placeholder='(Optional)',
interactive=True,
)
with gr.Row():
2023-03-15 23:31:52 +00:00
find_text = gr.Textbox(
label='Find text',
placeholder='Eg: , by some artist. Leave empty if you just want to add pre or postfix',
2022-12-16 16:04:06 +00:00
interactive=True,
)
2023-03-15 23:31:52 +00:00
replace_text = gr.Textbox(
label='Replacement text',
placeholder='Eg: , by some artist. Leave empty if you just want to replace with nothing',
2022-12-16 16:04:06 +00:00
interactive=True,
2023-03-20 12:47:00 +00:00
)
2023-03-15 23:31:52 +00:00
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,
2022-12-16 16:04:06 +00:00
)