1- '''
2- 使用前必看:
3- 1. 如果你是按照READ.md文档的顺序标注的,那么正常你是用不到这个文件的。如果你是从其他开源站获得的数据,由于标签不对需要更改标签,那么你可以使用这
4- 个脚本帮助修改。
5- 2.使用本脚本前请备份你的标注文件。
6- '''
7- import os
8- import argparse
9-
10-
11- def change_label_name (path ):
12- total_txt = os .listdir (path )
13- for file in total_txt :
14- file_name = path + '/' + file
15- file = open (file_name , 'r' )
16- lines = file .readlines ()
17- for index , line in enumerate (lines ):
18- t = lines [index ] # 读取当前行的内容
19- num = int (t [0 :2 ])
20-
21- # 在这里修改你想txt操作的内容
22- if num > 7 and num < 10 :
23- t = str (num - 1 ) + t [1 :] # 改成2加字符第二位往后
24- lines [index ] = t # 改写lines中的内容
25-
26- if num != 15 : # 切片判断前两个字符
27- t = ''
28- lines [index ] = t
29-
30- if num > 10 : # 切片判断前两个字符
31- t = str (num - 1 ) + t [2 :]
32- lines [index ] = t
33-
34- else :
35- t = ''
36- lines [index ] = t
37-
38- file .close ()
39- t = "" .join (lines )
40- file = open (file_name , 'w' )
41- file .write (t )
42- file .close ()
43-
44-
45- if __name__ == '__main__' :
46- parser = argparse .ArgumentParser ()
47- parser .add_argument ('path_dir' , type = str , default = '' , help = 'Location of path' )
48- args = parser .parse_args ()
49- change_label_name (args .path_dir )
50- print ("Finish change_label_id!" )
1+ '''
2+ 使用前必看:
3+ 1. 如果你是按照READ.md文档的顺序标注的,那么正常你是用不到这个文件的。如果你是从其他开源站获得的数据,由于标签不对需要更改标签,那么你可以使用这
4+ 个脚本帮助修改。
5+ 2.使用本脚本前请备份你的标注文件。下面的样例是将以YOLO格式导出的LabelStudio数据集转换为西浦GMaster训练的数据集
6+ '''
7+ import os
8+ import argparse
9+
10+
11+ def update_labels (folder_path ):
12+ # 你的旧的和新的类别顺序
13+ old_order = ["B1" , "B2" , "B3" , "B4" , "B5" , "BB" , "BO" , "BS" , "R1" , "R2" , "R3" , "R4" , "R5" , "RB" , "RO" , "RS" ]
14+ new_order = ["B1" , "B2" , "B3" , "B4" , "B5" , "BO" , "BS" , "R1" , "R2" , "R3" , "R4" , "R5" , "RO" , "RS" , "BB" , "RB" ]
15+
16+ # 创建一个字典来映射旧的id到新的id
17+ id_mapping = {old_order .index (name ): new_order .index (name ) for name in old_order }
18+
19+ # 遍历标签文件
20+ for root , dirs , files in os .walk (folder_path ):
21+ for file in files :
22+ if file .endswith (".txt" ): # 假设标签文件是.txt格式
23+ with open (os .path .join (root , file ), "r" ) as f :
24+ lines = f .readlines ()
25+
26+ # 修改每一行的标签id
27+ for i in range (len (lines )):
28+ parts = lines [i ].split (" " )
29+ parts [0 ] = str (id_mapping [int (parts [0 ])]) # 更新id
30+ lines [i ] = " " .join (parts )
31+
32+ # 将修改后的行写回文件
33+ with open (os .path .join (root , file ), "w" ) as f :
34+ f .writelines (lines )
35+
36+
37+ if __name__ == "__main__" :
38+ parser = argparse .ArgumentParser (description = "Update label files" )
39+ parser .add_argument ("folder_path" , type = str , help = "Path to the folder containing label files" )
40+ args = parser .parse_args ()
41+
42+ update_labels (args .folder_path )
0 commit comments