将CVAT点云格式标注格式由datumaro转换为kitti格式
依赖json与os
输入:default.json
输出每个点云对应一个标签
参考:3D点云目标检测数据集标注工具 保姆级教程——CVAT (附json转kitti代码)_3d点云标注工具-CSDN博客
import json
import osdef json_to_kitti(json_path, output_dir):with open(json_path, 'r') as f:data = json.load(f)labels = data['categories']['label']['labels']os.makedirs(output_dir, exist_ok=True)# 遍历每一帧for item in data['items']:item_id = item['id'] # 使用 JSON 中的 'id' 值annotations = item['annotations']# 输出 KITTI 格式文件的路径,使用 'id' 命名output_path = f"{output_dir}/{item_id}.txt"with open(output_path, 'w') as f_out:# 遍历每个标注for annotation in annotations:label_id = annotation['label_id']label_name = labels[label_id]['name']# 提取 3D 立方体信息position = annotation['position']rotation = annotation['rotation']scale = annotation['scale']# KITTI 格式字段truncated = 0 # 默认为 0,因为未提供截断信息occluded = 1 if annotation['attributes']['occluded'] else 0alpha = rotation[2] # 使用 Z 轴的旋转角作为方向角bbox_left = 0.0 # 2D 边界框位置,点云标注中通常为 0bbox_top = 0.0bbox_right = 0.0bbox_bottom = 0.0height = scale[2] # 物体高度width = scale[0] # 物体宽度length = scale[1] # 物体长度x = position[0] # 物体在相机坐标系中的 x 坐标y = position[1] # 物体在相机坐标系中的 y 坐标z = position[2] # 物体在相机坐标系中的 z 坐标rotation_y = rotation[2] # KITTI 中物体绕 Y 轴的旋转角度# 将数据写入到 KITTI 格式文件f_out.write(f"{label_name} {truncated} {occluded} {alpha} "f"{bbox_left} {bbox_top} {bbox_right} {bbox_bottom} "f"{height} {width} {length} {x} {y} {z} {rotation_y}\n")json_to_kitti('/home/wisdom/pyprojects/dataset/uat1/annotations/default.json', '/home/wisdom/pyprojects/dataset/uat1/annotations/kitti_labels')