MIN-MAX归一化

最近在做安全帽的识别,训练集标注好后需要进行归一化,简单写了个算法凑合用
import xml.etree.ElementTree as ET
import os
import numpy as np

VOC_CLASSES = (  # always index 0
    'blue', 'red', 'yellow', 'white','no')

#读取xml并进行归一化
def parse_rec(filename):
    """ Parse a PASCAL VOC xml file """
    tree = ET.parse(filename)
    objects = []
    jects = []

    for obj in tree.findall('object'):
        objc = {}
        difficult = int(obj.find('difficult').text)
        if difficult == 1:
            # print(filename)
            continue
        objc['name'] = obj.find('name').text
        bbox = obj.find('bndbox')
        objc['bbox'] = [int(float(bbox.find('xmin').text)),
                         int(float(bbox.find('ymin').text)),
                         int(float(bbox.find('xmax').text)),
                         int(float(bbox.find('ymax').text))]
        jects.append(objc)
        # 做归一化,分别对一张图片中的x、y做归一化
    vv = []
    nn = []
    for w in jects:
        ll = w['bbox']
        mm = w['name']
        vv.append(ll)
        nn.append(mm)
    vv = np.array(vv)
    min_x = min(vv[:, 0])
    min_y = min(vv[:, 1])
    max_x = max(vv[:, 2])
    max_y = max(vv[:, 3])
    j = 0
    for i in vv:
        obj_struct = {}
        obj_struct['name'] = nn [j]
        j += 1
        obj_struct['bbox'] = [float((i[0] - min_x) / (max_x - min_x)),
                               float((i[1] - min_y) / (max_y - min_y)),
                               float((i[2] - min_x) / (max_x - min_x)),
                               float((i[3] - min_y) / (max_y - min_y))]
        objects.append(obj_struct)
    return objects
#定义文件目录
Annotations = 'd:/image/label/'
xml_files = os.listdir(Annotations)

count = 0
for xml_file in xml_files:
    count += 1
    image_path = xml_file.split('.')[0] + '.jpg'
#定义保存路径
    txt_file = open('d:/image/result/' + xml_file.split('.')[0] + '.txt', 'w')
#读取标注文件并进行归一化
    results = parse_rec(Annotations + xml_file)
    if len(results) == 0:
        print(xml_file)
        continue
    for result in results:
        class_name = result ['name']
        bbox = result ['bbox']
        class_name = VOC_CLASSES.index(class_name)
        txt_file.write(str(class_name) + ' ' + str(bbox [0]) + ' ' + str(bbox [1]) + ' ' + str(bbox [2]) + ' ' + str(bbox [3]) + ' ' + '\n')
    txt_file.write('\n')
txt_file.close()

#xml文件如下

<annotation>
    <folder>picture</folder>
    <filename>00000.jpg</filename>
    <path>D:\image\picture\00000.jpg</path>
    <source>
        <database>Unknown</database>
    </source>
    <size>
        <width>1280</width>
        <height>720</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>blue</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>867</xmin>
            <ymin>349</ymin>
            <xmax>889</xmax>
            <ymax>383</ymax>
        </bndbox>
    </object>
    <object>
        <name>blue</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>1101</xmin>
            <ymin>376</ymin>
            <xmax>1127</xmax>
            <ymax>406</ymax>
        </bndbox>
    </object>
    <object>
        <name>blue</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>833</xmin>
            <ymin>253</ymin>
            <xmax>846</xmax>
            <ymax>269</ymax>
        </bndbox>
    </object>
    <object>
        <name>blue</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>772</xmin>
            <ymin>283</ymin>
            <xmax>784</xmax>
            <ymax>298</ymax>
        </bndbox>
    </object>
    <object>
        <name>blue</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>478</xmin>
            <ymin>160</ymin>
            <xmax>486</xmax>
            <ymax>170</ymax>
        </bndbox>
    </object>
</annotation>

发布了123 篇原创文章 · 获赞 12 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/haiziccc/article/details/102318038
今日推荐