MOT20数据集的详细介绍可看另外一篇博客:点击跳转
import os
import argparse
import json
import shutil
from tqdm import tqdm
parser = argparse.ArgumentParser("import args")
parser.add_argument("--motPaht",default=r'.\MOT20\test\MOT20-04\gt\gt.txt',type=str,help='MOT标签路径')
parser.add_argument('--out_path',default=r'.\MOT20\test\MOT20-04\gt_json',type= str,help="YOLO标签文件夹")
parser.add_argument('--img_path',default=r'.\MOT20\test\MOT20-04\img1',type= str,help="YOLO标签文件夹")
parser.add_argument("--img_size",default=[1920,1080],type=list,help='图片尺寸,宽、高')
parser.add_argument("--step_frame",default=5,type=int,help='间隔取帧')
args = parser.parse_args()
if not os.path.exists(args.out_path):
os.makedirs(args.out_path)
def main():
data = open(args.motPaht,"r",encoding="utf-8")
lines = data.readlines()
currenImg = 1
labels = dict()
shapes = []
n_ = len(lines)
labels_all = dict()
for line in lines:
lineList = line.split(",")
shape = dict()
n_ -= 1
if lineList[0] in labels_all:
if float(lineList[6])==1.0 and float(lineList[7])== 1.0 and float(lineList[-1])> 0.4:
shapes = labels_all[lineList[0]]
xmin = float(lineList[2])
ymin = float(lineList[3])
xmax = float(lineList[2])+float(lineList[4])
ymax = float(lineList[3])+float(lineList[5])
shape["label"] = "Person"
shape["text"] = ""
shape["points"] = [[xmin,ymin],[xmax,ymax]]
shape["group_id"] = None
shape["shape_type"] = "rectangle"
shape["flags"] = {}
shapes.append(shape)
labels_all[lineList[0]] = shapes
else:
shapes = []
xmin = float(lineList[2])
ymin = float(lineList[3])
xmax = float(lineList[2]) + float(lineList[4])
ymax = float(lineList[3]) + float(lineList[5])
shape["label"] = "Person"
shape["text"] = ""
shape["points"] = [[xmin, ymin], [xmax, ymax]]
shape["group_id"] = None
shape["shape_type"] = "rectangle"
shape["flags"] = {}
shapes.append(shape)
labels_all[lineList[0]] = shapes
for key ,val in tqdm(enumerate(labels_all)):
if int(val) % args.step_frame == 0:
name = val
len_pach = 6-len(name)
name_fix = args.motPaht.split("\\")[-3]
name = name_fix + "_" +"0"*len_pach + name +".jpg"
# label_lines = label_lines[:int(len(label_lines)/2)]
labels["version"] = "0.3.3"
labels["flags"] = dict()
labels["shapes"] = labels_all[val]
labels["imagePath"] = name
labels["imageData"] = None
labels["imageHeight"] = args.img_size[1]
labels["imageWidth"] = args.img_size[0]
labels["text"]: ""
shutil.copy(os.path.join(args.img_path,name.split("_")[-1]),os.path.join(args.out_path,name))
with open(os.path.join(args.out_path,name.replace(".jpg",".json")),'w') as f:
f.write(json.dumps(labels))
print("完成{}".format(currenImg))
currenImg += 1
if __name__ == '__main__':
main()