Move getFiles() to class Utils.

1.3
Micooz 10 years ago
parent 4ae3b06b17
commit 1e99b0307a

@ -17,7 +17,7 @@
#if defined (WIN32) || defined (_WIN32) #if defined (WIN32) || defined (_WIN32)
#include <io.h> #include <io.h>
#elif defined (linux) || defined (__linux__) #elif defined (linux) || defined (__linux__)
#include <sys/io.h>
#endif #endif
#include <iostream> #include <iostream>

@ -1,7 +1,9 @@
#ifndef __UTIL_H__ #ifndef __UTIL_H__
#define __UTIL_H__ #define __UTIL_H__
#include <vector>
#include <string>
#ifdef min #ifdef min
#undef min #undef min
#endif #endif
@ -16,12 +18,14 @@ namespace easypr {
* Get file name from a given path * Get file name from a given path
* bool postfix: including the postfix * bool postfix: including the postfix
*/ */
static std::string getFileName(const std::string &path, const bool postfix = false); static std::string getFileName(const std::string& path,
const bool postfix = false);
/* /*
* Split the given string into segements by a delimiter * Split the given string into segements by a delimiter
*/ */
static std::vector<std::string> splitString(const std::string &str, const char delimiter); static std::vector<std::string> splitString(const std::string& str,
const char delimiter);
/* /*
* returns the smaller of the two numbers * returns the smaller of the two numbers
@ -31,13 +35,16 @@ namespace easypr {
return (v1 < v2) ? v1 : v2; return (v1 < v2) ? v1 : v2;
} }
/*
* Get files from a given folder
* all: including all sub-folders
*/
static std::vector<std::string> getFiles(const std::string& folder,
const bool all = true);
}; };
} }
//C++的获取文件夹函数
void getFiles(std::string path, std::vector<std::string>& files);
//! levenshtein距离用于计算两个车牌的距离 //! levenshtein距离用于计算两个车牌的距离
//EasyPR中用levenshtein距离衡量车牌识别与真实车牌的误差 //EasyPR中用levenshtein距离衡量车牌识别与真实车牌的误差
template<class T> template<class T>

@ -7,14 +7,12 @@
using namespace easypr; using namespace easypr;
int acurayTest(const string& test_path) int acurayTest(const string& test_path) {
{
////获取该路径下的所有文件 ////获取该路径下的所有文件
vector<string> files; auto files = Utils::getFiles(test_path);
getFiles(test_path, files);
CPlateLocate lo; // CPlateLocate lo;
CPlateJudge ju; // CPlateJudge ju;
CPlateRecognize pr; CPlateRecognize pr;
pr.LoadANN("model/ann.xml"); pr.LoadANN("model/ann.xml");
@ -32,8 +30,7 @@ int acurayTest(const string& test_path)
int size = files.size(); int size = files.size();
// int size = 200; // int size = 200;
if (0 == size) if (0 == size) {
{
cout << "No File Found in general_test/native_test!" << endl; cout << "No File Found in general_test/native_test!" << endl;
return 0; return 0;
} }
@ -60,8 +57,7 @@ int acurayTest(const string& test_path)
time_t begin, end; time_t begin, end;
time(&begin); time(&begin);
for (int i = 0; i < size; i++) for (int i = 0; i < size; i++) {
{
string filepath = files[i].c_str(); string filepath = files[i].c_str();
cout << "------------------" << endl; cout << "------------------" << endl;
@ -74,22 +70,16 @@ int acurayTest(const string& test_path)
vector<string> plateVec; vector<string> plateVec;
int result = pr.plateRecognize(src, plateVec); int result = pr.plateRecognize(src, plateVec);
if (result == 0) if (result == 0) {
{
int num = plateVec.size(); int num = plateVec.size();
if (num == 0) if (num == 0) {
{ cout << "ÎÞ³µÅÆ" << endl;
cout << ""<< "ÎÞ³µÅÆ" <<endl; if (plateLicense != "ÎÞ³µÅÆ") count_norecogin++;
if (plateLicense != "ÎÞ³µÅÆ") } else if (num > 1) {
count_norecogin++;
}
else if ( num > 1)
{
// 多车牌使用diff最小的那个记录 // 多车牌使用diff最小的那个记录
int mindiff = 10000; int mindiff = 10000;
for (int j = 0; j < num; j++) for (int j = 0; j < num; j++) {
{
cout << plateVec[j] << " (" << j + 1 << ")" << endl; cout << plateVec[j] << " (" << j + 1 << ")" << endl;
string colorplate = plateVec[j]; string colorplate = plateVec[j];
@ -97,27 +87,22 @@ int acurayTest(const string& test_path)
vector<string> spilt_plate = Utils::splitString(colorplate, ':'); vector<string> spilt_plate = Utils::splitString(colorplate, ':');
int size = spilt_plate.size(); int size = spilt_plate.size();
if (size == 2 && spilt_plate[1] != "") if (size == 2 && spilt_plate[1] != "") {
{ int diff =
int diff = levenshtein_distance(plateLicense, spilt_plate[size-1]); levenshtein_distance(plateLicense, spilt_plate[size - 1]);
if (diff < mindiff) if (diff < mindiff) mindiff = diff;
mindiff = diff;
} }
} }
cout << "差距:" << mindiff << "个字符" << endl; cout << "差距:" << mindiff << "个字符" << endl;
if(mindiff == 0) if (mindiff == 0) {
{
// 完全匹配 // 完全匹配
match_count++; match_count++;
} }
diff_all = diff_all + mindiff; diff_all = diff_all + mindiff;
} } else {
else
{
// 单车牌只计算一次diff // 单车牌只计算一次diff
for (int j = 0; j < num; j++) for (int j = 0; j < num; j++) {
{
cout << plateVec[j] << endl; cout << plateVec[j] << endl;
string colorplate = plateVec[j]; string colorplate = plateVec[j];
@ -125,29 +110,24 @@ int acurayTest(const string& test_path)
vector<string> spilt_plate = Utils::splitString(colorplate, ':'); vector<string> spilt_plate = Utils::splitString(colorplate, ':');
int size = spilt_plate.size(); int size = spilt_plate.size();
if (size == 2 && spilt_plate[1] != "") if (size == 2 && spilt_plate[1] != "") {
{ int diff =
int diff = levenshtein_distance(plateLicense, spilt_plate[size-1]); levenshtein_distance(plateLicense, spilt_plate[size - 1]);
cout << "差距:" << diff << "个字符" << endl; cout << "差距:" << diff << "个字符" << endl;
if(diff == 0) if (diff == 0) {
{
// 完全匹配 // 完全匹配
match_count++; match_count++;
} }
diff_all = diff_all + diff; diff_all = diff_all + diff;
} }
} }
} }
} } else {
else
{
cout << "错误码:" << result << endl; cout << "错误码:" << result << endl;
count_err++; count_err++;
} }
count_all++; count_all++;
} }
time(&end); time(&end);
@ -161,7 +141,7 @@ int acurayTest(const string& test_path)
float count_recogin = count_all - (count_err + count_norecogin); float count_recogin = count_all - (count_err + count_norecogin);
float count_rate = count_recogin / count_all; float count_rate = count_recogin / count_all;
float count_norate = 1 - count_rate; //float count_norate = 1 - count_rate;
cout << "定位率:" << count_rate * 100 << "% " << endl; cout << "定位率:" << count_rate * 100 << "% " << endl;
diff_avg = diff_all / count_recogin; diff_avg = diff_all / count_recogin;
@ -171,7 +151,6 @@ int acurayTest(const string& test_path)
cout << "完全匹配数:" << match_count << "张, "; cout << "完全匹配数:" << match_count << "张, ";
cout << "完全匹配率:" << match_rate << "% " << endl; cout << "完全匹配率:" << match_rate << "% " << endl;
double seconds = difftime(end, begin); double seconds = difftime(end, begin);
double avgsec = seconds / double(count_all); double avgsec = seconds / double(count_all);
@ -183,8 +162,7 @@ int acurayTest(const string& test_path)
cout << "------------------" << endl; cout << "------------------" << endl;
ofstream myfile("run_accuracy.txt", ios::app); ofstream myfile("run_accuracy.txt", ios::app);
if (myfile.is_open()) if (myfile.is_open()) {
{
time_t t = time(0); // get time now time_t t = time(0); // get time now
struct tm* now = localtime(&t); struct tm* now = localtime(&t);
char buf[80]; char buf[80];
@ -201,9 +179,8 @@ int acurayTest(const string& test_path)
myfile << "总时间:" << seconds << "秒, "; myfile << "总时间:" << seconds << "秒, ";
myfile << "平均执行时间:" << avgsec << "" << endl; myfile << "平均执行时间:" << avgsec << "" << endl;
myfile.close(); myfile.close();
} } else {
else
cout << "Unable to open file"; cout << "Unable to open file";
}
return 0; return 0;
} }

@ -16,7 +16,7 @@
#if defined (WIN32) || defined (_WIN32) #if defined (WIN32) || defined (_WIN32)
#include <io.h> #include <io.h>
#elif defined (linux) || defined (__linux__) #elif defined (linux) || defined (__linux__)
#include <sys/io.h> //#include <sys/io.h>
#endif #endif
#include <stdlib.h> #include <stdlib.h>
@ -140,8 +140,7 @@ int saveTrainData()
stringstream ss(stringstream::in | stringstream::out); stringstream ss(stringstream::in | stringstream::out);
ss << path << "/" << strCharacters[i]; ss << path << "/" << strCharacters[i];
vector<string> files; auto files = Utils::getFiles(ss.str());
getFiles(ss.str(), files);
int size = files.size(); int size = files.size();
for (int j = 0; j < size; j++) for (int j = 0; j < size; j++)
@ -169,8 +168,7 @@ int saveTrainData()
stringstream ss(stringstream::in | stringstream::out); stringstream ss(stringstream::in | stringstream::out);
ss << path << "/" << strChinese[i]; ss << path << "/" << strChinese[i];
vector<string> files; auto files = Utils::getFiles(ss.str());
getFiles(ss.str(), files);
int size = files.size(); int size = files.size();
for (int j = 0; j < size; j++) for (int j = 0; j < size; j++)

@ -13,10 +13,9 @@ void learn2HasPlate(float bound = 0.7)
{ {
const char * filePath = "train/data/plate_detect_svm/learn/HasPlate"; const char * filePath = "train/data/plate_detect_svm/learn/HasPlate";
vector<string> files;
////获取该路径下的所有文件 ////获取该路径下的所有文件
getFiles(filePath, files ); auto files = Utils::getFiles(filePath);
int size = files.size(); int size = files.size();
if (0 == size) { if (0 == size) {
@ -60,10 +59,9 @@ void learn2NoPlate(float bound = 0.7)
{ {
const char * filePath = "train/data/plate_detect_svm/learn/NoPlate"; const char * filePath = "train/data/plate_detect_svm/learn/NoPlate";
vector<string> files;
////获取该路径下的所有文件 ////获取该路径下的所有文件
getFiles(filePath, files ); auto files = Utils::getFiles(filePath);
int size = files.size(); int size = files.size();
if (0 == size) { if (0 == size) {
cout << "File not found in " << filePath << endl; cout << "File not found in " << filePath << endl;
@ -107,10 +105,9 @@ void getHasPlateTrain(Mat& trainingImages, vector<int>& trainingLabels,
{ {
int label = 1; int label = 1;
const char * filePath = "train/data/plate_detect_svm/train/HasPlate"; const char * filePath = "train/data/plate_detect_svm/train/HasPlate";
vector<string> files;
////获取该路径下的所有文件 ////获取该路径下的所有文件
getFiles(filePath, files ); auto files = Utils::getFiles(filePath);
int size = files.size(); int size = files.size();
if (0 == size) { if (0 == size) {
@ -139,10 +136,9 @@ void getNoPlateTrain(Mat& trainingImages, vector<int>& trainingLabels,
{ {
int label = 0; int label = 0;
const char * filePath = "train/data/plate_detect_svm/train/NoPlate"; const char * filePath = "train/data/plate_detect_svm/train/NoPlate";
vector<string> files;
////获取该路径下的所有文件 ////获取该路径下的所有文件
getFiles(filePath, files ); auto files = Utils::getFiles(filePath);
int size = files.size(); int size = files.size();
if (0 == size) { if (0 == size) {
@ -169,10 +165,9 @@ void getHasPlateTest(vector<Mat>& testingImages, vector<int>& testingLabels)
{ {
int label = 1; int label = 1;
const char * filePath = "train/data/plate_detect_svm/test/HasPlate"; const char * filePath = "train/data/plate_detect_svm/test/HasPlate";
vector<string> files;
////获取该路径下的所有文件 ////获取该路径下的所有文件
getFiles(filePath, files ); auto files = Utils::getFiles(filePath);
int size = files.size(); int size = files.size();
if (0 == size) { if (0 == size) {
@ -194,10 +189,9 @@ void getNoPlateTest(vector<Mat>& testingImages, vector<int>& testingLabels)
{ {
int label = 0; int label = 0;
const char * filePath = "train/data/plate_detect_svm/test/NoPlate"; const char * filePath = "train/data/plate_detect_svm/test/NoPlate";
vector<string> files;
////获取该路径下的所有文件 ////获取该路径下的所有文件
getFiles(filePath, files ); auto files = Utils::getFiles(filePath);
int size = files.size(); int size = files.size();
if (0 == size) { if (0 == size) {

@ -19,8 +19,7 @@ const string dst_path = "F:/data/easypr-data/tmp-6";
int general_test() int general_test()
{ {
////获取该路径下的所有文件 ////获取该路径下的所有文件
vector<string> files; auto files = Utils::getFiles(src_path);
getFiles(src_path, files);
CPlateLocate lo; CPlateLocate lo;
CPlateJudge ju; CPlateJudge ju;

@ -31,8 +31,7 @@ int generate_gdts()
string cascadeName="model/haarcascade_frontalface_alt_tree.xml"; string cascadeName="model/haarcascade_frontalface_alt_tree.xml";
////获取该路径下的所有文件 ////获取该路径下的所有文件
vector<string> files; auto files = Utils::getFiles(src_path);
getFiles(src_path, files);
int size = files.size(); int size = files.size();
if (0 == size) if (0 == size)

@ -24,8 +24,7 @@ char * noPlatePath = "F:/data/easypr-data/learn/noPlate/";
void label_data() void label_data()
{ {
////获取该路径下的所有文件 ////获取该路径下的所有文件
vector<string> files; auto files = Utils::getFiles(notlabelPath);
getFiles(notlabelPath, files);
CPlateLocate lo; CPlateLocate lo;
CPlateJudge ju; CPlateJudge ju;

@ -9,7 +9,7 @@
#if defined (WIN32) || defined (_WIN32) #if defined (WIN32) || defined (_WIN32)
#include <io.h> #include <io.h>
#elif defined (linux) || defined (__linux__) #elif defined (linux) || defined (__linux__)
#include <sys/io.h> //#include <sys/io.h>
#endif #endif
#include <opencv/cv.h> #include <opencv/cv.h>
@ -24,8 +24,6 @@ using namespace easypr;
const int LEARANDATA_COUNT = 5000; const int LEARANDATA_COUNT = 5000;
void getFiles( string path, vector<string>& files );
//! 省份对应map //! 省份对应map
map<string, string> mc_map; map<string, string> mc_map;
@ -195,8 +193,7 @@ void getLearnData()
const char * filePath = "F:/data/easypr-data/rawdata"; const char * filePath = "F:/data/easypr-data/rawdata";
////获取该路径下的所有文件 ////获取该路径下的所有文件
vector<string> files; auto files = Utils::getFiles(filePath);
getFiles(filePath, files );
int size = files.size(); int size = files.size();
if (0 == size) { if (0 == size) {
@ -248,8 +245,7 @@ void changeFileName()
char * filePath = "F:/data/PlateLocate/pic1"; char * filePath = "F:/data/PlateLocate/pic1";
////获取该路径下的所有文件 ////获取该路径下的所有文件
vector<string> files; auto files = Utils::getFiles(filePath);
getFiles(filePath, files );
int size = files.size(); int size = files.size();
if (0 == size) if (0 == size)

@ -1,16 +1,13 @@
#if defined (WIN32) || defined (_WIN32) #include "../include/util.h"
#include <windows.h> #include <list>
#endif
#include <iostream>
#include <cstdlib>
#if defined(WIN32) || defined(_WIN32) #if defined(WIN32) || defined(_WIN32)
#include <windows.h>
#include <io.h> #include <io.h>
#elif defined(linux) || defined(__linux__) #elif defined(linux) || defined(__linux__)
#include <sys/io.h> //#include <sys/io.h>
#include <sys/stat.h> //#include <sys/stat.h>
#include <sys/types.h> //#include <sys/types.h>
#include <dirent.h> #include <dirent.h>
#include <time.h> #include <time.h>
#include <cstring> #include <cstring>
@ -21,18 +18,10 @@
#include <sys/timeb.h> #include <sys/timeb.h>
#endif #endif
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include "../include/util.h"
using namespace std; using namespace std;
using namespace easypr; using namespace easypr;
long long Utils::getTimestamp() {
Utils::getTimestamp()
{
#if defined(WIN32) || defined(_WIN32) #if defined(WIN32) || defined(_WIN32)
return GetTickCount(); return GetTickCount();
#endif #endif
@ -55,9 +44,8 @@ Utils::getTimestamp()
#endif #endif
} }
std::string std::string Utils::getFileName(const string &path,
Utils::getFileName(const string &path, const bool postfix /* = false */) const bool postfix /* = false */) {
{
if (!path.empty()) { if (!path.empty()) {
#if defined(WIN32) || defined(_WIN32) #if defined(WIN32) || defined(_WIN32)
size_t last_slash = path.find_last_of('\\'); size_t last_slash = path.find_last_of('\\');
@ -83,9 +71,7 @@ Utils::getFileName(const string &path, const bool postfix /* = false */)
return ""; return "";
} }
vector<string> vector<string> Utils::splitString(const string &str, const char delimiter) {
Utils::splitString(const string &str, const char delimiter)
{
vector<string> splited; vector<string> splited;
string s(str); string s(str);
size_t pos; size_t pos;
@ -105,64 +91,91 @@ Utils::splitString(const string &str, const char delimiter)
return splited; return splited;
} }
vector<string> Utils::getFiles(const string &folder,
const bool all /* = true */) {
vector<string> files;
#if defined(WIN32) || defined(_WIN32) #if defined(WIN32) || defined(_WIN32)
void getFiles(string path, vector<string>& files)
{
//文件句柄 //文件句柄
long hFile = 0; long hFile = 0;
//文件信息 //文件信息
struct _finddata_t fileinfo; struct _finddata_t fileinfo;
string p; string p;
if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) != -1) if ((hFile = _findfirst(p.assign(path).append("\\").c_str(), &fileinfo)) !=
{ -1) {
do do {
{
//如果是目录,迭代之 //如果是目录,迭代之
//如果不是,加入列表 //如果不是,加入列表
if((fileinfo.attrib & _A_SUBDIR)) if ((fileinfo.attrib & _A_SUBDIR)) {
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
getFiles(p.assign(path).append("\\").append(fileinfo.name), files); getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
} } else {
else
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name)); files.push_back(p.assign(path).append("\\").append(fileinfo.name));
} }
} while (_findnext(hFile, &fileinfo) == 0); } while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile); _findclose(hFile);
} }
#elif defined(linux) || defined(__linux__) || defined(__APPLE__)
list<string> subfolders;
subfolders.push_back(folder);
while (!subfolders.empty()) {
string current_folder(subfolders.back());
if (*(current_folder.end() - 1) != '/') {
current_folder.push_back('/');
} }
#elif defined (linux) || defined (__linux__) || defined (__APPLE__) DIR *pdir = opendir(current_folder.c_str());
subfolders.pop_back();
if (!pdir) {
continue;
}
dirent *dir = NULL;
void getFiles(string path, vector<string>& files) { while ((dir = readdir(pdir)) != NULL) {
DIR *dirp = opendir(path.c_str()); // iterates the current folder, search file & sub folder
if (dirp) {
struct stat st; struct stat st;
struct dirent *dir;
char fullpath[512]; if (all && (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))) {
while ((dir = readdir(dirp)) != NULL) { // must ignore . & ..
if (!strcmp(dir->d_name, ".") || continue;
!strcmp(dir->d_name, "..")) { }
if (!strcmp(dir->d_name, ".DS_Store")) {
// in OSX, 'finder' will create .DS_Store
continue; continue;
} }
sprintf(fullpath, "%s/%s", path.c_str(), dir->d_name); string file_path;
if (lstat(fullpath, &st) < 0) { file_path.append(current_folder.c_str());
file_path.append(dir->d_name);
if (lstat(file_path.c_str(), &st) < 0) {
// perror("lstat"); // perror("lstat");
continue; continue;
} }
if (S_ISDIR(st.st_mode)) { if (S_ISDIR(st.st_mode)) {
getFiles(fullpath, files); // it's a sub folder
} else { if (all) {
files.push_back(fullpath); // will search sub folder
} string folder(current_folder);
folder.append(dir->d_name);
subfolders.push_back(folder.c_str());
} }
} else {
// it's a file
files.push_back(file_path);
} }
closedir(dirp); } // while
closedir(pdir);
} }
#endif #endif
return files;
}

Loading…
Cancel
Save