File size: 6,383 Bytes
6b57b64 |
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 |
import os
import re
import shutil
import json
from pathlib import Path
import argparse
# from type import List
def get_list_train_test_exterior(camera_setting):
# print(camera_setting)
list_numbers = []
data = []
confuse = []
for images_name in os.listdir(os.path.join(camera_setting)):
check_images_name = os.path.join(camera_setting, images_name)
if os.path.isdir(check_images_name):
return False
# images_id = images_name.split(".")[0].split("\\")[-1]
images_id = images_name.split(".")[0]
numbers = images_id[-4:]
if not numbers.isdigit():
numbers = images_id[-3:]
if not numbers.isdigit():
numbers = images_id.split("_")[-2]
if not numbers.isdigit():
numbers = images_id.split("_")[1].split("-")[0]
if not numbers.isdigit():
print(images_id)
list_numbers.append(numbers)
set_numbers = set()
for number in list_numbers:
set_numbers.add(number)
# print(set_numbers)
for image_number in set_numbers:
temp_list_train = []
temp_list_test = []
for images_name in os.listdir(os.path.join(camera_setting)):
if image_number in images_name:
# print(image_number)
# print(images_name)
if images_name.endswith(".jpg"):
temp_list_test.append(images_name)
else:
temp_list_train.append(images_name)
if len(temp_list_train) == 0:
confuse.append([os.path.join(camera_setting, i) for i in temp_list_test])
elif len(temp_list_test) == 0:
confuse.append([os.path.join(camera_setting, i) for i in temp_list_train])
else:
temp_dict = {}
temp_dict['input_image'] = [os.path.join(camera_setting, i) for i in temp_list_train]
temp_dict['groundtruth'] = [os.path.join(camera_setting, i) for i in temp_list_test]
data.append(temp_dict)
return {"data": data, "confuse": confuse}
def get_list_train_test_hdr(camera_setting):
data = []
confuse = []
dict_numbers = {}
for images_name in os.listdir(os.path.join(camera_setting)):
# images_name = os.path.join(camera_setting, images_name)
if os.path.isdir(os.path.join(camera_setting, images_name)):
return False
if images_name.endswith(".log"):
continue
# images_id = images_name.split(".")[0].split("\\")[-1]
images_id = images_name.split(".")[0]
numbers = images_id[-4:]
if not numbers.isdigit():
numbers = images_id[-3:]
if not numbers.isdigit():
numbers = images_id.split("_")[-2]
if not numbers.isdigit():
numbers = images_id.split("_")[1].split("-")[0]
if not numbers.isdigit():
print(images_id)
dict_numbers[int(numbers)] = images_name
# print(numbers)
# print(images_id, "\n")
dict_numbers = dict(sorted(dict_numbers.items()))
temp_list_input_image = []
temp_list_groundtruth = []
is_groundtruth = False
temp_postfix = None
for number in dict_numbers:
image = os.path.join(camera_setting, dict_numbers[number])
if not image.endswith(".jpg"):
temp_list_input_image.append(image)
is_groundtruth = False
temp_postfix = image.split(".")[1]
elif image.endswith(".jpg") and is_groundtruth == False:
if os.path.isfile(f"{image.split('.')[0]}.{temp_postfix}"):
temp_list_input_image.append(f"{image.split('.')[0]}.{temp_postfix}")
temp_list_groundtruth.append(image)
data.append(
{
"input_image": temp_list_input_image,
"groundtruth": temp_list_groundtruth
}
)
temp_list_input_image = []
temp_list_groundtruth = []
is_groundtruth = True
elif image.endswith(".jpg") and is_groundtruth == True:
confuse.append(image)
return {"data": data, "confuse": confuse}
# def extract_numbers(string):
# numbers = re.findall(r'\d+', string)
# return ''.join(numbers)
def check_folder(camera_setting):
list_folder = []
for item in os.listdir(camera_setting):
if os.path.isdir(os.path.join(camera_setting, item)):
list_folder.append(os.path.join(camera_setting, item))
for folder in list_folder:
files = os.listdir(folder)
for fname in files:
# copying the files to the
# destination directory
shutil.copy2(os.path.join(folder,fname), os.path.join(folder,".."))
return list_folder
def main(data_path):
map_image = dict()
map_image['data'] = []
map_image['confuse'] = []
map_image['false'] = []
for fold_number in os.listdir(data_path):
if "." in fold_number:
continue
# print(fold_number)
for address in os.listdir(os.path.join(data_path, fold_number)):
address = os.path.join(data_path, fold_number, address)
for camera_setting in os.listdir(address):
# print(os.path.join(address, camera_setting))
if "exterior" in camera_setting.lower():
camera_setting = os.path.join(address, camera_setting)
temp_output = get_list_train_test_exterior(camera_setting=camera_setting)
elif "hdr" in camera_setting.lower():
camera_setting = os.path.join(address, camera_setting)
temp_output = get_list_train_test_hdr(camera_setting=camera_setting)
if temp_output == False:
map_image['false'].append(camera_setting)
else:
map_image['data'] += temp_output['data']
map_image['confuse'] += temp_output['confuse']
with open(f"metadata.json", 'w') as fout:
json_dumps_str = json.dumps(map_image, indent=4)
print(json_dumps_str, file=fout)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--data_path', required=True, type=str,
help='path to process')
args = parser.parse_args()
main(args.data_path) |