码迷,mamicode.com
首页 > 其他好文 > 详细

提取文件夹中所有xml文件中的数据到txt(为人脸识别级联器使用的txt做准备)

时间:2020-02-24 18:32:18      阅读:57      评论:0      收藏:0      [点我收藏+]

标签:int()   img   cas   logs   mes   多个   检索   目标   文件内容   

【知识点】pugixml框架提取多个相同节点内容、遍历文件夹、保存到txt。

xml文件由labelImg.exe软件标定图像生成。此博客把xml文件内容提取到txt中,为人脸识别opencv_annotation.exe、opencv_createsamples.exe、opencv_traincascade.exe服务

技术图片 

所有xml放到一个文件夹中,代码功能:遍历此文件夹,提取xml内容,保存到txt

#include"pugixml\pugixml.hpp"
#include<iostream>
#include<fstream> //检索文件夹
#include<io.h> //存储到txt

using namespace pugi;
using namespace std;

int main()
{
    ofstream OpenFile("file.txt");

    //目标文件夹路径
    string inPath = "D:/XMLtoTXT/xmlDataTrain/*.xml";//遍历文件夹下的所有.xml文件
    //用于查找的句柄
    long long handle=0;//这个地方需要特别注意,win10用户必须用long long 类型,win7可以用long类型
    struct _finddata_t fileinfo; //存储检索到的文件信息
    //第一次查找
    handle = _findfirst(inPath.c_str(), &fileinfo);
    if (handle == -1)    return -1;
    do
    {        
        //printf("%s\n", fileinfo.name);//找到的文件的文件名
        xml_document doc;
        string path(fileinfo.name);//char[]类型转string,为了方便字符串连接
        string path = "./xmlDataTrain/"+ path;
        doc.load_file(path.c_str());//c_str(),string转char[]

        xml_node nodes = doc.child("annotation");
        OpenFile << nodes.child("path").text().get(); //输出到txt
        int i = 0; //框选的缺陷个数,即object标签个数
        for (xml_node node = nodes.first_child(); node; node = node.next_sibling())
        {
            string name = node.name();
            if (name == "object")
            {
                i++;
            }
        }
        OpenFile << " " << i; //输出到txt
        for (xml_node node = nodes.first_child(); node; node = node.next_sibling())
        {
            string name = node.name();
            if (name == "object")
            {
                int x = node.child("bndbox").child("xmin").text().as_int();
                int y = node.child("bndbox").child("ymin").text().as_int();
                int x2 = node.child("bndbox").child("xmax").text().as_int();
                int y2 = node.child("bndbox").child("ymax").text().as_int();
                int w = x2 - x;
                int h = y2 - y;
                OpenFile << " " << x << " " << y << " " << w << " " << h; //输出到txt
            }
        }
        OpenFile << endl;    

    } while (!_findnext(handle, &fileinfo));
    OpenFile.close(); //关闭txt
    _findclose(handle);

    return 0;    
}

 

提取文件夹中所有xml文件中的数据到txt(为人脸识别级联器使用的txt做准备)

标签:int()   img   cas   logs   mes   多个   检索   目标   文件内容   

原文地址:https://www.cnblogs.com/xixixing/p/12358053.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!