Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support of turn signal annotation conversion #154

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ktro2828
Copy link
Contributor

@ktro2828 ktro2828 commented Sep 19, 2024

Description

Task link: TIER IV INTERNAL LINK

Add support of converting 2D turn signal annotation in FastLabel format.

How to review

To download non-annotated t4 dataset and annotation files, please refer to above task link.

How to test

test data

test command

Reference

Notes for reviewer

We need to split annotation files into corresponding non-annotated t4 dataset with the following script:

# split_fastlabel_annotation.py

from __future__ import annotations

import argparse
from glob import glob
import json
import os
import os.path as osp


def replace(src: str, target: str, replacement: str, n_times: list[int]) -> str:
    """Replace specified character in string.

    Args:
        src (str): Source string.
        target (str): Target character.
        replacement (str): Replacement character.
        n_times (list[int]): The number of

    Returns:
        str: Replaced string.

    Examples:
        >>> src = "foo_bar_hoge_fuga_piyo"
        >>> replace(src, "_", ":", [2, 4])
        "foo_bar:hoge_fuga:piyo"
    """
    src_list = list(src)
    count = 0
    for i, char in enumerate(src_list):
        if char == target:
            count += 1
            if count in n_times:
                src_list[i] = replacement
    return "".join(src_list)


def split_annotations(annotations: list[dict]) -> dict[str, list[dict]]:
    """Split annotation files.

    Args:
        annotations (list[dict]): Root directory path of annotation files.

    Returns:
        dict[str, list[dict]]: Splitted annotations corresponding to non-annotated directories.
    """
    output: dict[str, list[dict]] = {}
    for ann in annotations:
        root_dir, *filepaths = ann["name"].split("/")
        # overwrite _ to :
        # e.g.) 2023-02-06_14_31_04_14_32_33.json -> 2023-02-06_14:31:04_14:32:33.json
        if ":" not in root_dir and "rosbag" not in root_dir:
            root_dir = replace(root_dir, "_", ":", [2, 3, 5, 6])
        ann["name"] = osp.join(root_dir, "/".join(filepaths))
        if root_dir in output:
            output[root_dir].append(ann)
        else:
            output[root_dir] = [ann]
    return output


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser()
    parser.add_argument("-i", "--input", type=str, help="Directory path of annotation files.")
    parser.add_argument(
        "-o", "--output", type=str, help="Directory path to output splitted annotation files."
    )
    return parser.parse_args()


def main() -> None:
    args = parse_args()

    json_paths = glob(osp.join(args.input, "*.json"))
    # load and store all annotations
    annotations: list[dict] = []
    for filepath in json_paths:
        with open(filepath, "r", encoding="utf-8") as f:
            ann = json.load(f)
            annotations.extend(ann)

    result = split_annotations(annotations)

    os.makedirs(args.output, exist_ok=True)
    for filename, item in result.items():
        filepath = osp.join(args.output, f"{filename}.json")
        with open(filepath, "w", encoding="utf-8") as f:
            json.dump(item, f, indent=2, ensure_ascii=False)


if __name__ == "__main__":
    main()

Usage:

python3 split_fastlabel_annotation.py -i <INPUT_ROOT> -o <OUTPUT_ROOT>

After that,

$ python3 -m perception_dataset.convert --config <CONFIG>

@ktro2828 ktro2828 force-pushed the feat/turn-signal-conversion branch from 1e5272a to bfca485 Compare September 19, 2024 07:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant