Move getFiles() to class Utils.

1.3
Micooz 11 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,14 +1,16 @@
#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
namespace easypr { namespace easypr {
class Utils { class Utils {
public: public:
static long getTimestamp(); static long getTimestamp();
@ -16,28 +18,33 @@ 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
*/ */
template<typename T> template <typename T>
static T min(const T &v1, const T &v2) { static T min(const T& v1, const T& v2) {
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");
@ -25,15 +23,14 @@ int acurayTest(const string& test_path)
// 设置要处理的一张图片中最多有多少车牌 // 设置要处理的一张图片中最多有多少车牌
pr.setMaxPlates(4); pr.setMaxPlates(4);
//CPlateDetect pd; // CPlateDetect pd;
//pd.LoadSVM("model/svm.xml"); // pd.LoadSVM("model/svm.xml");
//pd.setPDLifemode(true); // pd.setPDLifemode(true);
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,80 +70,64 @@ 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];
// 计算"蓝牌:苏E7KU22"中冒号后面的车牌大小" // 计算"蓝牌:苏E7KU22"中冒号后面的车牌大小"
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];
// 计算"蓝牌:苏E7KU22"中冒号后面的车牌大小" // 计算"蓝牌:苏E7KU22"中冒号后面的车牌大小"
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,17 +141,16 @@ 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;
match_rate = match_count/ count_recogin * 100; match_rate = match_count / count_recogin * 100;
cout << "平均字符差距:" << diff_avg << "个, "; cout << "平均字符差距:" << diff_avg << "个, ";
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,10 +162,9 @@ 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];
strftime(buf, sizeof(buf), "%Y-%m-%d %X", now); strftime(buf, sizeof(buf), "%Y-%m-%d %X", now);
@ -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,43 +1,32 @@
#if defined (WIN32) || defined (_WIN32)
#include <windows.h>
#endif
#include <iostream>
#include <cstdlib>
#if defined (WIN32) || defined (_WIN32)
#include <io.h>
#elif defined (linux) || defined (__linux__)
#include <sys/io.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <time.h>
#include <cstring>
#elif defined (__APPLE__)
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/timeb.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include "../include/util.h" #include "../include/util.h"
#include <list>
#if defined(WIN32) || defined(_WIN32)
#include <windows.h>
#include <io.h>
#elif defined(linux) || defined(__linux__)
//#include <sys/io.h>
//#include <sys/stat.h>
//#include <sys/types.h>
#include <dirent.h>
#include <time.h>
#include <cstring>
#elif defined(__APPLE__)
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/timeb.h>
#endif
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
#if (linux) || defined (__linux__) #if (linux) || defined(__linux__)
struct timespec ts; struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts); clock_gettime(CLOCK_MONOTONIC, &ts);
@ -45,7 +34,7 @@ Utils::getTimestamp()
return (ts.tv_sec * 1e3 + ts.tv_nsec / 1e6); return (ts.tv_sec * 1e3 + ts.tv_nsec / 1e6);
#endif #endif
#if defined (__APPLE__) #if defined(__APPLE__)
// there is no function provided by osx to get system tick count. // there is no function provided by osx to get system tick count.
// but considering the purpose by using this function, // but considering the purpose by using this function,
// we can simply return a millisecond since 1970/1/1 to calc the time elapse. // we can simply return a millisecond since 1970/1/1 to calc the time elapse.
@ -55,11 +44,10 @@ 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('\\');
#else #else
size_t last_slash = path.find_last_of('/'); size_t last_slash = path.find_last_of('/');
@ -83,14 +71,12 @@ 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;
while ((pos = s.find(delimiter)) != string::npos ) { while ((pos = s.find(delimiter)) != string::npos) {
string sec = s.substr(0, pos); string sec = s.substr(0, pos);
if (!sec.empty()) { if (!sec.empty()) {
@ -105,64 +91,91 @@ Utils::splitString(const string &str, const char delimiter)
return splited; return splited;
} }
#if defined (WIN32) || defined (_WIN32) vector<string> Utils::getFiles(const string &folder,
const bool all /* = true */) {
void getFiles(string path, vector<string>& files) vector<string> files;
{ #if defined(WIN32) || defined(_WIN32)
//文件句柄 //文件句柄
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 {
} files.push_back(p.assign(path).append("\\").append(fileinfo.name));
else
{
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('/');
}
DIR *pdir = opendir(current_folder.c_str());
#elif defined (linux) || defined (__linux__) || defined (__APPLE__) subfolders.pop_back();
void getFiles(string path, vector<string>& files) { if (!pdir) {
DIR *dirp = opendir(path.c_str()); continue;
if (dirp) { }
dirent *dir = NULL;
while ((dir = readdir(pdir)) != NULL) {
// iterates the current folder, search file & sub folder
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;
file_path.append(current_folder.c_str());
file_path.append(dir->d_name);
if (lstat(fullpath, &st) < 0) { 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);
} }
} // while
closedir(pdir);
} }
closedir(dirp);
}
#endif #endif
return files;
}

Loading…
Cancel
Save