Added convert JSON image metadata files to TXT image tags files script.
Browse files- convert.py +36 -0
convert.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import tqdm
|
4 |
+
import utils
|
5 |
+
import random
|
6 |
+
import argparse
|
7 |
+
from constants import *
|
8 |
+
|
9 |
+
def parse_args():
|
10 |
+
parser = argparse.ArgumentParser(description="Convert JSON image metadata to TXT image tags.")
|
11 |
+
parser.add_argument("-n", "--no-delete", action="store_true", help="If set, won't delete the JSON image metadata files")
|
12 |
+
mutex = parser.add_mutually_exclusive_group()
|
13 |
+
mutex.add_argument("-e", "--exclude", nargs="+", help="Exclude tag groups with the specified group names, you can only set either exclude or include, but not both")
|
14 |
+
mutex.add_argument("-i", "--include", nargs="+", help="Include tag groups with the specified group names, you can only set either include or exclude, but not both")
|
15 |
+
return parser.parse_args()
|
16 |
+
|
17 |
+
def main():
|
18 |
+
args = parse_args()
|
19 |
+
print("Starting...\nGetting paths...")
|
20 |
+
image_id_image_metadata_path_tuple_dict = utils.get_image_id_image_metadata_path_tuple_dict(IMAGE_DIR)
|
21 |
+
print("Got", len(image_id_image_metadata_path_tuple_dict), "images.")
|
22 |
+
for _, metadata_path in tqdm.tqdm(image_id_image_metadata_path_tuple_dict.values(), desc="Converting"):
|
23 |
+
tags = utils.get_tags(metadata_path, args.exclude, args.include)
|
24 |
+
random.shuffle(tags)
|
25 |
+
tags_text = ", ".join(tag.replace("_", " ") for tag in tags)
|
26 |
+
with open(os.path.splitext(metadata_path)[0] + ".txt", "w", encoding="utf8") as tags_file:
|
27 |
+
tags_file.write(tags_text)
|
28 |
+
if not args.no_delete:
|
29 |
+
os.remove(metadata_path)
|
30 |
+
|
31 |
+
if __name__ == "__main__":
|
32 |
+
try:
|
33 |
+
main()
|
34 |
+
except KeyboardInterrupt:
|
35 |
+
print("\nScript interrupted by user, exiting...")
|
36 |
+
sys.exit(1)
|