-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerate_meta_info.py
62 lines (55 loc) · 1.88 KB
/
generate_meta_info.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
import os
import cv2
import glob
import argparse
def main(args):
txt_file = open(args.meta_info, "w")
for folder, root in zip(args.input, args.root):
img_paths = sorted(glob.glob(os.path.join(folder, "*")))
for img_path in img_paths:
status = True
if args.check:
# read the image once for check, as some images may have errors
try:
img = cv2.imread(img_path)
except (IOError, OSError) as error:
print(f"Read {img_path} error: {error}")
status = False
if img is None:
status = False
print(f"Img is None: {img_path}")
if status:
# get the relative path
img_name = os.path.relpath(img_path, root)
print(img_name)
txt_file.write(f"{img_name}\n")
if __name__ == "__main__":
"""Generate meta info (txt file) for only Ground-Truth images.
It can also generate meta info from several folders into one txt file.
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"--input",
nargs="+",
help="Input folder, can be a list",
)
parser.add_argument(
"--root",
nargs="+",
help="Folder root, should have the length as input folders",
)
parser.add_argument(
"--meta_info",
type=str,
help="txt path for meta info",
)
parser.add_argument(
"--check", action="store_true", help="Read image to check whether it is ok"
)
args = parser.parse_args()
assert len(args.input) == len(args.root), (
"Input folder and folder root should have the same length, but got "
f"{len(args.input)} and {len(args.root)}."
)
os.makedirs(os.path.dirname(args.meta_info), exist_ok=True)
main(args)