这篇文章将为大家详细讲解有关VOC格式如何转换CSV,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
import os import xml.dom.minidom path_img = "VOC2007/JPEGImages" path_xml = "VOC2007/Annotations" xml_list = [] for xml1 in os.listdir(path_xml): if xml1.endswith(".xml"): xml_list.append(xml1) csv_labels = open("csv_labels.csv", "w") for xml_file in xml_list: image_id, _ = os.path.splitext(xml_file) DomTree = xml.dom.minidom.parse(os.path.join(path_xml, xml_file)) annotation = DomTree.documentElement objectlist = annotation.getElementsByTagName('object') for objects in objectlist: namelist = objects.getElementsByTagName('name') objectname = namelist[0].childNodes[0].data bndbox = objects.getElementsByTagName("bndbox") for box in bndbox: x1_list = box.getElementsByTagName('xmin') x1 = int(x1_list[0].childNodes[0].data) y1_list = box.getElementsByTagName('ymin') y1 = int(y1_list[0].childNodes[0].data) x2_list = box.getElementsByTagName('xmax') x2 = int(x2_list[0].childNodes[0].data) y2_list = box.getElementsByTagName('ymax') y2 = int(y2_list[0].childNodes[0].data) line = str(image_id) + "," + str(x1) + "," + str(y1) \ + "," + str(x2) + "," + str(y2) + "," + objectname + "\n" print(line) csv_labels.write(line) csv_labels.close()
关于VOC格式如何转换CSV就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。