From e91d9eafe6ae926891468bdddb944211fb3d43a0 Mon Sep 17 00:00:00 2001 From: Victor Hall Date: Mon, 31 Oct 2022 22:18:23 -0400 Subject: [PATCH] file rename script --- README.MD | 8 +++- clip_rename.bat | 3 ++ doc/FILE_RENAME.md | 26 +++++++++++++ scripts/filename_replace.py | 74 +++++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 clip_rename.bat create mode 100644 doc/FILE_RENAME.md create mode 100644 scripts/filename_replace.py diff --git a/README.MD b/README.MD index b04ac2a..e9dbb80 100644 --- a/README.MD +++ b/README.MD @@ -18,7 +18,11 @@ Join the EveryDream discord here: https://discord.gg/uheqxU6sXN [Auto Captioning](./doc/AUTO_CAPTION.md) - Uses BLIP interrogation to caption images for training (includes colab notebook, needs minimal GPU). -[Training](https://github.com/victorchall/EveryDream-trainer) - Fine tuning with captioned training and ground truth data (needs 24GB GPU). +[File renaming](./doc/FILE_RENAME.md) - Simple script for replacing generic pronouns in filenames with proper names (ex "a man" -> "john doe"). + +see clip_rename.bat for an example to chain captioning and renaming together. + +[Training](https://github.com/victorchall/EveryDream-trainer) (separate repo) - Fine tuning with captioned training and ground truth data (needs 24GB GPU). ## Install @@ -50,4 +54,4 @@ Or you if you wish to configure your own venv, container/WSL, or Linux: git clone https://github.com/salesforce/BLIP scripts/BLIP -Thanks to the SalesForce team for the BLIP tool. It uses CLIP to produce sane sentences like you would expect to see in alt-text. \ No newline at end of file +Thanks to the SalesForce team for the [BLIP tool](https://github.com/salesforce/BLIP). It uses CLIP to produce sane sentences like you would expect to see in alt-text. \ No newline at end of file diff --git a/clip_rename.bat b/clip_rename.bat new file mode 100644 index 0000000..925d1aa --- /dev/null +++ b/clip_rename.bat @@ -0,0 +1,3 @@ +python scripts/auto_caption.py --q_factor 1.4 +python scripts/filename_replace.py --img_dir output --replace "rihanna" --find "a woman" +python scripts/filename_replace.py --img_dir output --replace "asap rocky" \ No newline at end of file diff --git a/doc/FILE_RENAME.md b/doc/FILE_RENAME.md new file mode 100644 index 0000000..0624535 --- /dev/null +++ b/doc/FILE_RENAME.md @@ -0,0 +1,26 @@ +# Filename Replace + +This is a very simple script to rename generic pronouns in files to proper names after using auto captioning. This script does not create copies. It renames the files in place. + +By default, it will replace "a man", "a woman", and "a person" with your supplied proper name. This works well for single subject without tweaking. + + +## Usage + + python scripts/filename_replace.py --img_dir output --replace "john doe" + +*"a man standing in a park with birds on his shoulders.jpg" +-> +"john doe standing in a park with birds on his shoulders.jpg"* + +You can chain together the auto_caption.py and file_name to help deal with multiple people in photos in a simple shell script (bash or windows .bat) with a bit of thinking about what you replace and using --fird to specify the pronoun to replace first more specifically than all three default pronouns. + + python scripts/auto_caption.py --q_factor 1.4 --img_dir input --out_dir output + python scripts/filename_replace.py --img_dir output --find "a woman" --replace "rihanna" + python scripts/filename_replace.py --img_dir output --replace "asap rocky" + +"a man and a woman standing next to each other in front of a green wall with leaves on it.webp" +-> +"asap rocky and rihanna standing next to each other in front of a green wall with leaves on it.webp" + +Renaming is nearly instant as it is just renaming the files and not using and AI models or calculations, just a dumb find and replace on the filename. \ No newline at end of file diff --git a/scripts/filename_replace.py b/scripts/filename_replace.py new file mode 100644 index 0000000..504f756 --- /dev/null +++ b/scripts/filename_replace.py @@ -0,0 +1,74 @@ +import aiofiles +import aiofiles.os +import asyncio +import sys +import os +import glob +import argparse + +def get_parser(**parser_kwargs): + parser = argparse.ArgumentParser(**parser_kwargs) + parser.add_argument( + "--img_dir", + type=str, + nargs="?", + const=True, + default="input", + help="directory with images to be renamed", + ), + parser.add_argument( + "--find", + type=str, + nargs="?", + const=True, + default="a man,a woman,a person", + help="what strings to replace, in csv format, default: 'a man,a woman,a person'", + ), + parser.add_argument( + "--replace", + type=str, + nargs="?", + required=True, + const=True, + default="filename", + help="string to replace with, ex. 'john doe'", + ), + + return parser + +def isWindows(): + return sys.platform.startswith('win') + +async def rename_files(opt): + print("go") + find_list = opt.find.split(",") + + dir_iter = await aiofiles.os.scandir(opt.img_dir) + for file in dir_iter: + # get file extension + if file.is_file() and os.path.splitext(file.name)[1] in (".jpg", ".png", ".jpeg", ".gif", ".bmp", ".webp"): + try: + for s in find_list: + if s in file.name: + new_filename = file.name.replace(s, opt.replace) + await aiofiles.os.rename(file, os.path.join(opt.img_dir, new_filename)) + except Exception as e: + print(f"error opening file: {file}") + print(f"{e}") + raise e + +if __name__ == "__main__": + parser = get_parser() + opt = parser.parse_args() + + if (isWindows()): + print("{Fore.CYAN}Windows detected, using asyncio.WindowsSelectorEventLoopPolicy{Style.RESET_ALL}") + asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) + else: + print("{Fore.CYAN}Unix detected, using default asyncio event loop policy{Style.RESET_ALL}") + import time + + s = time.perf_counter() + result = asyncio.run(rename_files(opt)) + elapsed = time.perf_counter() - s + print(f"{__file__} executed in {elapsed:0.2f} seconds.") \ No newline at end of file