KITTI Detection dataset whose format is letf_top_right_bottom to JDE normalied xc_yc_w_h

本文介绍了如何将KITTI Detection数据集转换为适用于JDE(一种注重精度与速度的跟踪算法)的标准化格式,包括筛选标签和调整图像尺寸,以适应JDE的标注需求。

Transform KITTI detection to normalized format which is adapt to JDE

  • 本质上就是 txt 版本的yolo 格式
  • JDE 方法是当前的主流,类似于SOT的Siamese,平衡精度与速度
  • 本代码是KITTIDetection数据集的转换,如要改成KITTI Tracking则需要改变图像尺寸以及筛选标签
from PIL import Image
from glob2 import glob
import os

# 测试数据集中某张图片的尺寸,KITTI的检测数据集尺寸为 1224x370
detection_kitti_imtemp = r"E:\8_DataSet\KITTI_detection\VOCKitti\JPEGImages\000000.png"

# 原始数据标签存放文件夹
'''
training/label_2
	- 000000.txt
		person x x x left top right bottom x ...
		car x x x left top right bottom x ...
		...
	- 000001.txt
	- ...
'''
label_path = r"E:\8_DataSet\KITTI_detection\KITTIObjectdata_object_label_2\training\label_2"

dst_label_path_human = r"E:\8_DataSet\KITTI_detection\KITTIObjectdata_object_label_2\training\label_2_jde_with_ids_human"
dst_label_path_car = r"E:\8_DataSet\KITTI_detection\KITTIObjectdata_object_label_2\training\label_2_jde_with_ids_car"
'''
training/label_2_jde_with_ids_car
	- 000000.txt
		0 -1 xc yc width height
		...
	- 000001.txt
	- ...
'''

# kitti_xywh = "{} {} {} {} {} {}".format(0, -1, x, )

# left_top right_bottom to normalized center x, y and width, height
def convert(size, box):
    dw = 1. / size[0]
    dh = 1. / size[1]
    for _, item in enumerate(box):
        box[_] = float(item)
    x = (box[0] + box[2]) / 2.0  # (x1+x2)/ 2
    y = (box[1] + box[3]) / 2.0  # (y1+y2)/ 2
    w = box[2] - box[0]  # x2 - x1 or l-r = w
    h = box[3] - box[1]  # y2 - y1 or b-t = h
    x = x * dw
    w = w * dw
    y = y * dh
    h = h * dh
    kitti_xywh = "{} {} {} {} {} {}".format(0, -1, x, y, w, h)
    # xywh = (x, y, w, h)
    return kitti_xywh


def get_img_size(img_path):
    # img_path = detection_kitti_imtemp
    im = Image.open(img_path)
    w = int(im.size[0])
    h = int(im.size[1])
    image_size = (w, h)
    # print("Image Size: width->{}, height->{}".format(image_size[0], image_size[1]))
    return image_size

# 1223, 370
image_size_det = get_img_size(img_path=detection_kitti_imtemp)


def write_txt_file(xywh, dst_directory, file_name):
    path = os.path.join(dst_directory, file_name)
    with open(path, "a") as f:
        f.write(xywh)
        f.write("\n")


def get_txt_list(label_path):
    # label_list = os.listdir(label_path)

    label_list = glob(label_path + "/*.txt")
    for txt_file in label_list:
        txt_file_name = os.path.split(txt_file)[-1]
        with open(txt_file, "r") as f:
            lines = f.readlines()
            for line in lines:
                line = line.split(" ")
                if line[0] == "person": # 筛选出标签为 person的bbox并正则化
                    bbox = line[4:8]
                    n_xywh = convert(image_size_det, bbox)
                    write_txt_file(xywh=n_xywh, dst_directory=dst_label_path_human, file_name=txt_file_name)
                if line[0] == "car": # 筛选出标签为 car 的bbox并正则化
                    bbox = line[4:8]
                    n_xywh = convert(image_size_det, bbox)
                    write_txt_file(xywh=n_xywh, dst_directory=dst_label_path_car, file_name=txt_file_name)
                # print(line)
        # print 和 break 为测试专用
        # break

    return label_list


get_txt_list(label_path)
# label_list = get_txt_list(label_path=label_path)
# for txt_file in label_list:
#     pass

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值