VOC2007格式
-VOC2007 建项目名称
–Annotations 为图像标注信息xml文件
–ImageSets 为训练集、测试集、验证、训练验证集图像名的txt文件
–JPEGImages 为原始的图片
新建项目名称,如VOC2007/mkdir.py
import os
def mkdir(path):
# 去除首位空格
path = path.strip()
# 去除尾部 \ 符号S
path = path.rstrip("\\")
# 判断路径是否存在
# 存在 True
# 不存在 False
isExists = os.path.exists(path)
# 判断结果
if not isExists:
# 如果不存在则创建目录
# 创建目录操作函数
os.makedirs(path)
print(path + ' 创建成功')
return True
else:
# 如果目录存在则不创建,并提示目录已存在
print(path + ' 目录已存在')
return False
if __name__=='__main__':
rootPath = os.getcwd()
mkdir(os.path.join(rootPath, 'Annotations'))
mkdir(os.path.join(rootPath,'ImageSets'))
mkdir(os.path.join(rootPath,'JPEGImages'))
mkdir(os.path.join(rootPath,'ImageSets/Main'))
VOC2007/matxt.py 将图片分成训练集和测试集(1)
import os
import random
trainval_percent = 0.1
train_percent = 0.9
xmlfilepath = './Annotations'
txtsavepath = './ImageSets/Main/'
total_xml = os.listdir(xmlfilepath)
num = len(total_xml)
list = range(num)
tv = int(num * trainval_percent)
tr = int(tv * train_percent)
trainval = random.sample(list, tv)
train = random.sample(trainval, tr)
ftrainval = open(txtsavepath+'trainval.txt', 'w')
ftest = open(txtsavepath+'test.txt', 'w')
ftrain = open(txtsavepath+'train.txt', 'w')
fval = open(txtsavepath+'val.txt', 'w')
for i in list:
name = total_xml[i][:-4] + '\n'
if i in trainval:
ftrainval.write(name)
if i in train:
ftest.write(name)
else:
fval.write(name)
else:
ftrain.write(name)
ftrainval.close()
ftrain.close()
fval.close()
ftest.close()
这里得到的train.txt test.txt只有图片的名称。
VOC2007/matxt.py 带路径的训练集和labels(2)
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
sets=['train', 'val', 'test']
classes = ["person"]
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(image_id):
in_file = open('./Annotations/%s.xml'%(image_id))
out_file = open('./labels/%s.txt'%(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()
print(wd)
for image_set in sets:
if not os.path.exists('./labels/'):
os.makedirs('./labels/')
image_ids = open('./ImageSets/Main/%s.txt'%(image_set)).read().strip().split()
list_file = open('VOC2007_%s.txt'%(image_set), 'w')
for image_id in image_ids:
list_file.write('%s/JPEGImages/%s.jpg\n'%(wd, image_id))
convert_annotation(image_id)
list_file.close()
有时候不能使用,或者生成的labels没有信息,此时需要注意一下路径问题。
标注采用yolo格式(txt格式文件)
使用labelimg 标注,
PascalVoc—>xml
YOLO -->txt (txt的信息直接放在labels文件中)
上述的样本集制作中的地一个脚本正常运行,第二个脚本需要简单的修改
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
sets=['train', 'val', 'test']
classes = ["person"]
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(image_id):
in_file = open('./Annotations/%s.xml'%(image_id))
out_file = open('./labels/%s.txt'%(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()
print(wd)
for image_set in sets:
# if not os.path.exists('./labels/'): # TXT存储位置创建
# os.makedirs('./labels/')
image_ids = open('./ImageSets/Main/%s.txt'%(image_set)).read().strip().split()
list_file = open('VOC2007_%s.txt'%(image_set), 'w')
for image_id in image_ids:
list_file.write('%s/JPEGImages/%s.jpg\n'%(wd, image_id))
# convert_annotation(image_id) # XML 转换成TXT
list_file.close()