打包属于自己数据的PASCAL-VOC-2012目标检测数据集
Pascal voc 2012数据集可以用于分类、检测和分割。这篇文章主要介绍如何将自己的数据集打包为PASCAL-VOC-2012格式。
原始的Pascal voc 2012数据集仅仅包含1464个训练数据集,可在官网 | 地址下载。其文件目录如下: |
+ VOCdevkit
+ VOC2012
+ Annotations # 存放目标检测的标注XML文件
+ ImageSets
+ main # 包括trainval、train和val 这3个txt文件主要描述训练集及验证集的文件划分
+ JPEGImages # 存放目标检测的原始图像
+ SegmentationClass # 语义分割任务中用到的label图片
+ SegmentationObject # 实例分割任务用到的label图片
Step 1 :图像标注与预处理
- 将图像统一转格式为jpeg格式,并将维度降为3维。
import cv2
import numpy as np
import [os.path](https://docs.python.org/3/library/os.path.html)
from [shutil](https://docs.python.org/3/library/shutil.html) import copy, rmtree
# 旋转
def rotate(image, angle, center=None, scale=1.0):
(h, w) = image.shape[:2]
if center is None:
center = (w / 2, h / 2)
m = cv2.getRotationMatrix2D(center, angle, scale)
rotated = cv2.warpAffine(image, m, (w, h))
return rotated
cla_path = "D:/kidney-detection/data/VOCdevkit/VOC2012/JPEGImage"
target_path = "D:/kidney-detection/data/VOCdevkit/VOC2012/JPEGImages"
images = os.listdir(cla_path)
for img_name in images:
img = cv2.imread(cla_path + "/" + img_name)
num = len(images)
rotated_360 = rotate(img, 360)
cv2.imwrite(target_path + "/" + img_name[0:-4] + '.jpg', rotated_360)
- 将图像导入label-studio,选定模板进行标注完成后点击导出时按pascal voc 2012格式导出。
- 将imges、Annotations文件夹内文件分别复制到pascal voc 2012模板目录内的JPEGImages、Annotations文件夹。
Step 2 :建立文件索引
- 统计文件夹内图像,建立文件索引,按比例划分训练集及验证集。
import os
import os.path
rootdir = "D:/迅雷下载/VOCtrainval_11-May-2012/VOCdevkit/VOC2012/JPEGImages"
file_object = open('D:/迅雷下载/VOCtrainval_11-May-2012/VOCdevkit/VOC2012/ImageSets/Main/trainval.txt','w')
for parent,dirnames,filenames in os.walk(rootdir):
for filename in filenames:
print(filename)
if ".jpg" in filename:
fileName= os.path.splitext(filename)[0]
print(fileName)
file_object.write(fileName + '\n')
file_object.close()
"""
将数据按比例切分
"""
from sklearn import model_selection
c = []
j = 0
#filename = r'E:/NER/CCKS2020/Data/ccks2020_2_task1_train/task1_train.txt'
filename = open(r'D:/迅雷下载/VOCtrainval_11-May-2012/VOCdevkit/VOC2012/ImageSets/Main/trainval.txt','r',encoding='utf-8')
out_train = open(r'D:/迅雷下载/VOCtrainval_11-May-2012/VOCdevkit/VOC2012/ImageSets/Main/train.txt', 'w',encoding='utf-8')
out_test = open(r'D:/迅雷下载/VOCtrainval_11-May-2012/VOCdevkit/VOC2012/ImageSets/Main/val.txt', 'w',encoding='utf-8')
for line in filename:
# items = line.strip().split()
c.append(line)
c_train, c_test = model_selection.train_test_split(c, test_size=0.2)
for i in c_train:
out_train.write(i)
for i in c_test:
out_test.write(i)