VOC格式数据集制作(YOLOv3 可用)

参考链接:使用labelImg制作自己的数据集(VOC2007格式)用于Faster-RCNN训练

VOC格式的数据集由五个文件夹组成

将所有的图片放入JPEGImages并重命名

下面是重命名的C++源码

#include <iostream>  
#include <io.h>  //对系统文件进行操作的头文件
#include <string>  
#include <sstream>
#include<vector>

using namespace std;

const int N = 6;   //整型格式化输出为字符串后的长度,例如,N=6,则整型转为长度为6的字符串,12转为为000012

const string FileType = ".jpg";    // 需要查找的文件类型

								   /* 函数说明 整型转固定格式的字符串
								   输入:
								   n 需要输出的字符串长度
								   i 需要结构化的整型
								   输出:
								   返回转化后的字符串
								   */
string int2string(int n, int i)
{
	char s[BUFSIZ];
	printf(s, "%d", i);
	int l = strlen(s);  // 整型的位数

	if (l > n)
	{
		cout << "整型的长度大于需要格式化的字符串长度!";
	}
	else
	{
		stringstream M_num;
		for (int i = 0; i < n - l; i++)
			M_num << "0";
		M_num << i;

		return M_num.str();
	}
}

int main()
{
	_finddata_t c_file;   // 查找文件的类

	string File_Directory = "E:\\datasets";   //文件夹目录

	string buffer = File_Directory + "\\*" + FileType;

	//long hFile;  //win7系统,_findnext()返回类型可以是long型
	intptr_t hFile;   //win10系统 ,_findnext()返回类型为intptr_t ,不能是long型

	hFile = _findfirst(buffer.c_str(), &c_file);   //找第一个文件

	if (hFile == -1L)   // 检查文件夹目录下存在需要查找的文件
		printf("No %s files in current directory!\n", FileType);
	else
	{
		printf("Listing of files:\n");

		int i = 0;
		string newfullFilePath;
		string oldfullFilePath;
		string str_name;
		do
		{
			oldfullFilePath.clear();
			newfullFilePath.clear();
			str_name.clear();

			//旧名字
			oldfullFilePath = File_Directory + "\\" + c_file.name;

			//新名字
			++i;
			str_name = int2string(N, i);    //整型转字符串
			newfullFilePath = File_Directory + "\\" + str_name + FileType;

			/*重命名函数rename(const char* _OldFileName,const char* _NewFileName)
			第一个参数为旧文件路径,第二个参数为新文件路径*/
			int c = rename(oldfullFilePath.c_str(), newfullFilePath.c_str());

			if (c == 0)
				puts("File successfully renamed");
			else
				perror("Error renaming file");

		} while (_findnext(hFile, &c_file) == 0);  //如果找到下个文件的名字成功的话就返回0,否则返回-1  
		_findclose(hFile);
	}

	return 0;
}

重命名完的图片信息如下图所示

 

使用windows版本的windows_labelimg工具(windows下不需要任何操作),进行xml文件的生成

给个工具链接如下(百度网盘)

Windows ImageLabel

密码:ra4q

XML文件保存在Annotations文件夹中

在VOC文件夹下重新建立一个python脚本文件,如下所示

import os
import random


def _main():
    trainval_percent = 0.1
    train_percent = 0.9
    xmlfilepath = 'Annotations'
    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('ImageSets/Main/trainval.txt', 'w')
    ftest = open('ImageSets/Main/test.txt', 'w')
    ftrain = open('ImageSets/Main/train.txt', 'w')
    fval = open('ImageSets/Main/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()


if __name__ == '__main__':
    _main()

利用shell命令行运行python脚本,就会在ImageSets的Main文件夹下生成四个文件

自此,就已经成功的制作成了VOC数据集

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值