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
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
- ...
'''
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
y = (box[1] + box[3]) / 2.0
w = box[2] - box[0]
h = box[3] - box[1]
x = x * dw
w = w * dw
y = y * dh
h = h * dh
kitti_xywh = "{} {} {} {} {} {}".format(0, -1, x, y, w, h)
return kitti_xywh
def get_img_size(img_path):
im = Image.open(img_path)
w = int(im.size[0])
h = int(im.size[1])
image_size = (w, h)
return image_size
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 = 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":
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":
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)
return label_list
get_txt_list(label_path)