python解决xml文件中存在中文文字的问题

如下


	250-499
	250.jpg
	/home/pengdezhi/数据标注/250-499/250.jpg
	
		Unknown
	
	
		1085
		612
		3
	
	0
	
    helmet_off
		Unspecified
		0
		0
		
			612
			37
			630
			55
		
	

解决方法

import os
import os.path
import xml.etree.ElementTree as ET


headstr = """\

    VOC
    %s
    zhangyt
    
        My Database
    
    
        %d
        %d
        %d
    
    0
"""
objstr = """\
    
        %s
        Unspecified
        0
        0
        
            %d
            %d
            %d
            %d
        
    
"""
 
tailstr = '''\

'''

def write_xml(anno_path,head, objs, tail):
    f = open(anno_path, "w")
    f.write(head)
    for obj in objs:
        f.write(objstr%(obj[0],obj[1],obj[2],obj[3],obj[4]))
    f.write(tail)


def convertxml(path_xml,files, save_xml):

	for xmlFile in files:
		dataset = []
		print(xmlFile)
		f = open(os.path.join(path_xml,xmlFile))
		xml_text = f.read()
		root = ET.fromstring(xml_text)
		f.close()

		image_name = xmlFile.replace("xml", "jpg")
		# name = root.find('path').text
		width = int(root.findtext("./size/width"))
		height = int(root.findtext("./size/height"))
		depth = int(root.findtext("./size/depth"))

		head=headstr % (image_name, width, height, depth)
 
		for obj in root.iter("object"):
			name = str(obj.findtext("name"))
			xmin = int(obj.findtext("bndbox/xmin"))
			ymin = int(obj.findtext("bndbox/ymin"))
			xmax = int(obj.findtext("bndbox/xmax"))
			ymax = int(obj.findtext("bndbox/ymax"))
 
			if xmax == xmin or ymax == ymin:
				print(xmlFile)
			dataset.append([name, xmin, ymin, xmax, ymax])
		tail = tailstr
		write_xml(os.path.join(save_xml, xmlFile),head, dataset, tail)


if __name__ == "__main__":
	path_xml = "Annotations/"
	save_xml =  "Annotations_name/"
	if not os.path.exists(save_xml):
		os.mkdir(save_xml)
	xmlFile = os.listdir(path_xml)
	convertxml(path_xml,xmlFile,save_xml)

 

你可能感兴趣的:(2020非专栏)