visdrone2019/2020数据集txt标签文件转xml

亲测可用。

visdrone2019/2020数据集txt标签文件转xml

txt标签内容为:
bbox_left,bbox_top,bbox_width,bbox_height,score,object_category,truncation,occlusion
类别: ignored regions(0), pedestrian(1), people(2), bicycle(3), car(4),van(5), truck(6), tricycle(7), awning-tricycle(8), bus(9), motor(10),
others(11)

import cv2
import os
import numpy as np


input_img_folder = 'images'
input_ann_folder = 'annotations'
output_ann_folder = 'annotations_xml'
output_img_folder = 'images_new'

os.makedirs(output_img_folder, exist_ok=True)
os.makedirs(output_ann_folder, exist_ok=True)


image_list = os.listdir(input_img_folder)
annotation_list = os.listdir(input_ann_folder)

label_dict = {
	"0" : "Ignore",
	"1" : "Pedestrian",
	"2" : "People",
	"3" : "Bicycle",
	"4" : "Car",
	"5" : "Van",
	"6" : "Truck",
	"7" : "Tricycle",
	"8" : "Awning-tricycle",
	"9" : "Bus",
	"10" : "Motor",
	"11" : "Others"
}

thickness = 2
color = (255,0,0)
count = 0

def object_string(label, bbox):
	req_str = '''
	
		{}
		Unspecified
		0
		0
		
			{}
			{}
			{}
			{}
		
	
	'''.format(label, bbox[0], bbox[1], bbox[2], bbox[3])
	return req_str

for annotation in annotation_list:
	annotation_path = os.path.join(os.getcwd(), input_ann_folder, annotation)
	xml_annotation = annotation.split('.txt')[0] + '.xml'
	xml_path = os.path.join(os.getcwd(), output_ann_folder, xml_annotation)
	img_file = annotation.split('.txt')[0] + '.jpg'
	img_path = os.path.join(os.getcwd(), input_img_folder, img_file)
	print(img_path)
	output_img_path = os.path.join(os.getcwd(), output_img_folder, img_file)
	img = cv2.imread(img_path)

	annotation_string_init = '''

	annotations
	{}
	{}
	
		Unknown
	
	
		{}
		{}
		{}
	
	0'''.format(img_file, img_path, img.shape[1], img.shape[0], img.shape[2])  # numpy图片顺序 高、宽、通道
	print(img.shape[0])
	file = open(annotation_path, 'r')
	lines = file.readlines()
	for line in lines:
		new_line = line.strip('\n').split(',')
		new_coords_min = (int(new_line[0]), int(new_line[1]))
		new_coords_max = (int(new_line[0])+int(new_line[2]), int(new_line[1])+int(new_line[3]))
		bbox = (int(new_line[0]), int(new_line[1]), int(new_line[0])+int(new_line[2]), int(new_line[1])+int(new_line[3]))
		label = label_dict.get(new_line[5])
		req_str = object_string(label, bbox)
		annotation_string_init = annotation_string_init + req_str
		#cv2.rectangle(img, new_coords_min, new_coords_max, color, thickness)

	# cv2.imwrite(output_img_path, img)

	annotation_string_final = annotation_string_init + ''
	f = open(xml_path, 'w')
	f.write(annotation_string_final)
	f.close()
	count += 1
	print('[INFO] Completed {} image(s) and annotation(s) pair'.format(count))

也可完成xml文件内path路径的修改

你可能感兴趣的:(机器学习,python,python,深度学习)