目标检测一般会自制xml文件,用以储存标记信息,根据网络传输数据格式的不同,需要对xml文件做一个转化,参考了一些博客,贴出来供参考,另附一个简单易懂可操作的程序:
使用Python库ElementTree解析XML文件
python使用ElementTree解析XML文件
# -*- coding: utf-8 -*-
"""
Created on Fri May 24 16:37:44 2019
@author: BIG1KOR
"""
import pandas as pd
import os
import xml.etree.ElementTree as ET
#%%
files = os.listdir('...\Annotations')
file_names = [os.path.join('...\Annotations', i) for i in files]
#%%
data_parsed = []
for item in file_names:
tree = ET.parse(item)
root = tree.getroot()
#parsing image name
img_name = root.find('filename').text
print(img_name)
#parsing image items
for object in root.findall('object'):
cel_typ = object.find('name').text
sizes = object.find('bndbox')
xmin = sizes.find('xmin').text
xmax = sizes.find('xmax').text
ymin = sizes.find('ymin').text
ymax = sizes.find('ymax').text
data_parsed.append([img_name, cel_typ, xmin, xmax, ymin, ymax])
print(data_parsed)
df_parsed = pd.DataFrame(data_parsed)
df_parsed.columns = ['image_names', 'cell_type', 'xmin', 'xmax', 'ymin', 'ymax']
就这吧!