目录
一、了解VOC数据格式
1、Annotations目录
2. JPEGImages目录
二、YOLO格式
三、VOC标签格式转yolo格式并划分训练集和测试集
一、了解VOC数据格式
Pascal VOC数据集下载地址:The PASCAL Visual Object Classes Homepage
介绍一下VOC 数据集下载后的目录结构:
1、Annotations目录
目录存放xml文件:
Annotations文件夹中存放的是xml格式的标签文件,每一个xml文件都对应于JPEGImages文件夹中的一张图片,包含了图片的重要信息:图片的名称,图片中object的类别及其bounding box坐标。
文件内容如下
<annotation>
<folder>images</folder>
<filename>HW57-2 (3).jpg</filename>
<path>C:\Users\lrj\Pictures\开裂标定\images\HW57-2 (3).jpg</path>
<source>
<database>Unknown</database>
</source>
<size>
<width>1008</width>
<height>1397</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>close</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>133</xmin>
<ymin>748</ymin>
<xmax>244</xmax>
<ymax>854</ymax>
2. JPEGImages目录
存放的是数据集的原图片,像素尺寸大小不一。这里是自己的数据集。
二、YOLO格式
yolo标注格式保存在.txt文件中,一共5个数据,用空格隔开,举例说明如下图所示:
三、VOC标签格式转yolo格式并划分训练集和测试集
标注数据最好选择VOC格式,因为VOC格式包含更多的信息。
下面介绍格式转换:
代码运行结果:
产生一个VOCdevkit目录,其下包含多个目录,其中YOLOLables文件夹是存储所有转换好的yolo标签文件,其他的目录或看文件夹名便知,或已在前面介绍过
转换代码:
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
import random
from shutil import copy
classes = ["open", "close"] # 自己标注的数据集的类别
#classes=["ball"]
def convert(size, box):
dw = 1./size[0]
dh = 1./size[1]
x = (box[0] + box[1])/2.0
y = (box[2] + box[3])/2.0
w = box[1] - box[0]
h = box[3] - box[2]
x = x*dw # 中心点横坐标与图像宽度比值
w = w*dw # bbox宽度与图像宽度比值
y = y*dh # 中心点纵坐标与图像高度比值
h = h*dh # bbox高度与图像高度比值
return (x,y,w,h)
def convert_annotation(input, output):
in_file = open(input)
out_file = open(output, 'w')
tree=ET.parse(in_file) # Python xml 读取
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'): # 对于每个bbox
difficult = obj.find('difficult').text
cls = obj.find('name').text # 目标类别
if cls not in classes or int(difficult) == 1: # 如果类别错误,则continue
continue
cls_id = classes.index(cls) # 获得类别索引
xmlbox = obj.find('bndbox') # bbpx
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) # voc转yolo
out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n') # cls_id centerx centery w h
in_file.close()
out_file.close()
def generate_diretorys(wd):
data_base_dir = os.path.join(wd, "VOCdevkit/")
os.mkdir(data_base_dir)
work_sapce_dir = os.path.join(data_base_dir, "VOC2007/")
os.mkdir(work_sapce_dir)
annotation_dir = os.path.join(work_sapce_dir, "Annotations/")
os.mkdir(annotation_dir)
image_dir = os.path.join(work_sapce_dir, "JPEGImages/")
os.mkdir(image_dir)
yolo_labels_dir = os.path.join(work_sapce_dir, "YOLOLabels/")
os.mkdir(yolo_labels_dir)
yolov5_images_dir = os.path.join(data_base_dir, "images/")
os.mkdir(yolov5_images_dir)
yolov5_labels_dir = os.path.join(data_base_dir, "labels/")
os.mkdir(yolov5_labels_dir)
yolov5_images_train_dir = os.path.join(yolov5_images_dir, "train/")
os.mkdir(yolov5_images_train_dir)
yolov5_images_test_dir = os.path.join(yolov5_images_dir, "val/")
os.mkdir(yolov5_images_test_dir)
yolov5_labels_train_dir = os.path.join(yolov5_labels_dir, "train/")
os.mkdir(yolov5_labels_train_dir)
yolov5_labels_test_dir = os.path.join(yolov5_labels_dir, "val/")
os.mkdir(yolov5_labels_test_dir)
def transform(images_dir, annotations_dir, split_val_rate, wd):
train_file = open(os.path.join(wd, "yolov5_train.txt"), 'w') # 记载训练集图片目录的txt文件
val_file = open(os.path.join(wd, "yolov5_val.txt"), 'w') # # 记载测试集图片目录的txt文件
train_file.close()
val_file.close()
train_file = open(os.path.join(wd, "yolov5_train.txt"), 'a')
val_file = open(os.path.join(wd, "yolov5_val.txt"), 'a')
assert os.path.exists(images_dir), "path '{}' does not exist.".format(images_dir)
assert os.path.exists(annotations_dir), "path '{}' does not exist.".format(annotations_dir)
assert os.path.exists(os.path.join(wd, "VOCdevkit", "labels", "val")), "val path does not exist"
assert os.path.exists(os.path.join(wd, "VOCdevkit", "labels", "train")), "train path does not exist"
list_imgs = os.listdir(images_dir) # list image files 所有图片名字
random.seed(0)
num = len(list_imgs)
eval_index = random.sample(list_imgs, k=int(num*split_val_rate))
another_images_dir = os.path.join(wd, "VOCdevkit", "VOC2007", "JPEGImages")
assert os.path.exists(another_images_dir), "dir '{}' does not exist".format(another_images_dir)
another_yolo_labels_dir = os.path.join(wd, "VOCdevkit", "VOC2007", "YOLOLabels")
assert os.path.exists(another_yolo_labels_dir), "dir '{}' does not exist".format(another_yolo_labels_dir)
another_Annotations_dir = os.path.join(wd, "VOCdevkit", "VOC2007", "Annotations")
assert os.path.exists(another_Annotations_dir), "'{}' path does not exist".format(another_Annotations_dir)
for index, image in enumerate(list_imgs):
if image in eval_index:
image_path = os.path.join(images_dir, image)
new_image_path = os.path.join(wd, "VOCdevkit","images", "val", image)
copy(image_path, new_image_path)
image_id, extention = os.path.splitext(image)
annotation_name = image_id + ".xml"
annotation_dir = os.path.join(annotations_dir, annotation_name)
new_annation_name = image_id + ".txt"
new_annotation_dir = os.path.join(wd, "VOCdevkit", "labels", "val", new_annation_name)
convert_annotation(annotation_dir,new_annotation_dir)
val_file.write(new_image_path +"\n")
copy(image_path, another_images_dir)
copy(new_annotation_dir, another_yolo_labels_dir)
copy(annotation_dir, another_Annotations_dir)
else:
image_path = os.path.join(images_dir, image)
new_image_path = os.path.join(wd, "VOCdevkit","images", "train", image)
copy(image_path, new_image_path)
image_id, extention = os.path.splitext(image)
annotation_name = image_id + ".xml"
annotation_dir = os.path.join(annotations_dir, annotation_name)
new_annation_name = image_id + ".txt"
new_annotation_dir = os.path.join(wd, "VOCdevkit", "labels", "train", new_annation_name)
convert_annotation(annotation_dir,new_annotation_dir)
train_file.write(new_image_path + "\n")
copy(image_path, another_images_dir)
copy(new_annotation_dir, another_yolo_labels_dir)
copy(annotation_dir, another_Annotations_dir)
print("\r processing [{}/{}]".format(index+1, num), end="")
train_file.close()
val_file.close()
def check_img_label(images_dir, labels_dir):
assert os.path.exists(images_dir), "'{}' does not exist!".format(images_dir)
assert os.path.exists(labels_dir), "'{}' dose not exist!".format(labels_dir)
list_imgs = os.listdir(images_dir) # list image files 所有图片名字
for img in list_imgs:
img_id, extension = os.path.splitext(img)
img_dir = os.path.join(images_dir, img)
label_name = img_id + ".xml"
label_dir = os.path.join(labels_dir, label_name)
if not os.path.exists(labels_dir):
os.remove(img_dir)
os.remove(label_dir)
if __name__ == "__main__":
wd = "/home/jason/work/my-datasets/"
generate_diretorys(wd=wd) # 生成多级目录
raw_images_dir = "/home/jason/work/my-datasets/images" # 图片所在目录
raw_annotions_dir = "/home/jason/work/my-datasets/annotions" # voc 格式标签文件坐在目录
check_img_label(images_dir=raw_images_dir, labels_dir=raw_annotions_dir) # 检查图片名字与标签文件名字是否一一对应
transform(images_dir=raw_images_dir, annotations_dir=raw_annotions_dir,split_val_rate=0.2, wd=wd) # VOC转yolo,并划分训练集、测试集
参考:
目标检测数据集标注-VOC格式_AI学长的博客-CSDN博客
YOLO数据格式说明与转换_yolo格式_lokvke的博客-CSDN博客