(1)将annotations放到一个文件夹下,并读取标签。
import os
import glob
import pandas as pd
import xml.etree.ElementTree as ET
def xml_to_csv(path):
xml_list = []
for xml_file in glob.glob(path + '/*.xml'):
tree = ET.parse(xml_file)
root = tree.getroot()
for Object in root.findall('object'):
bndbox=Object.find('bndbox')
Size = root.find('size')
value = (root.find('filename').text,
int(Size.find('width').text),
int(Size.find('height').text),
Object.find('name'),
int(bndbox.find('xmin').text),
int(bndbox.find('ymin').text),
int(bndbox.find('xmax').text),
int(bndbox.find('ymax').text)
)
xml_list.append(value)
column_name = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax']
xml_df = pd.DataFrame(xml_list, columns=column_name)
return xml_df
def main():
image_path = "D:/object_detection/research/object_detection/ROS1/VOCdevkit/RSDS2016/Annotations"
xml_df = xml_to_csv(image_path)
xml_df.to_csv('D:/object_detection/research/object_detection/ROS1/VOCdevkit/RSDS2016/labels.csv', index=None)
print('Successfully converted xml to csv.')
main()
注意更改程序中的路径,生成csv格式的文件。
(2)读取csv格式的文件,并选择数据集中的某一张图片,进行显示。
另外也可以在labelImg中加载图片和对应的标签,显示。