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()
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有