presencesw
commited on
Commit
•
553b69d
1
Parent(s):
ea972fb
Feat: Code to create metadata.json
Browse files
run.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import re
|
3 |
+
import shutil
|
4 |
+
import json
|
5 |
+
# from type import List
|
6 |
+
|
7 |
+
def get_list_train_test(camera_setting):
|
8 |
+
# print(camera_setting)
|
9 |
+
list_numbers = []
|
10 |
+
temp_dict = {
|
11 |
+
'train': {},
|
12 |
+
'test': {},
|
13 |
+
'confuse': {}
|
14 |
+
}
|
15 |
+
for images_name in os.listdir(os.path.join(camera_setting)):
|
16 |
+
images_name = os.path.join(camera_setting, images_name)
|
17 |
+
if os.path.isdir(images_name):
|
18 |
+
return False
|
19 |
+
images_id = images_name.split(".")[0].split("\\")[-1]
|
20 |
+
try:
|
21 |
+
numbers = re.findall(r'\d+', images_id)[0]
|
22 |
+
except:
|
23 |
+
print(images_id)
|
24 |
+
list_numbers.append(numbers)
|
25 |
+
set_numbers = set()
|
26 |
+
for number in list_numbers:
|
27 |
+
set_numbers.add(number)
|
28 |
+
# print(set_numbers)
|
29 |
+
|
30 |
+
for image_number in set_numbers:
|
31 |
+
temp_list_train = []
|
32 |
+
temp_list_test = []
|
33 |
+
for images_name in os.listdir(os.path.join(camera_setting)):
|
34 |
+
if image_number in images_name:
|
35 |
+
# print(image_number)
|
36 |
+
# print(images_name)
|
37 |
+
if images_name.endswith(".jpg"):
|
38 |
+
temp_list_test.append(images_name)
|
39 |
+
else:
|
40 |
+
temp_list_train.append(images_name)
|
41 |
+
if len(temp_list_train) == 0:
|
42 |
+
temp_dict['confuse'][image_number] = temp_list_test
|
43 |
+
elif len(temp_list_test) == 0:
|
44 |
+
temp_dict['confuse'][image_number] = temp_list_train
|
45 |
+
else:
|
46 |
+
temp_dict['train'][image_number] = temp_list_train
|
47 |
+
temp_dict['test'][image_number] = temp_list_test
|
48 |
+
return temp_dict
|
49 |
+
|
50 |
+
def check_folder(camera_setting):
|
51 |
+
list_folder = []
|
52 |
+
for item in os.listdir(camera_setting):
|
53 |
+
if os.path.isdir(os.path.join(camera_setting, item)):
|
54 |
+
list_folder.append(os.path.join(camera_setting, item))
|
55 |
+
for folder in list_folder:
|
56 |
+
files = os.listdir(folder)
|
57 |
+
for fname in files:
|
58 |
+
# copying the files to the
|
59 |
+
# destination directory
|
60 |
+
shutil.copy2(os.path.join(folder,fname), os.path.join(folder,".."))
|
61 |
+
return list_folder
|
62 |
+
|
63 |
+
def main(data_path):
|
64 |
+
map_image = dict()
|
65 |
+
map_image['train'] = {}
|
66 |
+
map_image['test'] = {}
|
67 |
+
map_image['confuse'] = {}
|
68 |
+
map_image['false'] = []
|
69 |
+
for fold_number in os.listdir(data_path):
|
70 |
+
if "." in fold_number:
|
71 |
+
continue
|
72 |
+
# print(fold_number)
|
73 |
+
for address in os.listdir(os.path.join(data_path, fold_number)):
|
74 |
+
address = os.path.join(data_path, fold_number, address)
|
75 |
+
for camera_setting in os.listdir(address):
|
76 |
+
camera_setting = os.path.join(address, camera_setting)
|
77 |
+
# print(check_folder(camera_setting=camera_setting))
|
78 |
+
temp_dict = get_list_train_test(camera_setting=camera_setting)
|
79 |
+
if temp_dict == False:
|
80 |
+
map_image['false'].append(camera_setting)
|
81 |
+
else:
|
82 |
+
map_image['train'][camera_setting] = temp_dict['train']
|
83 |
+
map_image['test'][camera_setting] = temp_dict['test']
|
84 |
+
map_image['confuse'][camera_setting] = temp_dict['confuse']
|
85 |
+
with open("metadata.json", 'w') as fout:
|
86 |
+
json_dumps_str = json.dumps(map_image, indent=4)
|
87 |
+
print(json_dumps_str, file=fout)
|
88 |
+
if __name__ == "__main__":
|
89 |
+
data_path = "data_temp"
|
90 |
+
main(data_path)
|