MOT20数据转成json格式,可用labelme打开

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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值