yasintuncerr
commited on
Commit
•
292722f
1
Parent(s):
c857fe4
utils methods included
Browse files
utils.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
############################################################################################################
|
2 |
+
## common utils
|
3 |
+
import os
|
4 |
+
import tarfile
|
5 |
+
from tqdm import tqdm
|
6 |
+
ROOT_DIR = os.path.dirname(__file__)
|
7 |
+
DATASET_DIR=os.path.join(ROOT_DIR, "dataset")
|
8 |
+
ANNOTATIONS_DIR=os.path.join(ROOT_DIR, "dataset", "annotations")
|
9 |
+
|
10 |
+
|
11 |
+
def get_supported_resolutions():
|
12 |
+
files = [file.split('_')[0] for file in os.listdir(ROOT_DIR) if file.endswith('.tar.gz') and 'annotations' not in file]
|
13 |
+
return files
|
14 |
+
|
15 |
+
def extract_tar_file(tar_path, extract_path):
|
16 |
+
with tarfile.open(tar_path, 'r:gz') as tar:
|
17 |
+
members = tar.getmembers()
|
18 |
+
for member in tqdm(members, desc=f"Extracting {os.path.basename(tar_path)}"):
|
19 |
+
tar.extract(member, path=extract_path)
|
20 |
+
|
21 |
+
def check_and_create_dir(directory):
|
22 |
+
if not os.path.exists(directory):
|
23 |
+
os.makedirs(directory)
|
24 |
+
|
25 |
+
def get_annotations_list():
|
26 |
+
annotations_dir = os.path.join(ANNOTATIONS_DIR, "single_frames", "000000", "annotations")
|
27 |
+
|
28 |
+
annotations_list = []
|
29 |
+
|
30 |
+
for file in os.listdir(annotations_dir):
|
31 |
+
if file.endswith(".json"):
|
32 |
+
annotations_list.append(file)
|
33 |
+
return annotations_list
|
34 |
+
|
35 |
+
def get_annotated_files_list():
|
36 |
+
annotated_files_dir = os.path.join(ANNOTATIONS_DIR, "single_frames")
|
37 |
+
|
38 |
+
annotated_files_list = os.listdir(annotated_files_dir)
|
39 |
+
return annotated_files_list
|
40 |
+
|
41 |
+
|
42 |
+
|
43 |
+
############################################################################################################
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
##------------------------------------------------------------------------------------------------------------
|
48 |
+
############################################################################################################
|
49 |
+
### Yolo Utils
|
50 |
+
import json
|
51 |
+
|
52 |
+
orig_width = 3840
|
53 |
+
orig_height = 2160
|
54 |
+
|
55 |
+
def extract_tar_file(tar_path, extract_path):
|
56 |
+
with tarfile.open(tar_path, 'r:gz') as tar:
|
57 |
+
members = tar.getmembers()
|
58 |
+
for member in tqdm(members, desc=f"Extracting {os.path.basename(tar_path)}"):
|
59 |
+
tar.extract(member, path=extract_path)
|
60 |
+
|
61 |
+
def normalize_coordinates(coords, orig_width, orig_height, new_width, new_height):
|
62 |
+
scale_x = new_width / orig_width
|
63 |
+
scale_y = new_height / orig_height
|
64 |
+
return [(x * scale_x / new_width, y * scale_y / new_height) for x, y in coords]
|
65 |
+
|
66 |
+
def convert_lane_annotations_to_yolo_seg_format(annotation, class_mapping, new_width, new_height, orig_width=3840, orig_height=2160):
|
67 |
+
yolo_annotations = []
|
68 |
+
|
69 |
+
for item in annotation:
|
70 |
+
properties = item.get('properties')
|
71 |
+
if not properties:
|
72 |
+
return None
|
73 |
+
|
74 |
+
obj_class = properties.get('class')
|
75 |
+
if not obj_class:
|
76 |
+
return None
|
77 |
+
|
78 |
+
if obj_class in class_mapping:
|
79 |
+
class_index = class_mapping[obj_class]
|
80 |
+
geometry = item.get('geometry')
|
81 |
+
if not geometry:
|
82 |
+
return None
|
83 |
+
|
84 |
+
coords = geometry.get('coordinates')
|
85 |
+
if not coords or not coords[0]:
|
86 |
+
return None
|
87 |
+
|
88 |
+
normalized_coords = normalize_coordinates(coords[0], orig_width, orig_height, new_width, new_height)
|
89 |
+
yolo_annotation = f"{class_index} " + " ".join([f"{x} {y}" for x, y in normalized_coords])
|
90 |
+
yolo_annotations.append(yolo_annotation)
|
91 |
+
|
92 |
+
return yolo_annotations if yolo_annotations else None
|
93 |
+
|
94 |
+
|
95 |
+
if __name__ == "__main__":
|
96 |
+
# Example usage
|
97 |
+
annotations = [
|
98 |
+
{"geometry": {"coordinates": [[[1312.5311279296875, 1204.11669921875], [1313.6014404296875, 1205.66259765625], [1352.015380859375, 1198.48486328125], [1352.5618896484375, 1197.9384765625], [1352.152099609375, 1196.845703125], [1352.015380859375, 1196.7091064453125], [1312.5311279296875, 1204.11669921875]]], "type": "Polygon"}, "properties": {"annotation_uuid": "dbf4ed67-c398-49dc-9652-672b4b6749cf", "class": "lm_dashed", "coloured": False, "MultipleLaneMarkings": "Single"}},
|
99 |
+
{"geometry": {"coordinates": [[[1199.517578125, 1223.2388916015625], [1201.5904541015625, 1224.14013671875], [1260.1224365234375, 1214.2451171875], [1260.3294677734375, 1213.2098388671875], [1259.8118896484375, 1212.588623046875], [1199.3529052734375, 1222.216552734375], [1199.517578125, 1223.2388916015625]]], "type": "Polygon"}, "properties": {"annotation_uuid": "cc809a66-815f-4b21-967a-5a613dc5b3d6", "class": "lm_dashed", "coloured": False, "MultipleLaneMarkings": "Single"}},
|
100 |
+
# Add more annotations as needed...
|
101 |
+
]
|
102 |
+
|
103 |
+
new_width = 640
|
104 |
+
new_height = 360
|
105 |
+
|
106 |
+
yolo_annotations = convert_lane_annotations_to_yolo_seg_format(annotations, new_width, new_height, orig_width, orig_height)
|
107 |
+
|
108 |
+
# Write to a text file
|
109 |
+
output_file = "annotations.txt"
|
110 |
+
with open(output_file, "w") as file:
|
111 |
+
for annotation in yolo_annotations:
|
112 |
+
file.write(f"{annotation}\n")
|