目标检测yolo标签全自动生成(voc_label.py程序修改)

最近做了很多目标检测相关的任务,因为任务有时候是客户自己标注的xml格式,需要转换成yolov3使用的txt格式,即一行中包含类别信息和位置信息的模式,这一转换我们通常是使用官方的voc_label.py这个程序完成的,我们先来看一下xml的标注标注格式:(已经熟悉的可以直接跳到后面)

<annotation>
	<folder>ZeHe</folder>
	<filename>18.PNG</filename>
	<path>D:\dongwu\ZeHe\18.PNG</path>
	<source>
		<database>Unknown</database>
	</source>
	<size>
		<width>2448</width>
		<height>2048</height>
		<depth>1</depth>
	</size>
	<segmented>0</segmented>
	<object>
		<name>snake</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>762</xmin>
			<ymin>994</ymin>
			<xmax>1107</xmax>
			<ymax>1313</ymax>
		</bndbox>
	</object>
	<object>
		<name>black horse</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>1603</xmin>
			<ymin>815</ymin>
			<xmax>1869</xmax>
			<ymax>1307</ymax>
		</bndbox>
	</object>
</annotation>

再看一些yolov3需要使用的txt格式的标注数据

5 0.38133169934640526 0.562744140625 0.1409313725490196 0.15576171875
2 0.7087418300653595 0.517578125 0.10866013071895425 0.240234375

下面是两种数据格式的官方转换代码voc_label.py

import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join

sets=[('2012', 'train'), ('2012', 'val'), ('2007', 'train'), ('2007', 'val'), ('2007', 'test')]

####################################################################
#这里需要认为手动指定xml标签中的全部类别,这样程序把这些类作为键去所以后续的数据#
####################################################################
classes = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]

#我们标注的图像尺寸可能不同,但是送进模型训练需要统一的尺寸,
#所以这里我们要将标注数据又之前的实际像素点的位置变为占整张图尺寸的比例,
#这样在后续的各种resize中才能保证框的位置准确
def convert(size, box):
    dw = 1./(size[0])
    dh = 1./(size[1])
    x = (box[0] + box[1])/2.0 - 1
    y = (box[2] + box[3])/2.0 - 1
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x*dw
    w = w*dw
    y = y*dh
    h = h*dh
    return (x,y,w,h)
#这里是读取xml文件中内容并将转换好的信息写入txt文件的过程,
#主要就是索引前面classes中的名字将名字转换成编号再写入txt
def convert_annotation(year, image_id):
    in_file = open('VOCdevkit/VOC%s/Annotations/%s.xml'%(year, image_id))
    out_file = open('VOCdevkit/VOC%s/labels/%s.txt'%(year, image_id), 'w')
    tree=ET.parse(in_file)
    root = tree.getroot()
    size = root.find('size')
    w = int(size.find('width').text)
    h = int(size.find('height').text)

    for obj in root.iter('object'):
        difficult = obj.find('difficult').text
        cls = obj.find('name').text
        if cls not in classes or int(difficult)==1:
            continue
        cls_id = classes.index(cls)
        xmlbox = obj.find('bndbox')
        b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))
        bb = convert((w,h), b)
        out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')

wd = getcwd()

for year, image_set in sets:
    if not os.path.exists('VOCdevkit/VOC%s/labels/'%(year)):
        os.makedirs('VOCdevkit/VOC%s/labels/'%(year))
    image_ids = open('VOCdevkit/VOC%s/ImageSets/Main/%s.txt'%(year, image_set)).read().strip().split()
    list_file = open('%s_%s.txt'%(year, image_set), 'w')
    for image_id in image_ids:
        list_file.write('%s/VOCdevkit/VOC%s/JPEGImages/%s.jpg\n'%(wd, year, image_id))
        convert_annotation(year, image_id)
    list_file.close()

os.system("cat 2007_train.txt 2007_val.txt 2012_train.txt 2012_val.txt > train.txt")
os.system("cat 2007_train.txt 2007_val.txt 2007_test.txt 2012_train.txt 2012_val.txt > train.all.txt")

以上是对官方代码的解析,非小白可以忽略!
以上的代码中有一处需要手动修改 ,那就是classes值,也就是我们要提前知道这批数据中的标注名称都是什么。但是有些时候你可能会拿到你不知道都有那些类的标注数据,或者单纯懒不想改写…,一下修改可以自动读取你全部的xml标注并自动导入到classes中,减少需要手动修改的环节,上代码:

classes = []  #设定classes为空列表
def gen_classes(year, image_id):

    #读取原始标注的xml文件信息
    in_file = open('VOCdevkit-car/VOC%s/Annotations/%s.xml'%(year, image_id))
    tree = ET.parse(in_file)
    root = tree.getroot()
    #索引到xml中的object关键字
    for obj in root.iter('object'):
    #读取xml中的”name“并把他的值赋值给cls_name,name中的值就是标注的类别信息
        cls_name = obj.find('name').text
        #如果新读取的类别名称没有存在于cls_name中则添加进去,存在则忽略
        if cls_name in classes:
            pass
        else:
            classes.append(cls_name)
            print("classes name is :", classes)
    return classes

完整代码如下

import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join

sets=[ ('2007', 'train'), ('2007', 'val'), ('2007', 'test')]


classes = []
def gen_classes(year, image_id):

    in_file = open('VOCdevkit-car/VOC%s/Annotations/%s.xml'%(year, image_id))
    tree = ET.parse(in_file)
    root = tree.getroot()
    
    for obj in root.iter('object'):
        cls_name = obj.find('name').text
        if cls_name in classes:
            pass
        else:
            classes.append(cls_name)
            print("classes name is :", classes)
    return classes

def convert(size, box):
    dw = 1./(size[0])
    dh = 1./(size[1])
    x = (box[0] + box[1])/2.0 - 1
    y = (box[2] + box[3])/2.0 - 1
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x*dw
    w = w*dw
    y = y*dh
    h = h*dh
    return (x,y,w,h)

def convert_annotation(year, image_id):
    in_file = open('VOCdevkit-car/VOC%s/Annotations/%s.xml'%(year, image_id))
    out_file = open('VOCdevkit-car/VOC%s/labels/%s.txt'%(year, image_id), 'w')
    tree=ET.parse(in_file)
    root = tree.getroot()
    size = root.find('size')
    w = int(size.find('width').text)
    h = int(size.find('height').text)

    for obj in root.iter('object'):
        difficult = obj.find('difficult').text
        cls = obj.find('name').text
        if cls not in classes or int(difficult)==1:
            continue
        cls_id = classes.index(cls)
        xmlbox = obj.find('bndbox')
        b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))
        bb = convert((w,h), b)
        out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')

wd = getcwd()

for year, image_set in sets:
    if not os.path.exists('VOCdevkit-car/VOC%s/labels/'%(year)):
        os.makedirs('VOCdevkit-car/VOC%s/labels/'%(year))
    image_ids = open('VOCdevkit-car/VOC%s/ImageSets/Main/%s.txt'%(year, image_set)).read().strip().split()
    list_file = open('%s_%s.txt'%(year, image_set), 'w')
    for image_id in image_ids:
        list_file.write('%s/VOCdevkit-car/VOC%s/JPEGImages/%s.jpg\n'%(wd, year, image_id))
        gen_classes(year, image_id)
        convert_annotation(year, image_id)
    list_file.close()

#os.system("cat 2007_train.txt 2007_val.txt 2012_train.txt 2012_val.txt > train.txt")
#os.system("cat 2007_train.txt 2007_val.txt 2007_test.txt 2012_train.txt 2012_val.txt > train.all.txt")

你可能感兴趣的:(目标检测yolo标签全自动生成(voc_label.py程序修改))