06le

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

用opencv写人脸识别的时候需要自己准备CSV文件,让程序来读取图像:

代码:

#include<io.h>           
#include<iostream>
#include<stdlib.h>
#include<vector>
#include <fstream>

using namespace std;

void GetFileName(string path, vector<string>& files);
string GetLabel(char* filename);

int main()
{
    const char* path = "E:\\yalefaces";                         
    const char* filename = "E:\\yalefaces\\statistical.txt";
    vector<string> filesName;
    ofstream file_out(filename);                
    GetFileName(path, filesName);                   

    for (size_t i = 0; i < filesName.size(); i++)            
    {
        file_out << filesName[i].c_str() << endl;
    }
    file_out.close();   


    return 0;
}

void GetFileName(string path, vector<string>& filesName)
{

    long    hFile = 0;                    
    struct _finddata_t fileinfo;   
    string aim = path + "\\*";
    
    if ((hFile = _findfirst(aim.c_str(), &fileinfo)) != -1)
    {
        do
        {
            if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)  
            {
                string label = GetLabel(fileinfo.name);
                filesName.push_back(fileinfo.name + label);
            }
        } while (_findnext(hFile, &fileinfo) == 0);      
        _findclose(hFile);              
    }
}

string GetLabel(char* filename)
{
    string s = filename;
    string label(";");
    for (auto i : s)
    {
        if (isdigit(i))
            if (i != '0')
                label += i;
    }
    return label;
}

文件夹:

得到的结果:

posted on 2021-03-04 11:16  06le  阅读(112)  评论(0)    收藏  举报