前言
在计算机视觉领域,数据标注是模型训练中不可或缺的一部分。不同的深度学习框架和算法通常要求特定的标签格式。KITTI(Karlsruhe Institute of Technology and Toyota Technological Institute)数据集是一个广泛用于自动驾驶研究的数据集,其标签文件是以文本形式保存的,并包含有关目标位置、类别等信息。YOLO(You Only Look Once)系列的目标检测算法因其快速性和准确性而备受青睐,但其所需的标签格式与KITTI不同。因此,在使用YOLO训练KITTI数据集时,我们需要将标签从KITTI格式转换为YOLO格式。
一、KITTI 标签格式
KITTI数据集中的每个标签文件都以纯文本格式存储,每行对应一个物体,字段之间用空格分隔。每一行的格式如下:
<class> <truncated> <occluded> <alpha> <bbox_left> <bbox_top> <bbox_right> <bbox_bottom> <dim_height> <dim_width> <dim_length> <location_x> <location_y> <location_z> <rotation_y>
其中:
<class>
:物体类别(例如Car, Pedestrian等)<truncated>
:被裁剪程度(0 = non-truncated, 1 = truncated),指目标是否被图像边缘截断<occluded>
:遮挡级别(0 = fully visible, 1 = partly occluded, 2 = largely occluded, 3 = unknown)<alpha>
:观察角度<bbox_left>
,<bbox_top>
,<bbox_right>
,<bbox_bottom>
:边界框的位置(像素坐标)<dim_height>
,<dim_width>
,<dim_length>
:物体三维尺寸(米)<location_x>
,<location_y>
,<location_z>
:物体中心在相机坐标系下的位置(米)<rotation_y>
:绕Y轴旋转角度(弧度)
二、YOLO 标签格式
YOLO的标签文件也是以纯文本格式保存,但结构更为简单。对于每个物体,一行表示如下:
<class_id> <x_center> <y_center> <width> <height>
所有数值均为相对于图片宽度和高度的归一化值,范围在0到1之间。<class_id>
是整数,表示物体所属类别的索引。
三、KITTI标签格式转YOLO的TXT数据集格式代码
既然我们已知kitti与yolo数据格式,接下来就是一些逻辑来构建方法了,我也不想再解读了,我直接给出代码,如下:
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
import shutil
from tqdm import tqdm
def show_img(img):
plt.imshow(img)
plt.show()
# 转换kitti数据标签labels
def build_dir(root):
if not os.path.exists(root):
os.makedirs(root)
return root
def read_label(label_filename):
lines = [line.rstrip() for line in open(label_filename)]
labels, boxes = [], []
for line in lines:
data = line.split(" ")
label = data[0]
xmin, ymin, xmax, ymax = float(data[4]), float(data[5]), float(data[6]), float(data[7])
box2d = np.array([xmin, ymin, xmax, ymax])
labels.append(label)
boxes.append(box2d)
return labels, boxes