Feat: Update code can handle map 2 map (hdr, exterior), add readme for metadata.json format

#5
Files changed (2) hide show
  1. README.md +14 -0
  2. run_v2.py +159 -0
README.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ The metadata.json have struct as:
2
+
3
+
4
+ ```json
5
+ {
6
+ "data": [
7
+ [{"input_image": [], "groundtruth": []}],
8
+ [{"input_image": [], "groundtruth": []}],
9
+ ...
10
+ ],
11
+ "confuse": [], # list of image path not have rigth format
12
+ "false": [], # list of folder path not have right format ex: data_temp\14\1303 Doylin Dr, Cary, NC\ex 22
13
+ }
14
+ ```
run_v2.py ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+ import shutil
4
+ import json
5
+ from pathlib import Path
6
+ import argparse
7
+ # from type import List
8
+
9
+ def get_list_train_test_exterior(camera_setting):
10
+ # print(camera_setting)
11
+ list_numbers = []
12
+ data = []
13
+ confuse = []
14
+ for images_name in os.listdir(os.path.join(camera_setting)):
15
+ check_images_name = os.path.join(camera_setting, images_name)
16
+ if os.path.isdir(check_images_name):
17
+ return False
18
+ # images_id = images_name.split(".")[0].split("\\")[-1]
19
+ images_id = images_name.split(".")[0]
20
+ numbers = images_id[-4:]
21
+ if not numbers.isdigit():
22
+ numbers = images_id[-3:]
23
+ if not numbers.isdigit():
24
+ numbers = images_id.split("_")[-2]
25
+ if not numbers.isdigit():
26
+ numbers = images_id.split("_")[1].split("-")[0]
27
+ if not numbers.isdigit():
28
+ print(images_id)
29
+ list_numbers.append(numbers)
30
+ set_numbers = set()
31
+ for number in list_numbers:
32
+ set_numbers.add(number)
33
+ # print(set_numbers)
34
+
35
+ for image_number in set_numbers:
36
+ temp_list_train = []
37
+ temp_list_test = []
38
+ for images_name in os.listdir(os.path.join(camera_setting)):
39
+ if image_number in images_name:
40
+ # print(image_number)
41
+ # print(images_name)
42
+ if images_name.endswith(".jpg"):
43
+ temp_list_test.append(images_name)
44
+ else:
45
+ temp_list_train.append(images_name)
46
+ if len(temp_list_train) == 0:
47
+ confuse.append([os.path.join(camera_setting, i) for i in temp_list_test])
48
+ elif len(temp_list_test) == 0:
49
+ confuse.append([os.path.join(camera_setting, i) for i in temp_list_train])
50
+ else:
51
+ temp_dict = {}
52
+ temp_dict['input_image'] = [os.path.join(camera_setting, i) for i in temp_list_train]
53
+ temp_dict['groundtruth'] = [os.path.join(camera_setting, i) for i in temp_list_test]
54
+ data.append(temp_dict)
55
+ return {"data": data, "confuse": confuse}
56
+
57
+ def get_list_train_test_hdr(camera_setting):
58
+ data = []
59
+ confuse = []
60
+ dict_numbers = {}
61
+ for images_name in os.listdir(os.path.join(camera_setting)):
62
+ # images_name = os.path.join(camera_setting, images_name)
63
+ if os.path.isdir(os.path.join(camera_setting, images_name)):
64
+ return False
65
+ if images_name.endswith(".log"):
66
+ continue
67
+ # images_id = images_name.split(".")[0].split("\\")[-1]
68
+ images_id = images_name.split(".")[0]
69
+ numbers = images_id[-4:]
70
+ if not numbers.isdigit():
71
+ numbers = images_id[-3:]
72
+ if not numbers.isdigit():
73
+ numbers = images_id.split("_")[-2]
74
+ if not numbers.isdigit():
75
+ numbers = images_id.split("_")[1].split("-")[0]
76
+ if not numbers.isdigit():
77
+ print(images_id)
78
+ dict_numbers[int(numbers)] = images_name
79
+ # print(numbers)
80
+ # print(images_id, "\n")
81
+ dict_numbers = dict(sorted(dict_numbers.items()))
82
+ temp_list_input_image = []
83
+ temp_list_groundtruth = []
84
+ is_groundtruth = False
85
+ temp_postfix = None
86
+ for number in dict_numbers:
87
+ image = os.path.join(camera_setting, dict_numbers[number])
88
+ if not image.endswith(".jpg"):
89
+ temp_list_input_image.append(image)
90
+ is_groundtruth = False
91
+ temp_postfix = image.split(".")[1]
92
+ elif image.endswith(".jpg") and is_groundtruth == False:
93
+ if os.path.isfile(f"{image.split('.')[0]}.{temp_postfix}"):
94
+ temp_list_input_image.append(f"{image.split('.')[0]}.{temp_postfix}")
95
+ temp_list_groundtruth.append(image)
96
+ data.append(
97
+ {
98
+ "input_image": temp_list_input_image,
99
+ "groundtruth": temp_list_groundtruth
100
+ }
101
+ )
102
+ temp_list_input_image = []
103
+ temp_list_groundtruth = []
104
+ is_groundtruth = True
105
+ elif image.endswith(".jpg") and is_groundtruth == True:
106
+ confuse.append(image)
107
+ return {"data": data, "confuse": confuse}
108
+
109
+
110
+ # def extract_numbers(string):
111
+ # numbers = re.findall(r'\d+', string)
112
+ # return ''.join(numbers)
113
+
114
+ def check_folder(camera_setting):
115
+ list_folder = []
116
+ for item in os.listdir(camera_setting):
117
+ if os.path.isdir(os.path.join(camera_setting, item)):
118
+ list_folder.append(os.path.join(camera_setting, item))
119
+ for folder in list_folder:
120
+ files = os.listdir(folder)
121
+ for fname in files:
122
+ # copying the files to the
123
+ # destination directory
124
+ shutil.copy2(os.path.join(folder,fname), os.path.join(folder,".."))
125
+ return list_folder
126
+
127
+ def main(data_path):
128
+ map_image = dict()
129
+ map_image['data'] = []
130
+ map_image['confuse'] = []
131
+ map_image['false'] = []
132
+ for fold_number in os.listdir(data_path):
133
+ if "." in fold_number:
134
+ continue
135
+ # print(fold_number)
136
+ for address in os.listdir(os.path.join(data_path, fold_number)):
137
+ address = os.path.join(data_path, fold_number, address)
138
+ for camera_setting in os.listdir(address):
139
+ # print(os.path.join(address, camera_setting))
140
+ if "exterior" in camera_setting.lower():
141
+ camera_setting = os.path.join(address, camera_setting)
142
+ temp_output = get_list_train_test_exterior(camera_setting=camera_setting)
143
+ elif "hdr" in camera_setting.lower():
144
+ camera_setting = os.path.join(address, camera_setting)
145
+ temp_output = get_list_train_test_hdr(camera_setting=camera_setting)
146
+ if temp_output == False:
147
+ map_image['false'].append(camera_setting)
148
+ else:
149
+ map_image['data'] += temp_output['data']
150
+ map_image['confuse'] += temp_output['confuse']
151
+ with open(f"metadata.json", 'w') as fout:
152
+ json_dumps_str = json.dumps(map_image, indent=4)
153
+ print(json_dumps_str, file=fout)
154
+ if __name__ == "__main__":
155
+ parser = argparse.ArgumentParser()
156
+ parser.add_argument('--data_path', required=True, type=str,
157
+ help='path to process')
158
+ args = parser.parse_args()
159
+ main(args.data_path)