代码源自RealESRGAN
import argparse
import cv2
import glob
import os
#应该是用于生成 路径信息的txt
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__':
"""
args.meta_info:指定用于保存有效图像相对路径信息的输出文件。
args.input:包含要处理的输入文件夹路径的列表。
args.root:与输入文件夹对应的根目录,用于计算相对路径。
args.check:指定是否需要检查图像文件的有效性。
python scripts/generate_meta_info.py --input datasets_temp/inputs/inputs_temp_HR --root datasets_temp/inputs --meta_info datasets_temp/inputs/meta_info/inputs_test_temp.txt
"""
"""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='+',
default=['datasets/DF2K/DF2K_HR', 'datasets/DF2K/DF2K_multiscale'],
help='Input folder, can be a list')
parser.add_argument(
'--root',
nargs='+',
default=['datasets/DF2K', 'datasets/DF2K'],
help='Folder root, should have the length as input folders')
parser.add_argument(
'--meta_info',
type=str,
default='datasets/DF2K/meta_info/meta_info_DF2Kmultiscale.txt',
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)