file rename script

This commit is contained in:
Victor Hall 2022-10-31 22:18:23 -04:00
parent 50ffc5ed5a
commit e91d9eafe6
4 changed files with 109 additions and 2 deletions

View File

@ -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). [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 ## 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 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. 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.

3
clip_rename.bat Normal file
View File

@ -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"

26
doc/FILE_RENAME.md Normal file
View File

@ -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.

View File

@ -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.")