Python cvat的kitti raw data格式里的3D目标框单个tracklet_labels.xml文件和打开对应frame_list.txt文件对应点云列表解析为paddle3D训练格式多个txt的脚本
时间: 2024-05-10 09:19:19 浏览: 138
以下是一个Python脚本,可以将KITTI raw data格式中的3D目标框单个tracklet_labels.xml文件和打开对应frame_list.txt文件对应点云列表解析为paddle3D训练格式多个txt:
```python
import os
import xml.etree.ElementTree as ET
from tqdm import tqdm
# 读取frame_list.txt文件
def get_frame_list(txt_path):
frame_list = []
with open(txt_path, 'r') as f:
for line in f:
frame_list.append(line.strip())
return frame_list
# 解析tracklet_labels.xml文件
def parse_xml(xml_path):
tree = ET.parse(xml_path)
root = tree.getroot()
objects = []
for tracklet in root.findall('tracklet'):
object_dict = {}
object_dict['frame'] = int(tracklet.attrib['frame'])
object_dict['id'] = int(tracklet.attrib['id'])
for item in tracklet:
if item.tag == 'objectType':
object_dict['class'] = item.text
elif item.tag == 'h':
object_dict['h'] = float(item.text)
elif item.tag == 'w':
object_dict['w'] = float(item.text)
elif item.tag == 'l':
object_dict['l'] = float(item.text)
elif item.tag == 't':
object_dict['tx'] = float(item.attrib['x'])
object_dict['ty'] = float(item.attrib['y'])
object_dict['tz'] = float(item.attrib['z'])
elif item.tag == 'poses':
object_dict['ry'] = float(item.attrib['r_y'])
objects.append(object_dict)
return objects
# 将解析后的结果写入txt文件
def write_txt(objects, txt_path):
with open(txt_path, 'w') as f:
for obj in objects:
line = '{} {} {} {} {} {} {} {} {}\n'.format(obj['frame'], obj['tx'], obj['ty'], obj['tz'], obj['l'], obj['w'], obj['h'], obj['ry'], obj['class'])
f.write(line)
if __name__ == '__main__':
data_dir = '/path/to/kitti/raw/data'
output_dir = '/path/to/paddle3D/training/data'
sequences = ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10']
for seq in sequences:
# 获取当前序列的目录路径
seq_dir = os.path.join(data_dir, 'sequences', seq)
# 获取当前序列的点云列表
frame_list = get_frame_list(os.path.join(seq_dir, 'image_2', 'frame_list.txt'))
# 遍历当前序列的每一帧,解析对应的xml文件并将结果写入txt文件
for frame in tqdm(frame_list):
frame_idx = int(frame.split('.')[0])
xml_path = os.path.join(seq_dir, 'label_02', '{}.xml'.format(str(frame_idx).zfill(6)))
objects = parse_xml(xml_path)
txt_path = os.path.join(output_dir, 'training', seq, 'txt', '{}.txt'.format(str(frame_idx).zfill(6)))
write_txt(objects, txt_path)
```
请根据实际情况修改`data_dir`和`output_dir`变量,以及序列列表`sequences`。此外,还需要安装`xml.etree.ElementTree`、`tqdm`等Python库。
阅读全文