KohyaSS/library/caption_gui.py

88 lines
2.5 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
2022-12-16 18:16:23 +00:00
from .common_gui import get_folder_path
2022-12-16 16:04:06 +00:00
2022-12-17 01:26:26 +00:00
def caption_images(
caption_text_input, images_dir_input, overwrite_input, caption_file_ext
):
2022-12-16 16:04:06 +00:00
# Check for caption_text_input
2022-12-17 01:26:26 +00:00
if caption_text_input == '':
msgbox('Caption text is missing...')
2022-12-16 16:04:06 +00:00
return
2022-12-17 01:26:26 +00:00
2022-12-16 16:04:06 +00:00
# Check for images_dir_input
2022-12-17 01:26:26 +00:00
if images_dir_input == '':
msgbox('Image folder is missing...')
2022-12-16 16:04:06 +00:00
return
2022-12-17 01:26:26 +00:00
print(
f'Captioning files in {images_dir_input} with {caption_text_input}...'
2022-12-17 01:26:26 +00:00
)
2022-12-16 16:04:06 +00:00
run_cmd = f'python "tools/caption.py"'
run_cmd += f' --caption_text="{caption_text_input}"'
if overwrite_input:
run_cmd += f' --overwrite'
2022-12-17 01:26:26 +00:00
if caption_file_ext != '':
2022-12-16 16:04:06 +00:00
run_cmd += f' --caption_file_ext="{caption_file_ext}"'
run_cmd += f' "{images_dir_input}"'
2022-12-17 01:26:26 +00:00
2022-12-16 16:04:06 +00:00
print(run_cmd)
2022-12-17 01:26:26 +00:00
2022-12-16 16:04:06 +00:00
# Run the command
subprocess.run(run_cmd)
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
###
2022-12-17 01:26:26 +00:00
2022-12-16 18:16:23 +00:00
def gradio_caption_gui_tab():
with gr.Tab('Captioning'):
2022-12-16 16:04:06 +00:00
gr.Markdown(
2022-12-17 01:26:26 +00:00
'This utility will allow the creation of caption files for each images in a folder.'
2022-12-16 16:04:06 +00:00
)
with gr.Row():
caption_text_input = gr.Textbox(
2022-12-17 01:26:26 +00:00
label='Caption text',
placeholder='Eg: , by some artist',
2022-12-16 16:04:06 +00:00
interactive=True,
2022-12-17 01:26:26 +00:00
)
2022-12-16 16:04:06 +00:00
overwrite_input = gr.Checkbox(
2022-12-17 01:26:26 +00:00
label='Overwrite existing captions in folder',
2022-12-16 16:04:06 +00:00
interactive=True,
2022-12-17 01:26:26 +00:00
value=False,
2022-12-16 16:04:06 +00:00
)
caption_file_ext = gr.Textbox(
2022-12-17 01:26:26 +00:00
label='Caption file extension',
placeholder='(Optional) Default: .caption',
2022-12-16 16:04:06 +00:00
interactive=True,
)
with gr.Row():
images_dir_input = gr.Textbox(
2022-12-17 01:26:26 +00:00
label='Image forder to caption',
placeholder='Directory containing the images to caption',
2022-12-16 16:04:06 +00:00
interactive=True,
)
button_images_dir_input = gr.Button(
2022-12-17 01:26:26 +00:00
'📂', elem_id='open_folder_small'
)
2022-12-16 16:04:06 +00:00
button_images_dir_input.click(
2022-12-17 01:26:26 +00:00
get_folder_path, outputs=images_dir_input
)
caption_button = gr.Button('Caption images')
caption_button.click(
caption_images,
inputs=[
caption_text_input,
images_dir_input,
overwrite_input,
caption_file_ext,
],
)