学习笔记一:xml文件的读取操作,

import os
import xml.etree.ElementTree as ET
########   解析xml的基本信息  ##################
import numpy as np

def per_xml(self):
    filename = os.listdir(self.path_dir)
    # step1 解析根路径,获取图片大小信息
    for file in filename:
        tree = ET.parse(file)
        root = tree.getroot()
        bb_box = []
        lable = []
        size_tree = root.find('size')
        width = float(size_tree.find('width').text)
        height =float(size_tree.find('height').text)
   #获取每张图片里的多个对象的信息
        for object_tree in root.findall('object'):
            for box in object_tree.iter("bndbox"):
                xmin = float(box.find('xmin').text)/width #除以宽主要是进行归一化的结果
                ymin = float(box.find('ymin').text)/height
                xmax = float(box.find('xmax').text)/width
                ymax = float(box.find('ymax').text) / height
            bb_box1 = [xmin,ymin,xmax,ymax]
            bb_box.append(bb_box1)
            lable1 = object_tree.find('name').text
            one_hot_lable = self.one_hot(lable1)
            lable.append(one_hot_lable)
    # 获取图片名字,保存bb_box以及类别的one-hot编码信息
    image_name = root.find('filename').text
    bounding_boxes = np.asarray(bb_box)
    one_hot_classes = np.asarray(lable)
    image_data = np.hstack ((bounding_boxes,one_hot_classes))#按照维度拼接
    self.data[ image_name] = image_data

你可能感兴趣的:(python,学习,xml,numpy)