Add find/replace option to Basic Caption utility
This commit is contained in:
parent
115ed35187
commit
c79a8a9409
@ -1,7 +1,7 @@
|
|||||||
import gradio as gr
|
import gradio as gr
|
||||||
from easygui import msgbox
|
from easygui import msgbox
|
||||||
import subprocess
|
import subprocess
|
||||||
from .common_gui import get_folder_path, add_pre_postfix
|
from .common_gui import get_folder_path, add_pre_postfix, find_replace
|
||||||
|
|
||||||
|
|
||||||
def caption_images(
|
def caption_images(
|
||||||
@ -10,7 +10,7 @@ def caption_images(
|
|||||||
overwrite_input,
|
overwrite_input,
|
||||||
caption_file_ext,
|
caption_file_ext,
|
||||||
prefix,
|
prefix,
|
||||||
postfix,
|
postfix, find, replace
|
||||||
):
|
):
|
||||||
# Check for images_dir_input
|
# Check for images_dir_input
|
||||||
if images_dir_input == '':
|
if images_dir_input == '':
|
||||||
@ -35,13 +35,21 @@ def caption_images(
|
|||||||
subprocess.run(run_cmd)
|
subprocess.run(run_cmd)
|
||||||
|
|
||||||
if overwrite_input:
|
if overwrite_input:
|
||||||
# Add prefix and postfix
|
if not prefix=='' or not postfix=='':
|
||||||
add_pre_postfix(
|
# Add prefix and postfix
|
||||||
folder=images_dir_input,
|
add_pre_postfix(
|
||||||
caption_file_ext=caption_file_ext,
|
folder=images_dir_input,
|
||||||
prefix=prefix,
|
caption_file_ext=caption_file_ext,
|
||||||
postfix=postfix,
|
prefix=prefix,
|
||||||
)
|
postfix=postfix,
|
||||||
|
)
|
||||||
|
if not find=='':
|
||||||
|
find_replace(
|
||||||
|
folder=images_dir_input,
|
||||||
|
caption_file_ext=caption_file_ext,
|
||||||
|
find=find,
|
||||||
|
replace=replace,
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
if not prefix == '' or not postfix == '':
|
if not prefix == '' or not postfix == '':
|
||||||
msgbox(
|
msgbox(
|
||||||
@ -73,6 +81,16 @@ def gradio_basic_caption_gui_tab():
|
|||||||
button_images_dir_input.click(
|
button_images_dir_input.click(
|
||||||
get_folder_path, outputs=images_dir_input
|
get_folder_path, outputs=images_dir_input
|
||||||
)
|
)
|
||||||
|
caption_file_ext = gr.Textbox(
|
||||||
|
label='Caption file extension',
|
||||||
|
placeholder='(Optional) Default: .caption',
|
||||||
|
interactive=True,
|
||||||
|
)
|
||||||
|
overwrite_input = gr.Checkbox(
|
||||||
|
label='Overwrite existing captions in folder',
|
||||||
|
interactive=True,
|
||||||
|
value=False,
|
||||||
|
)
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
prefix = gr.Textbox(
|
prefix = gr.Textbox(
|
||||||
label='Prefix to add to caption',
|
label='Prefix to add to caption',
|
||||||
@ -81,7 +99,7 @@ def gradio_basic_caption_gui_tab():
|
|||||||
)
|
)
|
||||||
caption_text_input = gr.Textbox(
|
caption_text_input = gr.Textbox(
|
||||||
label='Caption text',
|
label='Caption text',
|
||||||
placeholder='Eg: , by some artist. Leave empti if you just want to add pre or postfix',
|
placeholder='Eg: , by some artist. Leave empty if you just want to add pre or postfix',
|
||||||
interactive=True,
|
interactive=True,
|
||||||
)
|
)
|
||||||
postfix = gr.Textbox(
|
postfix = gr.Textbox(
|
||||||
@ -90,14 +108,14 @@ def gradio_basic_caption_gui_tab():
|
|||||||
interactive=True,
|
interactive=True,
|
||||||
)
|
)
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
overwrite_input = gr.Checkbox(
|
find = gr.Textbox(
|
||||||
label='Overwrite existing captions in folder',
|
label='Find text',
|
||||||
|
placeholder='Eg: , by some artist. Leave empty if you just want to add pre or postfix',
|
||||||
interactive=True,
|
interactive=True,
|
||||||
value=False,
|
|
||||||
)
|
)
|
||||||
caption_file_ext = gr.Textbox(
|
replace = gr.Textbox(
|
||||||
label='Caption file extension',
|
label='Replacement text',
|
||||||
placeholder='(Optional) Default: .caption',
|
placeholder='Eg: , by some artist. Leave empty if you just want to replace with nothing',
|
||||||
interactive=True,
|
interactive=True,
|
||||||
)
|
)
|
||||||
caption_button = gr.Button('Caption images')
|
caption_button = gr.Button('Caption images')
|
||||||
@ -111,5 +129,6 @@ def gradio_basic_caption_gui_tab():
|
|||||||
caption_file_ext,
|
caption_file_ext,
|
||||||
prefix,
|
prefix,
|
||||||
postfix,
|
postfix,
|
||||||
|
find, replace
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
@ -7,6 +7,15 @@ def get_dir_and_file(file_path):
|
|||||||
dir_path, file_name = os.path.split(file_path)
|
dir_path, file_name = os.path.split(file_path)
|
||||||
return (dir_path, file_name)
|
return (dir_path, file_name)
|
||||||
|
|
||||||
|
def has_ext_files(directory, extension):
|
||||||
|
# Iterate through all the files in the directory
|
||||||
|
for file in os.listdir(directory):
|
||||||
|
# If the file name ends with extension, return True
|
||||||
|
if file.endswith(extension):
|
||||||
|
return True
|
||||||
|
# If no extension files were found, return False
|
||||||
|
return False
|
||||||
|
|
||||||
def get_file_path(file_path='', defaultextension='.json', extension_name='Config files'):
|
def get_file_path(file_path='', defaultextension='.json', extension_name='Config files'):
|
||||||
current_file_path = file_path
|
current_file_path = file_path
|
||||||
# print(f'current file path: {current_file_path}')
|
# print(f'current file path: {current_file_path}')
|
||||||
@ -127,13 +136,13 @@ def get_saveasfilename_path(file_path='', extensions='*', extension_name='Config
|
|||||||
def add_pre_postfix(
|
def add_pre_postfix(
|
||||||
folder='', prefix='', postfix='', caption_file_ext='.caption'
|
folder='', prefix='', postfix='', caption_file_ext='.caption'
|
||||||
):
|
):
|
||||||
|
if not has_ext_files(folder, caption_file_ext):
|
||||||
|
msgbox(f'No files with extension {caption_file_ext} were found in {folder}...')
|
||||||
|
return
|
||||||
|
|
||||||
if prefix == '' and postfix == '':
|
if prefix == '' and postfix == '':
|
||||||
return
|
return
|
||||||
|
|
||||||
# set caption extention to default in case it was not provided
|
|
||||||
if caption_file_ext == '':
|
|
||||||
caption_file_ext = '.caption'
|
|
||||||
|
|
||||||
files = [f for f in os.listdir(folder) if f.endswith(caption_file_ext)]
|
files = [f for f in os.listdir(folder) if f.endswith(caption_file_ext)]
|
||||||
if not prefix == '':
|
if not prefix == '':
|
||||||
prefix = f'{prefix} '
|
prefix = f'{prefix} '
|
||||||
@ -147,6 +156,27 @@ def add_pre_postfix(
|
|||||||
f.seek(0, 0)
|
f.seek(0, 0)
|
||||||
f.write(f'{prefix}{content}{postfix}')
|
f.write(f'{prefix}{content}{postfix}')
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
def find_replace(
|
||||||
|
folder='', caption_file_ext='.caption', find='', replace=''
|
||||||
|
):
|
||||||
|
print('Running caption find/replace')
|
||||||
|
if not has_ext_files(folder, caption_file_ext):
|
||||||
|
msgbox(f'No files with extension {caption_file_ext} were found in {folder}...')
|
||||||
|
return
|
||||||
|
|
||||||
|
if find == '':
|
||||||
|
return
|
||||||
|
|
||||||
|
files = [f for f in os.listdir(folder) if f.endswith(caption_file_ext)]
|
||||||
|
for file in files:
|
||||||
|
with open(os.path.join(folder, file), 'r') as f:
|
||||||
|
content = f.read()
|
||||||
|
f.close
|
||||||
|
content = content.replace(find, replace)
|
||||||
|
with open(os.path.join(folder, file), 'w') as f:
|
||||||
|
f.write(content)
|
||||||
|
f.close()
|
||||||
|
|
||||||
def color_aug_changed(color_aug):
|
def color_aug_changed(color_aug):
|
||||||
if color_aug:
|
if color_aug:
|
||||||
|
Loading…
Reference in New Issue
Block a user