使用Dlib進行人臉標註時需要遍歷所有的png格式的文件,可使用以下代碼獲取:
int getAllFiles(string path, vector<string>& files)
{
intptr_t hFile = 0;
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
{
do
{
if ((fileinfo.attrib & _A_SUBDIR))
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
{
if (CheckPostFix(fileinfo.name) == true)
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
getAllFiles(p.assign(path).append("\\").append(fileinfo.name), files);
}
}
else
{
if (CheckPostFix(fileinfo.name) == true)
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
}
} while (_findnext(hFile, &fileinfo) == 0);
}
_findclose(hFile);
return 0;
}
bool CheckPostFix(char* pFileName)
{
if (pFileName == nullptr)
{
return false;
}
std::string strFileName = pFileName;
int nStartPos = strFileName.rfind(".");
if (nStartPos == -1)
{
return false;
}
std::string strPostFix = strFileName.substr(strFileName.rfind("."));
if (strPostFix.find(".png") == std::string::npos)
{
return false;
}
return true;
}
包含的頭文件:
#include <iostream>
#include<vector>
#include <io.h>
調用方法:
std::vector<std::string> filePaths;
getAllFiles(ImgDir, filePaths);
這樣所有的png文件的路徑都保存在filePaths裏,通過遍歷這個vector即可對每一個圖片進行處理。
另外通過修改CheckPostFix函數裏的後綴格式即可搜索不同格式的文件。