This repository has been archived by the owner on May 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 310
/
merge_all_to_metadata.py
226 lines (186 loc) · 7.34 KB
/
merge_all_to_metadata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import argparse
import json
import os
import re
from pathlib import Path
from typing import List
from tqdm import tqdm
from collections import Counter
import library.train_util as train_util
TAGS_EXT = ".txt"
CAPTION_EXT = ".caption"
PATTERN_HAIR_LENGTH = re.compile(r', (long|short|medium) hair, ')
PATTERN_HAIR_CUT = re.compile(r', (bob|hime) cut, ')
PATTERN_HAIR = re.compile(r', ([\w\-]+) hair, ')
PATTERN_WORD = re.compile(r', ([\w\-]+|hair ornament), ')
PATTERNS_REMOVE_IN_MULTI = [
PATTERN_HAIR_LENGTH,
PATTERN_HAIR_CUT,
re.compile(r', [\w\-]+ eyes, '),
re.compile(r', ([\w\-]+ sleeves|sleeveless), '),
re.compile(
r', (ponytail|braid|ahoge|twintails|[\w\-]+ bun|single hair bun|single side bun|two side up|two tails|[\w\-]+ braid|sidelocks), '),
]
CAPTION_REPLACEMENTS = [
('anime anime', 'anime'),
('young ', ''),
('anime girl', 'girl'),
('cartoon female', 'girl'),
('cartoon lady', 'girl'),
('cartoon character', 'girl'),
('cartoon woman', 'girl'),
('cartoon women', 'girls'),
('cartoon girl', 'girl'),
('anime female', 'girl'),
('anime lady', 'girl'),
('anime character', 'girl'),
('anime woman', 'girl'),
('anime women', 'girls'),
('lady', 'girl'),
('female', 'girl'),
('woman', 'girl'),
('women', 'girls'),
('people', 'girls'),
('person', 'girl'),
('a cartoon figure', 'a figure'),
('a cartoon image', 'an image'),
('a cartoon picture', 'a picture'),
('an anime cartoon image', 'an image'),
('a cartoon anime drawing', 'a drawing'),
('a cartoon drawing', 'a drawing'),
('girl girl', 'girl'),
]
def clean_tags(image_key, tags):
tags = tags.replace('^_^', '^@@@^')
tags = tags.replace('_', ' ')
tags = tags.replace('^@@@^', '^_^')
tokens = tags.split(", rating")
if len(tokens) == 1:
pass
else:
if len(tokens) > 2:
print("multiple ratings:")
print(f"{image_key} {tags}")
tags = tokens[0]
tags = ", " + tags.replace(", ", ", , ") + ", "
if 'girls' in tags or 'boys' in tags:
for pat in PATTERNS_REMOVE_IN_MULTI:
found = pat.findall(tags)
if len(found) > 1:
tags = pat.sub("", tags)
srch_hair_len = PATTERN_HAIR_LENGTH.search(tags)
if srch_hair_len:
org = srch_hair_len.group()
tags = PATTERN_HAIR_LENGTH.sub(", @@@, ", tags)
found = PATTERN_HAIR.findall(tags)
if len(found) > 1:
tags = PATTERN_HAIR.sub("", tags)
if srch_hair_len:
tags = tags.replace(", @@@, ", org)
found = PATTERN_WORD.findall(tags)
for word in found:
if re.search(f", ((\w+) )+{word}, ", tags):
tags = tags.replace(f", {word}, ", "")
tags = tags.replace(", , ", ", ")
assert tags.startswith(", ") and tags.endswith(", ")
tags = tags[2:-2]
return tags
def clean_caption(caption):
for rf, rt in CAPTION_REPLACEMENTS:
replaced = True
while replaced:
bef = caption
caption = caption.replace(rf, rt)
replaced = bef != caption
return caption
def count_files(image_paths, metadata):
counts = Counter({'_captions': 0, '_tags': 0})
for image_key in metadata:
if 'tags' not in metadata[image_key]:
counts['_tags'] += 1
if 'caption' not in metadata[image_key]:
counts['_captions'] += 1
return counts
def report_counts(counts, total_files):
for key, value in counts.items():
if value == total_files:
print(f"No {key.replace('_', '')} found for any of the {total_files} images")
elif value == 0:
print(f"All {total_files} images have {key.replace('_', '')}")
else:
print(f"{total_files - value}/{total_files} images have {key.replace('_', '')}")
def merge_metadata(image_paths, metadata, full_path):
for image_path in tqdm(image_paths):
tags_path = image_path.with_suffix(TAGS_EXT)
if not tags_path.exists():
tags_path = image_path.joinpath(TAGS_EXT)
caption_path = image_path.with_suffix(CAPTION_EXT)
if not caption_path.exists():
caption_path = image_path.joinpath(CAPTION_EXT)
image_key = str(image_path) if full_path else image_path.stem
if image_key not in metadata:
metadata[image_key] = {}
if tags_path.is_file():
tags = tags_path.read_text(encoding='utf-8').strip()
metadata[image_key]['tags'] = tags
if caption_path.is_file():
caption = caption_path.read_text(encoding='utf-8').strip()
metadata[image_key]['caption'] = caption
counts = count_files(image_paths, metadata)
report_counts(counts, len(image_paths))
return metadata
def clean_metadata(metadata):
image_keys = list(metadata.keys())
for image_key in tqdm(image_keys):
tags = metadata[image_key].get('tags')
if tags is not None:
org = tags
tags = clean_tags(image_key, tags)
metadata[image_key]['tags'] = tags
caption = metadata[image_key].get('caption')
if caption is not None:
org = caption
caption = clean_caption(caption)
metadata[image_key]['caption'] = caption
return metadata
def main(args):
assert not args.recursive or (args.recursive and args.full_path), "--recursive requires --full_path!"
train_data_dir_path = Path(args.train_data_dir)
image_paths: List[Path] = train_util.glob_images_pathlib(train_data_dir_path, args.recursive)
print(f"Found {len(image_paths)} images.")
if args.in_json is not None:
print(f"Loading existing metadata: {args.in_json}")
metadata = json.loads(Path(args.in_json).read_text(encoding='utf-8'))
print("Metadata for existing images will be overwritten")
else:
print("Creating a new metadata file")
metadata = {}
print("Merging tags and captions into metadata json.")
metadata = merge_metadata(image_paths, metadata, args.full_path)
if args.clean_caption:
print("Cleaning captions and tags.")
metadata = clean_metadata(metadata)
if args.debug:
print("Debug: image_key, tags, caption")
for image_key, data in metadata.items():
print(image_key, data['tags'], data['caption'])
print(f"Writing metadata: {args.out_json}")
Path(args.out_json).write_text(json.dumps(metadata, indent=2), encoding='utf-8')
print("Done!")
def setup_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser()
parser.add_argument("train_data_dir", type=str, help="directory for train images")
parser.add_argument("out_json", type=str, help="metadata file to output")
parser.add_argument("--in_json", type=str,
help="metadata file to input (if omitted and out_json exists, existing out_json is read)")
parser.add_argument("--full_path", action="store_true",
help="use full path as image-key in metadata (supports multiple directories)")
parser.add_argument("--recursive", action="store_true",
help="recursively search for training tags and captions in all child folders of train_data_dir")
parser.add_argument("--debug", action="store_true", help="debug mode")
parser.add_argument("--clean_caption", action="store_true", help="clean captions and tags in metadata")
return parser
if __name__ == '__main__':
parser = setup_parser()
args = parser.parse_args()
main(args)