zoukankan      html  css  js  c++  java
  • 提取文件夹中所有xml文件中的数据到txt(为人脸识别级联器使用的txt做准备)

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

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

     

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

    #include"pugixmlpugixml.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
    ", 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;    
    }
  • 相关阅读:
    mysql修改表
    MySQL sql优化(摘抄自文档)
    mysql show操作
    mysql load data infile
    mysql Insert强化
    mysql group_concat
    HTML js 复习
    mysql开发实战8问
    不使用Ajax,如何实现表单提交不刷新页面
    跨域的方式总结
  • 原文地址:https://www.cnblogs.com/xixixing/p/12358053.html
Copyright © 2011-2022 走看看