1. 程式人生 > 其它 >WPI資料集的標籤解析程式

WPI資料集的標籤解析程式

技術標籤:python

本程式用於WPI資料集的標籤解析,將xgtf檔案轉換為txt檔案,每個txt檔案包含一張圖片的標籤。在txt檔案中,每一行有5個引數,表示一個類,以及一個矩形框的中心橫縱座標、寬度、高度。

# Transform xgtf file to txt file.
# In each txt file, it includes labels with class number, 
# and four normalized values such as x, y, width and height.
try:
    import xml.etree.cElementTree as
ET except ImportError: import xml.etree.ElementTree as ET import os import glob # WPI data set: image width=1920, height=1080 # WPI (Worchester Polytechnical Institute) img_width = float(1920.0) img_height = float(1080.0) #0 1 2 3 4 5 6 7 8 file_number = 9 root_dir = './WPI/Frames_GT_wHolder/labels/ViPER/'
xgtf_list = glob.glob(root_dir + '*.xgtf') loc = xgtf_list[file_number].rfind('/') loc_point = xgtf_list[file_number].rfind('.') label_name = xgtf_list[file_number][loc + 1 : loc_point] print(xgtf_list) print('label directory:',label_name) label_path = [] if not os.path.exists(root_dir + label_name)
: # label_path = os.mkdir(root_dir + label_name) os.mkdir(root_dir + label_name) def get_name_length(label): name_length = 0 # the length of the number string of each image name if 'RC' == label: name_length = 5 else: name_length = 4 return name_length # Traffic light labels of YOLO V4 label_dict = { 'GA_left':'1', 'GA_right':'2', 'GA_up':'3', 'GA_up_left':'5', 'GA_up_right':'4', 'GC_GA_left':'13', 'GC_GA_right':'14', 'GC':'0', 'RA_left':'8', 'RC':'7' } # print('RA_left:', label_dict['RA_left']) # XML parse tree = ET.parse(xgtf_list[file_number]) root = tree.getroot() # print('root tag:',root.tag) # print(root[1]) for child in root[1][0]: print('\n') labelname = (child.attrib)['name'] name_length = get_name_length(labelname) for child1 in child: # print(child1.tag, child1.attrib) for child2 in child1: if 'height' in child2.attrib: # print((child2.attrib)['framespan']) # print(child2.attrib) low = '' high = '' low = ((child2.attrib)['framespan']).split(':')[0] high = ((child2.attrib)['framespan']).split(':')[1] print('frame span:', low, high) label_num = '' if labelname in label_dict: label_num = label_dict[labelname] else: print('The Label name in the xgtf file is wrong.') exit() width = float((child2.attrib)['width'])/img_width height = float((child2.attrib)['height'])/img_height x = (float((child2.attrib)['x']) + float((child2.attrib)['width'])/2.0)/img_width y = (float((child2.attrib)['y']) + float((child2.attrib)['height'])/2.0)/img_height oneline = [] oneline = [label_num, '\t', str(x), '\t', str(y), '\t', str(width), '\t', str(height), '\n'] print("one line:", oneline) if low == high: str_num = low.zfill(name_length) with open(root_dir + label_name + '/image.' + str_num + '.txt', "a", encoding='utf-8') as txt: txt.writelines(oneline) txt.close() else: a = int(low) b = int(high) + 1 for i in range(a, b): str_num = str(i).zfill(name_length) with open(root_dir + label_name + '/image.' + str_num + '.txt', "a", encoding='utf-8') as txt: txt.writelines(oneline) txt.close()