繁体   English   中英

如何使用“ PASCAL批注”文件通过C ++训练SVM

[英]How to use “PASCAL Annotation” files to train SVM with C++

我需要在科学数据集(INRIA人数据集)上使用猪描述符(Dalal paper)测试我的手写人检测器。 我的文件夹中有数千张pos图片和neg图片,用于训练支持向量机(SVM)。 但是,要将图像标记为正数(1.0)或负数(-1.0),我需要从数据集随附的文本文件中读取信息,该文本文件的格式为“ PASCAL批注”。

我的问题是我不知道如何有效地读取此格式。 我正在使用C ++和OpenCV。 有谁知道如何有效地做到这一点? 是否已经有C ++的代码片段?

最后,我需要一个遍历文件“ Annotations.lst”的循环,其中列出了所有图片文件名。 程序加载图片和相应的注释文件(picturename.txt),以查看该图片是属于正训练数据还是负训练数据(或稍后在实际检测时:被测图片是否属于检测到的人)

谢谢你的帮助!

也许这不是最好的实现,但是效果很好,希望它在今天仍然有用! 您将需要文件系统库才能使用文件。

string line,value; //Line stores lines of the file and value stores characters of the line
int i=0; //Iterate through lines
int j=0; //Iterate through characters
int n=0; //Iterate through ,()-...
char v; //Stores variable value as a char to be able to make comparisions easily

vector <Rect> anotations; //Stores rectangles for each image
vector <int> bbValues; //Bounding box values (xmin,ymin,xmax,ymax)

fs::path anotationsFolder = "THE FOLDER PATH OF ANOTATIONS"; //Path of anotations folder
fs::path anotationsParsedFolder = "THE FOLDER PATH TO STORE PARSED ANOTATIONS"; //Path to store new anotations

fs::recursive_directory_iterator it(anotationsFolder); //Iteradores of files
fs::recursive_directory_iterator endit;

cout<<"Loading anotations from "<<anotationsFolder<<endl;

while((it != endit)) //Until end of folder
{
    if((fs::is_regular_file(*it))) //Good practice
    {
        fs::path imagePath(it->path()); //Complete path of the image

        cout<<"Reading anotations from"<<it->path().filename()<<endl;

        ifstream inputFile; //Declare input file with image path
        inputFile.open(imagePath.string().data(), std::ios_base::in);

        i=0;
        while (! inputFile.eof() ){ //Until end of file

            getline (inputFile,line);//Get lines one by one

            if ((i>=17) && ((i-17)%7==0)){ //In lines numer 17,24,31,38...where bounding boxes coordinates are

                j=69;
                v=line[j]; //Start from character num 69 corresponding to first value of Xmin

                while (j<line.size()){ //Until end of line

                    if (v=='(' || v==',' || v==')' || v==' ' || v=='-'){ //if true, push back acumulated value unless previous value wasn't a symbol also
                        if (n==0){
                            bbValues.push_back(stoi(value)); //stoi converts string in to integer ("567"->567) 
                            value.clear();
                        }
                        n++;
                    }
                    else{
                        value+=v; //Append new number
                        n=0;//Reset in order to know that a number has been read
                    }
                    j++;
                    v=line[j];//Read next character
                }
                Rect rect(bbValues[0],bbValues[1],bbValues[2]-bbValues[0],bbValues[3]-bbValues[1]); //Build a rectangle rect(xmin,ymin,xmax-xmin,ymax-ymin)
                anotations.push_back(rect);
                bbValues.clear();
            }
            i++;//Next line
        }
        inputFile.close();            

        cout<<"Writing..."<<endl;

        //Save the anotations to a file
        ofstream outputFile; //Declare file
        fs::path outputPath(anotationsParsedFolder / it->path().filename());// Complete path of the file
        outputFile.open(outputPath.string().data(), ios_base::trunc);

        // Store anotations as x y width heigth
        for (int i=0; i<anotations.size(); i++){
            outputFile<<anotations[i].x<<" ";
            outputFile<<anotations[i].y<<" ";
            outputFile<<anotations[i].width<<" ";
            outputFile<<anotations[i].height<<endl;
        }
        anotations.clear();
        outputFile.close();
    }
    ++it;//Next file in anotations folder
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM