您的位置:首页 > 其它

将数据集制作成VOC数据集格式的实例

2020-02-17 21:06 447 查看

在做目标检测任务时,若使用Github已复现的论文时,需首先将自己的数据集转化为VOC数据集的格式,因为论文作者使用的是公开数据集VOC 2007、VOC2012、COCO等类型数据集做方法验证与比对。

一、VOC数据集格式

--VOCdevkit2007

--VOC2007

--Annotations (xml格式的文件)

--000001.xml

--ImageSets

--Layout

--Main

--train.txt

--test.txt

--val.txt

--trainval.txt

--Segmentation

--JPEGImages (训练集和测试集图片)

--000001.jpg

--results

二、转换过程步骤

1. 使用标注工具标注图片目标检测框,生成JSON格式的标注文件(本人使用此生成类型的标注工具,也可使用(LabelImg等标注工具);

2. 批量修改图片和标注文件名称,从000001.jpg、000001.json标号开始;

#coding='utf-8'
import os
import numpy as np

def imgs_rename(imgs_path):
imgs_labels_name = np.array(os.listdir(imgs_path)).reshape(-1,2)
# 从 000001开始
i = 1
for img_label_name in imgs_labels_name:
if img_label_name[0].endswith('.jpg'):
# 修改图片名称
img_old_name = os.path.join(os.path.abspath(imgs_path), img_label_name[0])
# 类别+图片编号  format(str(i),'0>3s') 填充对齐
img_new_name = os.path.join(os.path.abspath(imgs_path), '00' + format(str(i),'0>4s') + '.jpg')
os.rename(img_old_name, img_new_name)
# 修改json文件名称
label_old_name = os.path.join(os.path.abspath(imgs_path), img_label_name[1])
label_new_name = os.path.join(os.path.abspath(imgs_path), '00' + format(str(i), '0>4s') + '.json')
os.rename(label_old_name, label_new_name)
i = i + 1

if __name__=='__main__':
# 读取json文件的路径
root = "read_file_path"

imgs_rename(root)

3. 提取图片和标注文件到不同文件夹下,并将读取的标注框转化为txt文件格式(本人的图片和JSON文件在同一目录下生成);

import json
import os
import numpy as np
import cv2

#读取json格式文件,返回坐标
def read_json(file_name):
file = open(file_name,'r',encoding='utf-8')
set = json.load(file)
# print("读取完整信息:",set)
coord = set['objects'][0]['seg'] # 只读取第一个标注的车牌
return coord

def save_imgs(imgs_jsons_files, imgs_path):
# 提取图片文件夹中的jpg文件名称
for idx in range(len(imgs_jsons_list)):
if imgs_jsons_list[idx][-3:]=='jpg':
img_name = imgs_jsons_list[idx]
read_img_path = os.path.join(imgs_jsons_files, img_name)
img = cv2.imread(read_img_path)
save_img_path = os.path.join(imgs_path, img_name)
cv2.imwrite(save_img_path, img)

def save_labels(imgs_jsons_files, labels_path):
# 提取图片文件夹中的json文件名称
for idx in range(len(imgs_jsons_list)):
if imgs_jsons_list[idx][-4:] == 'json':
json_name = imgs_jsons_list[idx]

# 操作每一个json文件,读取并保存坐标
json_path = os.path.join(imgs_jsons_files, json_name)
json_coord = read_json(json_path)
if len(json_coord) > 8:
print("标注坐标多于四个点的文件名称:", json_name)

# 提取左上和右下坐标
roi_coord = []
for idx in range(len(json_coord)):
if idx == 0 or idx == 1 or idx == 4 or idx == 5:
roi_coord.extend([json_coord[idx]])
# 保存roi坐标到txt文件中
label_path = labels_path + json_name[:6] + '.txt'
np.savetxt(label_path, roi_coord)

if __name__=='__main__':
print("loading......")
# 读取jpg json文件的路径
imgs_jsons_files = "Jpg_json_file_path"

# 保存读取的真实标签路径
labels_path = "save_labels_path"
if not os.path.exists(labels_path):
os.mkdir(labels_path)
# 保存读取的图片
imgs_path = "sabe_imgs_path"
if not os.path.exists(imgs_path):
os.mkdir(imgs_path)

imgs_jsons_list = os.listdir(imgs_jsons_files)

save_imgs(imgs_jsons_files, imgs_path)
save_labels(imgs_jsons_files, labels_path)
print("done!!!")

4. 转化标注框txt格式为xml格式;

# encoding = utf-8
import os
import numpy as np
import codecs
import cv2

def read_txt(label_path):
file = open(label_path,'r',encoding='utf-8')
label_lines = file.readlines()
label = []
for line in label_lines:
one_line = float(line.strip().split('\n')[0])
label.extend([one_line])
return np.array(label,dtype=np.float64)

def covert_xml(label,xml_path, img_name, img_path):
# 获得图片信息
img = cv2.imread(img_path)
height, width, depth = img.shape
x_min,y_min,x_max,y_max = label

xml = codecs.open(xml_path, 'w', encoding='utf-8')
xml.write('<annotation>\n')
xml.write('\t<folder>' + 'VOC2007' + '</folder>\n')
xml.write('\t<filename>' + img_name + '</filename>\n')
xml.write('\t<source>\n')
xml.write('\t\t<database>The VOC 2007 Database</database>\n')
xml.write('\t\t<annotation>Pascal VOC2007</annotation>\n')
xml.write('\t\t<image>flickr</image>\n')
xml.write('\t\t<flickrid>NULL</flickrid>\n')
xml.write('\t</source>\n')
xml.write('\t<owner>\n')
xml.write('\t\t<flickrid>NULL</flickrid>\n')
xml.write('\t\t<name>faster</name>\n')
xml.write('\t</owner>\n')
xml.write('\t<size>\n')
xml.write('\t\t<width>' + str(width) + '</width>\n')
xml.write('\t\t<height>' + str(height) + '</height>\n')
xml.write('\t\t<depth>' + str(depth) + '</depth>\n')
xml.write('\t</size>\n')
xml.write('\t\t<segmented>0</segmented>\n')
xml.write('\t<object>\n')
xml.write('\t\t<name>plate</name>\n')
xml.write('\t\t<pose>Unspecified</pose>\n')
xml.write('\t\t<truncated>0</truncated>\n')
xml.write('\t\t<difficult>0</difficult>\n')
xml.write('\t\t<bndbox>\n')
xml.write('\t\t\t<xmin>' + str(x_min) + '</xmin>\n')
xml.write('\t\t\t<ymin>' + str(y_min) + '</ymin>\n')
xml.write('\t\t\t<xmax>' + str(x_max) + '</xmax>\n')
xml.write('\t\t\t<ymax>' + str(y_max) + '</ymax>\n')
xml.write('\t\t</bndbox>\n')
xml.write('\t</object>\n')
xml.write('</annotation>')

if __name__=='__main__':
labels_file_path = "D:/Code_py/VOC2007/labels/"
imgs_file_path = "D:/Code_Py/VOC2007/imgs/"

xmls_file_path = "D:/Code_py/VOC2007/xmls/"
if not os.path.exists(xmls_file_path):
os.mkdir(xmls_file_path)

labels_name = os.listdir(labels_file_path)
for label_name in labels_name:
label_path = os.path.join(labels_file_path, label_name)
label = read_txt(label_path)

xml_name = label_name[:6]+'.xml'
xml_path = os.path.join(xmls_file_path, xml_name)

img_name = label_name[:6]+'.jpg'
img_path = os.path.join(imgs_file_path, img_name)

covert_xml(label, xml_path, img_name, img_path)

5. 切分数据集为训练集、验证集和测试集,仅保存图片的名称到txt问价下即可;

import os
import numpy as np

if __name__=='__main__':
root = "save_path"
train = open(root+"train.txt", 'w', encoding='utf-8')
train_val = open(root+"trainval.txt", 'w', encoding='utf-8')
test = open(root+"test.txt", 'w', encoding='utf-8')
val = open(root+"val.txt", 'w', encoding='utf-8')

imgs_path = os.path.join(root, "imgs")

imgs_name = os.listdir(imgs_path)

# 首先切分训练验证集和测试集
train_val_img_info = []
for img_name in imgs_name:
x = np.random.uniform(0,1)
img_info = str(img_name).strip().split('.')[0]
# 随机选取1/2比例的数据为测试集
if x>0.5:
train_val_img_info.append(img_info)
train_val.writelines(img_info)
else:
test.writelines(img_info+'\n')

# 然后切分训练验证集为训练集和验证集
for img_name in train_val_img_info:
x = np.random.uniform(0,1)
if x>0.5:
train.writelines(img_name+'\n')
else:
val.writelines(img_name+'\n')

以上这篇将数据集制作成VOC数据集格式的实例就是小编分享给大家的全部内容了,希望能给大家一个参考

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据集 VOC 格式