Download utils.py from 8bits-ai/ZOD-Mini-2D-Road-Scenes: direct link, hf CLI and curl.
- Browser
- Download file 5.08 kB
-
https://huggingface.co/datasets/8bits-ai/ZOD-Mini-2D-Road-Scenes/resolve/main/utils.py
- Command line
-
hf download hf://datasets/8bits-ai/ZOD-Mini-2D-Road-Scenes/utils.py
-
curl -L -o utils.py https://huggingface.co/datasets/8bits-ai/ZOD-Mini-2D-Road-Scenes/resolve/main/utils.py
5.08 kB
| ############################################################################################################ | |
| ## common utils | |
| import os | |
| import tarfile | |
| from tqdm import tqdm | |
| ROOT_DIR = os.path.dirname(__file__) | |
| DATASET_DIR=os.path.join(ROOT_DIR, "dataset") | |
| ANNOTATIONS_DIR=os.path.join(ROOT_DIR, "dataset", "annotations") | |
| def get_supported_resolutions(): | |
| files = [file.split('_')[0] for file in os.listdir(ROOT_DIR) if file.endswith('.tar.gz') and 'annotations' not in file] | |
| return files | |
| def extract_tar_file(tar_path, extract_path): | |
| with tarfile.open(tar_path, 'r:gz') as tar: | |
| members = tar.getmembers() | |
| for member in tqdm(members, desc=f"Extracting {os.path.basename(tar_path)}"): | |
| tar.extract(member, path=extract_path) | |
| def check_and_create_dir(directory): | |
| if not os.path.exists(directory): | |
| os.makedirs(directory) | |
| def get_annotations_list(): | |
| annotations_dir = os.path.join(ANNOTATIONS_DIR, "single_frames", "000000", "annotations") | |
| annotations_list = [] | |
| for file in os.listdir(annotations_dir): | |
| if file.endswith(".json"): | |
| annotations_list.append(file) | |
| return annotations_list | |
| def get_annotated_files_list(): | |
| annotated_files_dir = os.path.join(ANNOTATIONS_DIR, "single_frames") | |
| print("Annotated files directory:", annotated_files_dir) | |
| if not os.path.exists(annotated_files_dir): | |
| print("The directory does not exist.") | |
| return [] | |
| annotated_files_list = [os.path.join(annotated_files_dir, file) for file in os.listdir(annotated_files_dir)] | |
| return annotated_files_list | |
| ############################################################################################################ | |
| ##------------------------------------------------------------------------------------------------------------ | |
| ############################################################################################################ | |
| ### Yolo Utils | |
| import json | |
| orig_width = 3840 | |
| orig_height = 2160 | |
| def extract_tar_file(tar_path, extract_path): | |
| with tarfile.open(tar_path, 'r:gz') as tar: | |
| members = tar.getmembers() | |
| for member in tqdm(members, desc=f"Extracting {os.path.basename(tar_path)}"): | |
| tar.extract(member, path=extract_path) | |
| def normalize_coordinates(coords, orig_width, orig_height, new_width, new_height): | |
| scale_x = new_width / orig_width | |
| scale_y = new_height / orig_height | |
| return [(x * scale_x / new_width, y * scale_y / new_height) for x, y in coords] | |
| def convert_lane_annotations_to_yolo_seg_format(annotation, class_mapping, new_width, new_height, orig_width=3840, orig_height=2160): | |
| yolo_annotations = [] | |
| for item in annotation: | |
| properties = item.get('properties') | |
| if not properties: | |
| return None | |
| obj_class = properties.get('class') | |
| if not obj_class: | |
| return None | |
| if obj_class in class_mapping: | |
| class_index = class_mapping[obj_class] | |
| geometry = item.get('geometry') | |
| if not geometry: | |
| return None | |
| coords = geometry.get('coordinates') | |
| if not coords or not coords[0]: | |
| return None | |
| normalized_coords = normalize_coordinates(coords[0], orig_width, orig_height, new_width, new_height) | |
| yolo_annotation = f"{class_index} " + " ".join([f"{x} {y}" for x, y in normalized_coords]) | |
| yolo_annotations.append(yolo_annotation) | |
| return yolo_annotations if yolo_annotations else None | |
| if __name__ == "__main__": | |
| # Example usage | |
| annotations = [ | |
| {"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"}}, | |
| {"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"}}, | |
| # Add more annotations as needed... | |
| ] | |
| new_width = 640 | |
| new_height = 360 | |
| yolo_annotations = convert_lane_annotations_to_yolo_seg_format(annotations, new_width, new_height, orig_width, orig_height) | |
| # Write to a text file | |
| output_file = "annotations.txt" | |
| with open(output_file, "w") as file: | |
| for annotation in yolo_annotations: | |
| file.write(f"{annotation}\n") | |