虚拟数据集录制后用于训练操作

1.处理物体信息 把空格改成“,” gt含义obj_id, det_id, kf_id, min_x, min_y, max_x, max_y

import os
import numpy as np
import pandas as pd
import os.path as osp

def replace(file, old_content, new_content):
    content = read_file(file)
    content = content.replace(old_content, new_content)
    rewrite_file(file, content)

# 读文件内容
def read_file(file):
    with open(file, encoding='UTF-8') as f:
        read_all = f.read()
        f.close()

    return read_all

# 写内容到文件
def rewrite_file(file, data):
    with open(file, 'w', encoding='UTF-8') as f:
        f.write(data)
        f.close()
# src_data='/media/ckq/data/kitti/data_object/training/label_2'
src_data='/media/ckq/NewData/test/MOT/images/train'
seqs = [s for s in os.listdir(src_data)]
seqs.sort()  #排个序
print(seqs)
for seq in seqs:
    path=osp.join(src_data,seq,'gt/gt.txt')

    print(path)
    replace(path, ' ', ',')

2.图片重命名 原先是从0开始,重新命名后是img000001—img0000…

import os
import re
import sys
import os
import os.path as osp

def renameall(path):
    fileList = os.listdir(path)  # 待修改文件夹
    fileList.sort()
    print("修改前:" + str(fileList))  # 输出文件夹中包含的文件
    os.chdir(path)  # 将当前工作目录修改为待修改文件夹的位置
    num = 1  # 名称变量
    for fileName in fileList:  # 遍历文件夹中所有文件
        #print(fileName)
        tiqu_fileName=fileName[:6]
        # tiqu_fileName=fileName[3:6]
        seq_num =int(tiqu_fileName)+1
        # seq_num = int(tiqu_fileName)
        #print(seq_num)
        name = str(seq_num).zfill(6)  # 设置宽度
        all_name='img'+name
        print(all_name)


        # pat = ".+\.(jpg|jpeg|JPG)"  # 匹配文件名正则表达式
        pat = ".+\.(png|PNG)"  # 匹配文件名正则表达式
        pattern = re.findall(pat, fileName)  # 进行匹配
        print('pattern[0]:', pattern)
        print('num:', num, 'filename:', fileName)
        os.rename(fileName, (all_name + '.' + pattern[0]))  # 文件重新命名
        #name = str(num).zfill(6)  # 设置宽度
        #name = num
        #os.rename(fileName, ('img' + name +'.'+ pattern[0]))  # 文件重新命名
        #print(name)
        #os.rename(fileName, (name + '.' + pattern[0]))  # 文件重新命名
        #num = num + 1  # 改变编号,继续下一项
    print("---------------------------------------------------")
    sys.stdin.flush()  # 刷新
    print("修改后:" + str(os.listdir(path)))  # 输出修改后文件夹中包含的文件



src_path='/media/ckq/NewData/test/MOT/images/train'
imgs_name = os.listdir(src_path)
imgs_name.sort()
print(imgs_name)
for img_name in imgs_name:
    img_path=osp.join(src_path,img_name,'img1')
    print(img_path)
    renameall(img_path)



3.生成训练gt,每张图片对应一个gt.txt
对原先的改动是:首先给每张图片对应的文档建立一个空文件里面预先赋值0,0,0,0,0,0
之后在这基础上对图片添加检测交通灯的信息

import os.path as osp
import os
import shutil
import numpy as np


def mkdirs(d):
    # if not osp.exists(d):
    if not osp.isdir(d):
        os.makedirs(d)

data_root = '/media/ckq/NewData/test/'
seq_root = data_root + 'MOT/images/train'
label_root = data_root + 'MOT/labels_with_ids/train'

if not os.path.isdir(label_root):
    mkdirs(label_root)
else:  # 如果之前已经生成过: 递归删除目录和文件, 重新生成目录
    shutil.rmtree(label_root)
    os.makedirs(label_root)

#这次只有交通灯
# cls_map = {
     
#     'Person=1'
#     'Pedestrian=2'
#     'Car=3'
#     'Person_sitting=4'
#     'Cyclist=5'
#     'Van=6'
#     'Truck=7'
#     'Tram=8'
#     'Misc=9'
#     'DontCare=10'
# }

print("Dir %s made" % label_root)
seqs = [s for s in os.listdir(seq_root)]
#seqs=['0000']

# seqs=['0000', '0001', '0002', '0003',
#       '0004', '0005', '0006', '0007',
#       '0008', '0009', '0010', '0011',
#       '0012', '0014', '0015', '0018',
#       '0019', '0020']
#打印序列
print(seqs)


tid_curr = 0
tid_last = -1
total_track_id_num = 0
for seq in seqs:  # 每段视频都对应一个gt.txt
    print("Process %s, " % seq, end='')

    seq_info_path = osp.join(seq_root, seq, 'seqinfo.ini')   #提取每个数据的info信息 /media/ckq/data/kitti/MOT/images/train
    print(seq_info_path)
    with open(seq_info_path) as seq_info_h:  # 读取 *.ini 文件
        seq_info = seq_info_h.read()
        seq_photo_numbers = int(seq_info[seq_info.find('seqLength=') + 10:seq_info.find('\nimWidth')])  # 视频的宽
        seq_width = int(seq_info[seq_info.find('imWidth=') + 8:seq_info.find('\nimHeight')])  # 视频的宽
        seq_height = int(seq_info[seq_info.find('imHeight=') + 9:seq_info.find('\nimExt')])  # 视频的高
        print('seq_photo_numbers:', seq_photo_numbers)
        print('seq_width:',seq_width)
        print('seq_height:', seq_height)

    gt_txt = osp.join(seq_root, seq, 'gt', 'gt.txt')  # 读取GT文件
    # print(gt_txt)  #打印路径
    #gt = np.loadtxt(gt_txt, dtype=np.str, delimiter=',')  # 加载成np格式

    gt = np.loadtxt(gt_txt, dtype=np.float64, delimiter=',')  # 加载成np格式
    # print(gt)  #打印文本内容
    # print('gt.T')
    # print(gt.T) #也是打印文本内容
    idx = np.lexsort(gt.T[:2, :])  # 优先按照track id排序(对视频帧进行排序, 而后对轨迹ID进行排序)
    # print(idx)
    gt = gt[idx, :]

    tr_ids = set(gt[:, 1])
    # print("%d track ids in seq %s" % (len(tr_ids), seq))
    total_track_id_num += len(tr_ids)  # track id统计数量如何正确计算?

    seq_label_root = osp.join(label_root, seq, 'img1')
    mkdirs(seq_label_root)

    for i in range(1,seq_photo_numbers+1):
        seq_name = '{:06d}.txt'.format(i)
        add_name = 'img' + str(seq_name)
        print(add_name)
        label_f_path = osp.join(seq_label_root, add_name)  # 原先不加1  因为kitti数据集从第一帧从0 开始所以加了1 同时加上img前缀名称
        #os.mknod(label_f_path)    #创建空文件
        file = open(label_f_path, 'a')
        label_str = '0 {:d} {:.6f} {:.6f} {:.6f} {:.6f}\n'.format(
            0,
            0,  # center_x
            0,  # center_y
            0,  # bbox_w
            0)  # bbox_h
        file.write(label_str)


    # 读取GT数据的每一行(一行即一条数据)

    # for obj_id det_id kf_id x_b y_b w_b h_b
    for obj_id, det_id, kf_id, min_x, min_y, max_x, max_y in gt:
        fid = int(kf_id+1)
        tid = int(det_id)
        #
        # 判断是否是同一个track, 记录上一个track和当前track
        if not tid == tid_last:  # not 的优先级比 == 高
            tid_curr += 1
            tid_last = tid
        #由于kitti标签与训练标签参数有点不同 需要自己计算 x y w h
        # w=float(bbox_right-bbox_left)
        # h=float(bbox_bottom-bbox_top)
        # x=int(bbox_left+0.5)
        # y=int(bbox_top+0.5)
        w = float(max_x-min_x)
        h = float(max_y-min_y)
        x = int(min_x+0.5)
        y = int(min_y+0.5)
        # min_x, min_y, max_x, max_y
        # x_b, y_b, w_b, h_b
        # bbox中心点坐标
        x += w / 2
        y += h / 2


        # 网label中写入track id, bbox中心点坐标和宽高(归一化到0~1)
        # 第一列的0是默认只对一种类别进行多目标检测跟踪(0是类别)
        label_str = '0 {:d} {:.6f} {:.6f} {:.6f} {:.6f}\n'.format(
            tid_curr,
            x / seq_width,  # center_x
            y / seq_height,  # center_y
            w / seq_width,  # bbox_w
            h / seq_height)  # bbox_h
        # print(label_str.strip())

        seq_name= '{:06d}.txt'.format(fid)
        add_name= 'img'+str(seq_name)
        print(add_name)
        label_f_path = osp.join(seq_label_root,add_name)   #原先不加1  因为kitti数据集从第一帧从0 开始所以加了1 同时加上img前缀名称
        #label_f_path = osp.join(seq_label_root, '{:06d}.txt'.format(fid+1))   #原先不加1  因为kitti数据集从第一帧从0 开始所以加了1
        #label_f_path = osp.join(seq_label_root, '{:06d}.txt'.format(fid))
        with open(label_f_path, 'a') as f:  # 以追加的方式添加每一帧的label
            f.write(label_str)

print("Total %d track ids in this dataset" % total_track_id_num)
print('Done')

4.删除有物体信息的gt,对于没有物体信息的就不删除0,0,0,0,0,0

import fileinput
import os
import re
import sys
import os
import os.path as osp
src_path="/media/ckq/NewData/test/MOT/labels_with_ids/train"
fileList = os.listdir(src_path)  # 待修改文件夹
fileList.sort()
# print(fileList)
for filename in fileList:
    img_path=osp.join(src_path,filename,'img1')
    print(img_path)
    img_path_gt_list = os.listdir(img_path)  # 待修改文件夹
    # print(img_path_gt)
    for img_path_gt in img_path_gt_list:
        img_gt_name = osp.join(img_path, img_path_gt)
        print(img_gt_name)
        count=0
        f = open(img_gt_name,"r")
        for line in f.readlines():
            count=count+1
        if(count > 1):
            with open(img_gt_name, 'r') as r:
                lines = r.readlines()
            with open(img_gt_name, 'w') as w:
                for l in lines:
                    if '0 0 0.000000 0.000000 0.000000 0.000000' not in l:
                        w.write(l)

5.展示标签是否正确

# -*- coding:utf-8 -*-
import os
import cv2
import os.path as osp
'''
显示跟踪训练数据集标注
'''

root_path ='/media/ckq/NewData/test/MOT'
img_dir = "images/train"
label_dir = "labels_with_ids/train"


imgs = os.listdir(root_path + "/" + img_dir)  #遍历图片数据集列表  0000 0001........
imgs.sort()
for i, img in enumerate(imgs):  #一个一个遍历
    #img_name = img[:-1]   #img[:-1] -1代表从右往左 第一个不取
    #print(img)
    img_name=img #每个图片集名字
    print(img_name)
    label_path=osp.join(root_path,label_dir,img_name,'img1')
    # print(label_path)
    label_gts_name=os.listdir(label_path)
    label_gts_name.sort()
    # print(label_gts_name)
    for frame_gt in label_gts_name:
        #print(frame_gt)
        frame_gt_name=frame_gt[:9]
        #print(frame_gt_name)
        label_f = open(label_path + "/" +frame_gt_name+".txt", "r") #路劲标签名
        #print(label_f)
        lines = label_f.readlines()
        # print(lines)
        # print(root_path + "/" + img_dir + "/" + img+"/img/"+frame_gt_name)
        img_data = cv2.imread(root_path + "/" + img_dir + "/" + img+"/img1/"+frame_gt_name+".png") #gt对应的图片序号
        # img_data = cv2.imread(root_path + "/" + img_dir + "/" + img + "/img1/" + frame_gt_name + ".png")  # gt对应的图片序号
        # print(img_data)
        H, W, C = img_data.shape
        # print(H)
        # print(W)
        # print(C)
        for line in lines:
            line_list = line.strip().split()
            class_num = int(line_list[0])  # 类别号
            obj_ID = int(line_list[1])  # 目标ID
            x, y, w, h = line_list[2:]  # 中心坐标,宽高(经过原图宽高归一化后)
            x = int(float(x) * W)
            y = int(float(y) * H)
            w = int(float(w) * W)
            h = int(float(h) * H)
            left = int(x - w / 2)
            top = int(y - h / 2)
            right = left + w
            bottom = top + h
            cv2.circle(img_data, (x, y), 1, (0, 0, 255))
            cv2.rectangle(img_data, (left, top), (right, bottom), (0, 255, 0), 2)
            cv2.putText(img_data,str(obj_ID), (left, top), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 255), 1)
        # resized_img = cv2.resize(img_data, (800, 416))
        resized_img = cv2.resize(img_data, (1242, 375))
        cv2.imshow("label", resized_img)
        cv2.waitKey(100)

虚拟数据集录制后用于训练操作_第1张图片
6.生成train文件

import os
import os.path as osp
image_flder = "/media/ckq/NewData/test/MOT/images/train"
#image_flder = "/home/ckq/Desktop/MOT/images/train"   #用来test
need_image_flder="MOT/images/train"
imgs = os.listdir(image_flder)
#print(imgs)
train_f = open("/media/ckq/NewData/test/MOT/unreal_light.train", "w")
#train_f = open("/home/ckq/Desktop/MOT/kitt_car.train", "w")  #用来test
for img_name in imgs:
    image_path=osp.join(image_flder,img_name,'img1')
    need_image_path=osp.join(need_image_flder,img_name,'img1')
    #print(image_path)
    print(need_image_path)
    image_names=os.listdir(image_path)
    image_names.sort()
    print(image_names)
    for image_name in image_names:
        #save_str = image_path + '/' + image_name +"\n"   #这里保存的路径是绝对路径  代码中不需要绝对路径  相对路径即可
        save_str = need_image_path + '/' + image_name + "\n"
        print(save_str)
        train_f.write(save_str)
train_f.close()

你可能感兴趣的:(FairMOT,计算机视觉)