-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_image_dict.py
45 lines (35 loc) · 1.35 KB
/
create_image_dict.py
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
"""
Returns a dict with form:
{
Plant_1(label): {image_1_name: path_to_image_1}, {image_2_name: path_to_image_2}, ..., {image_n_name: path_to_image_n},
Plant_2(label): {image_1_name: path_to_image_1}, {image_2_name: path_to_image_2}, ..., {image_n_name: path_to_image_n},
...,
Plant_n(label): {image_1_name: path_to_image_1}, {image_2_name: path_to_image_2}, ..., {image_n_name: path_to_image_n}
}
"""
import os
import sys
import json
from _config import PATH, DATA_PATH, RAW_DATA_PATH
# try to get the subfolders of the directory where data is stored
try:
plants = os.listdir(RAW_DATA_PATH)
if len(plants) == 0:
print("\nThe folder you specified in _conf.py seems empty...")
raise FileNotFoundError
# raise
except FileNotFoundError:
print("\nSomething went wrong... make sure the path you specified in _conf.py is correct...\n")
sys.quit(0)
# init empty dict
plant_dict = {}
# populate the dict
for plant_type in plants:
plant_dict[plant_type] = {}
plant_imgs = os.listdir(RAW_DATA_PATH + plant_type)
for plant_img in plant_imgs:
plant_dict[plant_type][plant_type + "_" + plant_img.replace(".png", "")] = RAW_DATA_PATH + str(plant_type) +"/" + plant_img
# write the created dict to a json file
with open(PATH + 'image_dict.json', 'w') as outfile:
json.dump(plant_dict, outfile)
print("\nImage dict created succefully at " + PATH + "image_dict.json ...\n")