kitti 二维目标检测数据集转yolo格式
只转标签,其他随便挪挪就行。
·
只转标签,其他随便挪挪就行
import os
import cv2
def convert_kitti_to_yolo(kitti_labels_path, images_path, yolo_labels_path, names,suffix:str=".png"):
if not os.path.exists(yolo_labels_path):
os.makedirs(yolo_labels_path)
for label_file in os.listdir(kitti_labels_path):
if label_file.endswith('.txt'):
with open(os.path.join(kitti_labels_path, label_file), 'r') as f:
lines = f.readlines()
yolo_label_content = []
image_file = label_file.replace('.txt', suffix)
image_path = os.path.join(images_path, image_file)
# 获取图片宽高
image = cv2.imread(image_path)
height, width = image.shape[:2]
for line in lines:
elements = line.strip().split()
name = elements[0]
x_min = int(elements[4])
y_min = int(elements[5])
x_max = int(elements[6])
y_max = int(elements[7])
# 查找名称对应的类别编号
class_id = names.index(name)
# 将边界框转换为YOLO格式
x_center = (x_min + x_max) / 2.0 / width
y_center = (y_min + y_max) / 2.0 / height
bbox_width = (x_max - x_min) / float(width)
bbox_height = (y_max - y_min) / float(height)
# YOLO格式:class_id x_center y_center bbox_width bbox_height
yolo_label_content.append(f"{class_id} {x_center} {y_center} {bbox_width} {bbox_height}")
# 将转换后的标签写入YOLO标签文件
yolo_label_file = os.path.join(yolo_labels_path, label_file)
print("saving:",yolo_label_file)
with open(yolo_label_file, 'w') as f:
f.write('\n'.join(yolo_label_content))
if __name__=="__main__":
labels = ['a', 'b', 'c']
img_path=r".....rgb"
label_path=r"....label"
yolo_label_path=r"output_label_path"
convert_kitti_to_yolo(label_path, img_path, yolo_label_path, labels)
更多推荐
所有评论(0)