cwcc / run_v2.py
hieupth's picture
Feat: Update code can handle map 2 map (hdr, exterior), add readme for metadata.json format (#5)
6b57b64 verified
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)