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

🗺Supporting different coordinate systems when exporting data to the KITTI format #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions eval/convert_annotations_to_kitti.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"""Converts dynamic object annotations to KITTI format."""
import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), "../"))

import argparse
import glob
import json
Expand Down Expand Up @@ -120,35 +123,49 @@ def _lidar_to_camera(objects, calib):
return objects


def convert_annotation(calib_path, src_anno_pth, target_path):
def convert_annotation(calib_path, src_anno_pth, target_path, target_coordinate_system):
with open(src_anno_pth) as anno_file:
src_anno = json.load(anno_file)
vehicle, camera_name, time_str, id_ = basename(src_anno_pth.strip(".json")).split("_")
id_ = int(id_)
objects = ObjectAnnotationHandler.from_annotations(src_anno)
objects = [obj[2] for obj in objects]

# Convert objects from LIDAR to camera using calibration information
frame_time = datetime.strptime(time_str, TIME_FORMAT)
calib = load_calib_from_json(calib_path, vehicle, frame_time, camera_name)
objects = _lidar_to_camera(objects, calib)
# Write a KITTI-style annotation with obj in camera frame
target_anno = _convert_to_kitti(objects, yaw_func=lambda rot: -rot.yaw_pitch_roll[0])
if target_coordinate_system == "kitti_camera":
# Convert objects from LIDAR to camera using calibration information
frame_time = datetime.strptime(time_str, TIME_FORMAT)
calib = load_calib_from_json(calib_path, vehicle, frame_time, camera_name)
objects = _lidar_to_camera(objects, calib)
# Write a KITTI-style annotation with obj in camera frame
target_anno = _convert_to_kitti(objects, yaw_func=lambda rot: -rot.yaw_pitch_roll[0])
elif target_coordinate_system == "zenseact_lidar":
# Write a KITTI-style annotation with obj still in the original frame
target_anno = _convert_to_kitti(objects, yaw_func=lambda rot: rot.yaw_pitch_roll[0])
else:
raise ValueError("Unknown coordinate system " + target_coordinate_system)
with open(join(target_path, f"{id_:06d}.txt"), "w") as target_file:
target_file.write("\n".join(target_anno))


def _parse_args():
parser = argparse.ArgumentParser(description="Convert annotations to KITTI format")
parser.add_argument("--dataset-dir", required=True, help="Root dataset directory")
parser.add_argument("--dataset-dir", required=True, help="Root dataset directory. Assumed to contain json files with label-coordinates in the zenseact lidar coordinate system")
parser.add_argument("--target-dir", required=True, help="Output directory")
parser.add_argument("--target_coordinate_system", required=True, help="What coordinate system to convert files to")
return parser.parse_args()


def main():
args = _parse_args()
calib_path = join(args.dataset_dir, "calibration")
source_path = join(args.dataset_dir, "annotations", "dynamic_objects")

target_coordinate_system = args.target_coordinate_system
if target_coordinate_system not in ["zenseact_lidar", "kitti_camera"]:
raise ValueError("Unknown coordinate system " + target_coordinate_system)

print("WARNING: CONVERTING FROM ZENSEACT LIDAR COORDINATE FRAME TO", target_coordinate_system)

assert args.dataset_dir not in args.target_dir, "Do not write to the dataset"

print("Looking up all source annotations...")
Expand All @@ -159,7 +176,7 @@ def main():

for src_anno_pth in tqdm(source_anno_paths, desc="Converting annotations..."):
try:
convert_annotation(calib_path, src_anno_pth, args.target_dir)
convert_annotation(calib_path, src_anno_pth, args.target_dir, target_coordinate_system)
except Exception as err:
print("Failed converting annotation: ", src_anno_pth, "with error:", str(err))
raise
Expand Down