|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright 2021 Graviti. Licensed under MIT License. |
| 4 | +# |
| 5 | +# pylint: disable=invalid-name, missing-module-docstring |
| 6 | + |
| 7 | +"""Dataloader of UrbanObjectDetection dataset.""" |
| 8 | + |
| 9 | +import os |
| 10 | + |
| 11 | +from tensorbay.dataset import Data, Dataset |
| 12 | +from tensorbay.label import LabeledBox2D |
| 13 | + |
| 14 | +try: |
| 15 | + import xmltodict |
| 16 | +except ModuleNotFoundError: |
| 17 | + from tensorbay.opendataset._utility.mocker import xmltodict # pylint:disable=ungrouped-imports |
| 18 | + |
| 19 | +_SEGMENT_NAMES = ("train", "val", "test", "trainval") |
| 20 | +DATASET_NAME = "UrbanObjectDetection" |
| 21 | + |
| 22 | + |
| 23 | +def UrbanObjectDetection(path: str) -> Dataset: |
| 24 | + """`UrbanObjectDetection <http://www.rovit.ua.es/dataset/traffic/>`_ dataset. |
| 25 | +
|
| 26 | + The file structure should be like:: |
| 27 | +
|
| 28 | + <path> |
| 29 | + Annotations/ |
| 30 | + <image_name>.xml |
| 31 | + ... |
| 32 | + JPEGImages/ |
| 33 | + <image_name>.jpg |
| 34 | + ... |
| 35 | + ImageSets/ |
| 36 | + train.txt |
| 37 | + val.txt |
| 38 | + test.txt |
| 39 | + trainval.txt |
| 40 | + ... |
| 41 | + ... |
| 42 | + ... |
| 43 | +
|
| 44 | + Arguments: |
| 45 | + path: The root directory of the dataset. |
| 46 | +
|
| 47 | + Returns: |
| 48 | + Loaded :class: `~tensorbay.dataset.dataset.Dataset` instance. |
| 49 | +
|
| 50 | + """ |
| 51 | + root_path = os.path.abspath(os.path.expanduser(path)) |
| 52 | + annotation_path = os.path.join(root_path, "Annotations") |
| 53 | + image_path = os.path.join(root_path, "JPEGImages") |
| 54 | + main_path = os.path.join(root_path, "ImageSets") |
| 55 | + |
| 56 | + dataset = Dataset(DATASET_NAME) |
| 57 | + dataset.load_catalog(os.path.join(os.path.dirname(__file__), "catalog.json")) |
| 58 | + |
| 59 | + for segment_name in _SEGMENT_NAMES: |
| 60 | + segment = dataset.create_segment(segment_name) |
| 61 | + with open(os.path.join(main_path, f"{segment_name}.txt"), encoding="utf-8") as fp: |
| 62 | + for filename in fp: |
| 63 | + segment.append(_get_data(filename.rstrip(), image_path, annotation_path)) |
| 64 | + return dataset |
| 65 | + |
| 66 | + |
| 67 | +def _get_data(filename: str, image_path: str, annotation_path: str) -> Data: |
| 68 | + """Get all information of the datum corresponding to filename. |
| 69 | +
|
| 70 | + Arguments: |
| 71 | + filename: The filename of the data. |
| 72 | + image_path: The path of the image directory. |
| 73 | + annotation_path: The path of the annotation directory. |
| 74 | +
|
| 75 | + Returns: |
| 76 | + Data: class: `~tensorbay.dataset.data.Data` instance. |
| 77 | +
|
| 78 | + """ |
| 79 | + data = Data(os.path.join(image_path, f"{filename}.jpg")) |
| 80 | + box2d = [] |
| 81 | + with open(os.path.join(annotation_path, f"{filename}.xml"), "r", encoding="utf-8") as fp: |
| 82 | + objects = xmltodict.parse(fp.read())["annotation"]["object"] |
| 83 | + if not isinstance(objects, list): |
| 84 | + objects = [objects] |
| 85 | + for obj in objects: |
| 86 | + attributes = {} |
| 87 | + attributes["truncated"] = obj["truncated"] |
| 88 | + attributes["difficult"] = obj["difficult"] |
| 89 | + attributes["pose"] = obj["pose"] |
| 90 | + bndbox = obj["bndbox"] |
| 91 | + box2d.append( |
| 92 | + LabeledBox2D( |
| 93 | + float(bndbox["xmin"]), |
| 94 | + float(bndbox["ymin"]), |
| 95 | + float(bndbox["xmax"]), |
| 96 | + float(bndbox["ymax"]), |
| 97 | + category=obj["name"], |
| 98 | + attributes=attributes, |
| 99 | + ) |
| 100 | + ) |
| 101 | + data.label.box2d = box2d |
| 102 | + return data |
0 commit comments