Python cvat的kitti raw data格式里的3D目标框单个tracklet_labels.xml文件和打开对应frame_list.txt文件对应点云列表解析为paddle3D训练格式多个txt的脚本
时间: 2024-05-04 07:21:19 浏览: 119
以下是Python实现的脚本,用于将Kitti Raw Data格式中的3D目标框转换为Paddle3D训练格式的多个txt文件:
```python
import os
import xml.etree.ElementTree as ET
import numpy as np
def parse_tracklet_labels(tracklet_labels_path):
tree = ET.parse(tracklet_labels_path)
root = tree.getroot()
tracklets = []
for tracklet in root.iter('tracklet'):
tracklet_id = int(tracklet.attrib['id'])
for object3d in tracklet.iter('object3d'):
frame_id = int(object3d.attrib['frame'])
pose = object3d.attrib['pose']
dimensions = [float(x) for x in object3d.attrib['dimensions'].split()]
location = [float(x) for x in object3d.attrib['location'].split()]
rotation_y = float(object3d.attrib['rotation_y'])
tracklets.append((tracklet_id, frame_id, pose, dimensions, location, rotation_y))
return tracklets
def parse_frame_list(frame_list_path):
with open(frame_list_path) as f:
frame_list = [line.strip() for line in f]
return frame_list
def convert_to_paddle3d_format(tracklets, frame_list, output_dir):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for frame_id, frame in enumerate(frame_list):
frame_path = os.path.join(output_dir, f'{frame_id:06d}.txt')
with open(frame_path, 'w') as f:
for tracklet_id, _, pose, dimensions, location, rotation_y in tracklets:
if frame_id < tracklet_id or frame_id > tracklet_id + len(tracklets) - 1:
continue
tracklet_frame_id = frame_id - tracklet_id
if tracklet_frame_id < 0 or tracklet_frame_id >= len(pose):
continue
f.write(f'{tracklet_id} {location[0]} {location[1]} {location[2]} {dimensions[0]} {dimensions[1]} {dimensions[2]} {rotation_y}\n')
```
运行以上脚本,将输入参数传入即可:
```python
tracklet_labels_path = '/path/to/tracklet_labels.xml'
frame_list_path = '/path/to/frame_list.txt'
output_dir = '/path/to/output/dir'
tracklets = parse_tracklet_labels(tracklet_labels_path)
frame_list = parse_frame_list(frame_list_path)
convert_to_paddle3d_format(tracklets, frame_list, output_dir)
```
阅读全文