11"""
2- 将UAVDT转换为yolo v5格式
2+ 将MOT17转换为yolo v5格式
33class_id, xc_norm, yc_norm, w_norm, h_norm
44"""
55
1111import numpy as np
1212import random
1313
14+ from tqdm import tqdm
15+
1416# DATA_ROOT = '/data/wujiapeng/datasets/MOT17/'
1517DATA_ROOT = "/perception/yixu.cui/datas/tracking/MOT17"
1618image_wh_dict = {} # seq->(w,h) 字典 用于归一化
1719
1820
1921def generate_imgs_and_labels (opts ):
2022 """
21- 产生图片路径的txt文件以及yolo格式真值文件
23+ 产生图片路径的txt文件以及yolo格式真值(gt)文件
2224 """
2325 if opts .split == "test" :
2426 seq_list = os .listdir (osp .join (DATA_ROOT , "test" ))
2527 else :
2628 seq_list = os .listdir (osp .join (DATA_ROOT , "train" ))
2729 seq_list = [item for item in seq_list if "FRCNN" in item ] # 只取一个FRCNN即可
28- if "val" in opts .split :
30+ if opts .split .lower () == "val" :
31+ opts .half = True # 验证集取训练集的一半
32+ elif opts .split .lower () == "val_dpm" :
2933 opts .half = True # 验证集取训练集的一半
34+ seq_list = os .listdir (osp .join (DATA_ROOT , "train" ))
35+ seq_list = [item for item in seq_list if "DPM" in item ] # 只取一个DPM即可
36+ elif opts .split .lower () == "val_sdp" :
37+ opts .half = True # 验证集取训练集的一半
38+ seq_list = os .listdir (osp .join (DATA_ROOT , "train" ))
39+ seq_list = [item for item in seq_list if "SDP" in item ] # 只取一个SDP即可
3040
3141 print ("--------------------------" )
3242 print (f"Total { len (seq_list )} seqs!!" )
@@ -62,37 +72,37 @@ def process_train_test(
6272
6373 """
6474
65- for seq in seqs :
75+ for seq in tqdm ( sorted ( seqs )) :
6676 print (f"Dealing with { split } dataset..." )
6777
68- img_dir = (
78+ img_src_root = (
6979 osp .join (DATA_ROOT , "train" , seq , "img1" )
7080 if split != "test"
7181 else osp .join (DATA_ROOT , "test" , seq , "img1" )
7282 ) # 图片路径
73- imgs = sorted (os .listdir (img_dir )) # 所有图片的相对路径
83+ imgs = sorted (os .listdir (img_src_root )) # 所有图片的相对路径
7484 seq_length = len (imgs ) # 序列长度
7585
7686 if split != "test" :
7787
7888 # 求解图片高宽
79- img_eg = cv2 .imread (osp .join (img_dir , imgs [0 ]))
89+ img_eg = cv2 .imread (osp .join (img_src_root , imgs [0 ]))
8090 w0 , h0 = img_eg .shape [1 ], img_eg .shape [0 ] # 原始高宽
8191
82- ann_of_seq_path = os .path .join (img_dir , "../" , "gt" , "gt.txt" ) # GT文件路径
92+ ann_of_seq_path = os .path .join (img_src_root , "../" , "gt" , "gt.txt" ) # GT文件路径
8393 ann_of_seq = np .loadtxt (
8494 ann_of_seq_path , dtype = np .float32 , delimiter = ","
8595 ) # GT内容
8696
87- gt_to_path = osp .join (DATA_ROOT , "labels" , split , seq ) # 要写入的真值文件夹
97+ gt_file_root = osp .join (DATA_ROOT , "labels" , split , seq ) # 要写入的真值(gt)文件夹
8898 # 如果不存在就创建
89- if not osp .exists (gt_to_path ):
90- os .makedirs (gt_to_path )
99+ if not osp .exists (gt_file_root ):
100+ os .makedirs (gt_file_root )
91101
92- exist_gts = [] # 初始化该列表 每个元素对应该seq的frame中有无真值框
102+ exist_gts = [] # 初始化该列表 每个元素对应该seq的frame中有无真值(gt)框
93103 # 如果没有 就在train.txt产生图片路径
94104
95- for idx , img in enumerate (imgs ):
105+ for idx , img_name in enumerate (tqdm ( imgs ) ):
96106 # img 形如: img000001.jpg
97107 if idx < int (seq_length * frame_range ["start" ]) or idx > int (
98108 seq_length * frame_range ["end" ]
@@ -102,23 +112,26 @@ def process_train_test(
102112 # 第一步 产生图片软链接
103113 # print('step1, creating imgs symlink...')
104114 if opts .generate_imgs :
105- img_to_path = osp .join (DATA_ROOT , "images" , split , seq ) # 该序列图片存储位置
115+ img_link_root = osp .join (DATA_ROOT , "images" , split , seq ) # 该序列图片存储位置
106116
107- if not osp .exists (img_to_path ):
108- os .makedirs (img_to_path )
117+ if not osp .exists (img_link_root ):
118+ os .makedirs (img_link_root )
109119
110- os .symlink (
111- osp .join (img_dir , img ), osp .join (img_to_path , img )
112- ) # 创建软链接
120+ link_name = osp .join (img_link_root , img_name )
121+ if not osp .exists (link_name ):
122+ os .symlink (
123+ osp .join (img_src_root , img_name ),
124+ link_name ,
125+ ) # 创建软链接
113126
114- # 第二步 产生真值文件
127+ # 第二步 产生真值(gt)文件
115128 # print('step2, generating gt files...')
116129 ann_of_current_frame = ann_of_seq [
117130 ann_of_seq [:, 0 ] == float (idx + 1 ), :
118- ] # 筛选真值文件里本帧的目标信息
131+ ] # 筛选真值(gt)文件里本帧的目标信息
119132 exist_gts .append (True if ann_of_current_frame .shape [0 ] != 0 else False )
120133
121- gt_to_file = osp .join (gt_to_path , img [:- 4 ] + ".txt" )
134+ gt_to_file = osp .join (gt_file_root , img_name [:- 4 ] + ".txt" )
122135
123136 with open (gt_to_file , "w" ) as f_gt :
124137 for i in range (ann_of_current_frame .shape [0 ]):
@@ -156,10 +169,8 @@ def process_train_test(
156169
157170 f_gt .write (write_line )
158171
159- f_gt .close ()
160-
161172 else : # test 只产生图片软链接
162- for idx , img in enumerate (imgs ):
173+ for idx , img_name in enumerate (tqdm ( imgs ) ):
163174 # img 形如: img000001.jpg
164175 if idx < int (seq_length * frame_range ["start" ]) or idx > int (
165176 seq_length * frame_range ["end" ]
@@ -169,18 +180,21 @@ def process_train_test(
169180 # 第一步 产生图片软链接
170181 # print('step1, creating imgs symlink...')
171182 if opts .generate_imgs :
172- img_to_path = osp .join (DATA_ROOT , "images" , split , seq ) # 该序列图片存储位置
183+ img_link_root = osp .join (DATA_ROOT , "images" , split , seq ) # 该序列图片存储位置
173184
174- if not osp .exists (img_to_path ):
175- os .makedirs (img_to_path )
185+ if not osp .exists (img_link_root ):
186+ os .makedirs (img_link_root , exist_ok = True )
176187
177- os .symlink (
178- osp .join (img_dir , img ), osp .join (img_to_path , img )
179- ) # 创建软链接
188+ link_name = osp .join (img_link_root , img_name )
189+ if not osp .exists (link_name ):
190+ os .symlink (
191+ osp .join (img_src_root , img_name ),
192+ link_name ,
193+ ) # 创建软链接
180194
181- # 第三步 产生图片索引train.txt等
195+ # 第三步 产生图片索引train.txt, test. txt等
182196 print (f"generating img index file of { seq } " )
183- to_file = os .path .join ("./mot17/ " , split + ".txt" )
197+ to_file = os .path .join ("./datasets/mot17 " , split + ".txt" )
184198 with open (to_file , "a" ) as f :
185199 for idx , img in enumerate (imgs ):
186200 if idx < int (seq_length * frame_range ["start" ]) or idx > int (
@@ -191,33 +205,18 @@ def process_train_test(
191205 if split == "test" or exist_gts [idx ]:
192206 f .write ("MOT17/" + "images/" + split + "/" + seq + "/" + img + "\n " )
193207
194- f .close ()
195-
196208
197209if __name__ == "__main__" :
198- if not osp .exists ("./mot17" ):
199- os .system ( "mkdir mot17" )
210+ if not osp .exists ("./datasets/ mot17" ):
211+ os .makedirs ( "./datasets/ mot17" )
200212
201213 parser = argparse .ArgumentParser ()
202- parser .add_argument ("--split" , type = str , default = "train" , help = "train, test or val" )
203- parser .add_argument (
204- "--generate_imgs" ,
205- action = "store_true" ,
206- help = "whether generate soft link of imgs" ,
207- )
208- parser .add_argument ("--certain_seqs" , action = "store_true" , help = "for debug" )
209- parser .add_argument ("--half" , action = "store_true" , help = "half frames" )
210- parser .add_argument (
211- "--ratio" ,
212- type = float ,
213- default = 0.8 ,
214- help = "ratio of test dataset devide train dataset" ,
215- )
216- parser .add_argument (
217- "--random" ,
218- action = "store_true" ,
219- help = "random split train and test" ,
220- )
214+ parser .add_argument ('--split' , type = str , default = 'train' , help = 'train, test, val or val_dpm' )
215+ parser .add_argument ('--generate_imgs' , action = 'store_true' , help = 'whether generate soft link of imgs' )
216+ parser .add_argument ('--certain_seqs' , action = 'store_true' , help = 'for debug' )
217+ parser .add_argument ('--half' , action = 'store_true' , help = 'half frames' )
218+ parser .add_argument ('--ratio' , type = float , default = 0.8 , help = 'ratio of test dataset devide train dataset' )
219+ parser .add_argument ('--random' , action = 'store_true' , help = 'random split train and test' )
221220
222221 opts = parser .parse_args ()
223222
0 commit comments