Commit
·
c3262a7
1
Parent(s):
6aea5a9
automatically export yolo seg dataset from base dataset files
Browse files
extract_yolo_seg_lane_marking_dataset.py
ADDED
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
import random
|
4 |
+
import argparse
|
5 |
+
import shutil
|
6 |
+
from tqdm import tqdm
|
7 |
+
import yaml
|
8 |
+
import utils
|
9 |
+
from safe_executor import SafeExecutor
|
10 |
+
|
11 |
+
class_mapping = {
|
12 |
+
"lm_dashed": 1,
|
13 |
+
"lm_solid": 0,
|
14 |
+
"lm_botts_dot": 0, # Treating as lm_solid
|
15 |
+
"lm_shaded": 0 # Treating as lm_solid
|
16 |
+
}
|
17 |
+
|
18 |
+
def extract_base_dataset(from_res):
|
19 |
+
os.system(f"python extract_base_dataset.py --from_res {from_res}")
|
20 |
+
|
21 |
+
def remove_cache_dir(cache_dir):
|
22 |
+
if os.path.exists(cache_dir):
|
23 |
+
shutil.rmtree(cache_dir)
|
24 |
+
|
25 |
+
def create_cache_dir(cache_dir):
|
26 |
+
utils.check_and_create_dir(cache_dir)
|
27 |
+
|
28 |
+
def load_annotations(file):
|
29 |
+
with open(file) as f:
|
30 |
+
return json.load(f)
|
31 |
+
|
32 |
+
def convert_and_save_annotations(annotated_files, cache_dir, from_res):
|
33 |
+
width, height = map(int, from_res.split('x'))
|
34 |
+
for file in tqdm(annotated_files, desc="Converting and saving annotations"):
|
35 |
+
base_name = os.path.basename(file)
|
36 |
+
output_file_path = os.path.join(cache_dir, f'{base_name}.txt')
|
37 |
+
|
38 |
+
lane_annotations_path = os.path.join(file, "annotations", "lane_markings.json")
|
39 |
+
|
40 |
+
try:
|
41 |
+
lane_annotations = load_annotations(lane_annotations_path)
|
42 |
+
except FileNotFoundError:
|
43 |
+
with open(output_file_path, 'w') as f:
|
44 |
+
f.write("")
|
45 |
+
continue
|
46 |
+
|
47 |
+
yolo_annotations = utils.convert_lane_annotations_to_yolo_seg_format(lane_annotations, class_mapping, width, height)
|
48 |
+
|
49 |
+
with open(output_file_path, 'w') as f:
|
50 |
+
if yolo_annotations:
|
51 |
+
for line in yolo_annotations:
|
52 |
+
f.write(f"{line}\n")
|
53 |
+
else:
|
54 |
+
# Create empty file if no annotations
|
55 |
+
f.write("")
|
56 |
+
|
57 |
+
def split_files(list_of_files, train_split=0.8):
|
58 |
+
random.shuffle(list_of_files)
|
59 |
+
split_index = int(len(list_of_files) * train_split)
|
60 |
+
return list_of_files[:split_index], list_of_files[split_index:]
|
61 |
+
|
62 |
+
def prepare_yolo_dataset(train_files, val_files, from_res):
|
63 |
+
dataset_dir = os.path.join(utils.ROOT_DIR, "dataset", f"yolo_seg_lane_{from_res}")
|
64 |
+
train_dir = os.path.join(dataset_dir, "train")
|
65 |
+
val_dir = os.path.join(dataset_dir, "val")
|
66 |
+
|
67 |
+
if os.path.exists(dataset_dir):
|
68 |
+
user_input = input(f"The dataset directory {dataset_dir} already exists. Do you want to remove it? (y/n): ")
|
69 |
+
if user_input.lower() == 'y':
|
70 |
+
shutil.rmtree(dataset_dir)
|
71 |
+
else:
|
72 |
+
print("Exiting without making changes.")
|
73 |
+
return
|
74 |
+
|
75 |
+
utils.check_and_create_dir(train_dir)
|
76 |
+
utils.check_and_create_dir(val_dir)
|
77 |
+
|
78 |
+
for file in tqdm(train_files, desc="Preparing YOLO train dataset"):
|
79 |
+
base_name = os.path.splitext(os.path.basename(file))[0]
|
80 |
+
image_file = os.path.join(utils.ROOT_DIR, "dataset", f'{from_res}_images', f'{base_name}.jpg')
|
81 |
+
if os.path.exists(image_file):
|
82 |
+
shutil.copy(os.path.join(utils.ROOT_DIR, '.cache', f'{from_res}_annotations', file), train_dir)
|
83 |
+
shutil.copy(image_file, train_dir)
|
84 |
+
|
85 |
+
for file in tqdm(val_files, desc="Preparing YOLO val dataset"):
|
86 |
+
base_name = os.path.splitext(os.path.basename(file))[0]
|
87 |
+
image_file = os.path.join(utils.ROOT_DIR, "dataset", f'{from_res}_images', f'{base_name}.jpg')
|
88 |
+
if os.path.exists(image_file):
|
89 |
+
shutil.copy(os.path.join(utils.ROOT_DIR, '.cache', f'{from_res}_annotations', file), val_dir)
|
90 |
+
shutil.copy(image_file, val_dir)
|
91 |
+
|
92 |
+
create_yaml_file(dataset_dir, train_dir, val_dir)
|
93 |
+
|
94 |
+
def create_yaml_file(dataset_dir, train_dir, val_dir):
|
95 |
+
yaml_content = {
|
96 |
+
'path': dataset_dir,
|
97 |
+
'train': 'train', # relative to 'path'
|
98 |
+
'val': 'val', # relative to 'path'
|
99 |
+
'names': {
|
100 |
+
0: 'lm_solid',
|
101 |
+
1: 'lm_dashed',
|
102 |
+
}
|
103 |
+
}
|
104 |
+
|
105 |
+
yaml_file_path = os.path.join(dataset_dir, 'dataset.yaml')
|
106 |
+
with open(yaml_file_path, 'w') as yaml_file:
|
107 |
+
yaml.dump(yaml_content, yaml_file, default_flow_style=False)
|
108 |
+
|
109 |
+
def main():
|
110 |
+
parser = argparse.ArgumentParser()
|
111 |
+
supported_resolutions = utils.get_supported_resolutions()
|
112 |
+
str_supported_resolutions = ', '.join(supported_resolutions)
|
113 |
+
parser.add_argument('--from_res', type=str, help=f'Choose available dataset: {str_supported_resolutions}', required=True)
|
114 |
+
parser.add_argument('--cache_enabled', type=bool, help='Enable caching', default=False)
|
115 |
+
args = parser.parse_args()
|
116 |
+
|
117 |
+
if args.from_res not in supported_resolutions:
|
118 |
+
print(f"Unsupported resolution. Supported resolutions are: {str_supported_resolutions}")
|
119 |
+
exit(1)
|
120 |
+
|
121 |
+
extract_base_dataset(args.from_res)
|
122 |
+
|
123 |
+
annotated_files = utils.get_annotated_files_list()
|
124 |
+
|
125 |
+
cache_dir = os.path.join(utils.ROOT_DIR, ".cache", f"{args.from_res}_annotations")
|
126 |
+
if not args.cache_enabled:
|
127 |
+
remove_cache_dir(cache_dir)
|
128 |
+
create_cache_dir(cache_dir)
|
129 |
+
|
130 |
+
paths_to_cleanup = [cache_dir, os.path.join(utils.ROOT_DIR, "dataset", f"yolo_seg_lane_{args.from_res}")]
|
131 |
+
|
132 |
+
with SafeExecutor(paths_to_cleanup):
|
133 |
+
convert_and_save_annotations(annotated_files, cache_dir, args.from_res)
|
134 |
+
|
135 |
+
list_of_files = os.listdir(cache_dir)
|
136 |
+
train_files, val_files = split_files(list_of_files)
|
137 |
+
|
138 |
+
prepare_yolo_dataset(train_files, val_files, args.from_res)
|
139 |
+
|
140 |
+
print("Annotations extracted and YOLO dataset prepared successfully")
|
141 |
+
|
142 |
+
if __name__ == "__main__":
|
143 |
+
main()
|