Move getFiles() to class Utils.

1.3
Micooz 10 years ago
parent 4ae3b06b17
commit 1e99b0307a

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

@ -1,14 +1,16 @@
#ifndef __UTIL_H__
#define __UTIL_H__
#include <vector>
#include <string>
#ifdef min
#undef min
#undef min
#endif
namespace easypr {
class Utils {
class Utils {
public:
static long getTimestamp();
@ -16,28 +18,33 @@ namespace easypr {
* Get file name from a given path
* 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
*/
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
*/
template<typename T>
static T min(const T &v1, const T &v2) {
template <typename T>
static T min(const T& v1, const T& 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距离用于计算两个车牌的距离
//EasyPR中用levenshtein距离衡量车牌识别与真实车牌的误差
template<class T>

@ -7,14 +7,12 @@
using namespace easypr;
int acurayTest(const string& test_path)
{
int acurayTest(const string& test_path) {
////获取该路径下的所有文件
vector<string> files;
getFiles(test_path, files);
auto files = Utils::getFiles(test_path);
CPlateLocate lo;
CPlateJudge ju;
// CPlateLocate lo;
// CPlateJudge ju;
CPlateRecognize pr;
pr.LoadANN("model/ann.xml");
@ -25,15 +23,14 @@ int acurayTest(const string& test_path)
// 设置要处理的一张图片中最多有多少车牌
pr.setMaxPlates(4);
//CPlateDetect pd;
//pd.LoadSVM("model/svm.xml");
//pd.setPDLifemode(true);
// CPlateDetect pd;
// pd.LoadSVM("model/svm.xml");
// pd.setPDLifemode(true);
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;
return 0;
}
@ -60,8 +57,7 @@ int acurayTest(const string& test_path)
time_t begin, end;
time(&begin);
for (int i = 0; i < size; i++)
{
for (int i = 0; i < size; i++) {
string filepath = files[i].c_str();
cout << "------------------" << endl;
@ -74,80 +70,64 @@ int acurayTest(const string& test_path)
vector<string> plateVec;
int result = pr.plateRecognize(src, plateVec);
if (result == 0)
{
if (result == 0) {
int num = plateVec.size();
if (num == 0)
{
cout << ""<< "ÎÞ³µÅÆ" <<endl;
if (plateLicense != "ÎÞ³µÅÆ")
count_norecogin++;
}
else if ( num > 1)
{
if (num == 0) {
cout << "ÎÞ³µÅÆ" << endl;
if (plateLicense != "ÎÞ³µÅÆ") count_norecogin++;
} else if (num > 1) {
// 多车牌使用diff最小的那个记录
int mindiff = 10000;
for (int j = 0; j < num; j++)
{
cout << plateVec[j] << " (" << j+1 << ")"<<endl;
for (int j = 0; j < num; j++) {
cout << plateVec[j] << " (" << j + 1 << ")" << endl;
string colorplate = plateVec[j];
// 计算"蓝牌:苏E7KU22"中冒号后面的车牌大小"
vector<string> spilt_plate = Utils::splitString(colorplate, ':');
int size = spilt_plate.size();
if (size == 2 && spilt_plate[1] != "")
{
int diff = levenshtein_distance(plateLicense, spilt_plate[size-1]);
if (diff < mindiff)
mindiff = diff;
if (size == 2 && spilt_plate[1] != "") {
int diff =
levenshtein_distance(plateLicense, spilt_plate[size - 1]);
if (diff < mindiff) mindiff = diff;
}
}
cout << "差距:" << mindiff << "个字符" << endl;
if(mindiff == 0)
{
if (mindiff == 0) {
// 完全匹配
match_count++;
}
diff_all = diff_all + mindiff;
}
else
{
} else {
// 单车牌只计算一次diff
for (int j = 0; j < num; j++)
{
cout << plateVec[j] <<endl;
for (int j = 0; j < num; j++) {
cout << plateVec[j] << endl;
string colorplate = plateVec[j];
// 计算"蓝牌:苏E7KU22"中冒号后面的车牌大小"
vector<string> spilt_plate = Utils::splitString(colorplate, ':');
int size = spilt_plate.size();
if (size == 2 && spilt_plate[1] != "")
{
int diff = levenshtein_distance(plateLicense, spilt_plate[size-1]);
if (size == 2 && spilt_plate[1] != "") {
int diff =
levenshtein_distance(plateLicense, spilt_plate[size - 1]);
cout << "差距:" << diff << "个字符" << endl;
if(diff == 0)
{
if (diff == 0) {
// 完全匹配
match_count++;
}
diff_all = diff_all + diff;
}
}
}
}
else
{
} else {
cout << "错误码:" << result << endl;
count_err++;
}
count_all++;
}
time(&end);
@ -161,17 +141,16 @@ int acurayTest(const string& test_path)
float count_recogin = count_all - (count_err + count_norecogin);
float count_rate = count_recogin / count_all;
float count_norate = 1 - count_rate;
//float count_norate = 1 - count_rate;
cout << "定位率:" << count_rate * 100 << "% " << endl;
diff_avg = diff_all / count_recogin;
match_rate = match_count/ count_recogin * 100;
match_rate = match_count / count_recogin * 100;
cout << "平均字符差距:" << diff_avg << "个, ";
cout << "完全匹配数:" << match_count << "张, ";
cout << "完全匹配率:" << match_rate << "% " << endl;
double seconds = difftime(end, begin);
double avgsec = seconds / double(count_all);
@ -183,10 +162,9 @@ int acurayTest(const string& test_path)
cout << "------------------" << endl;
ofstream myfile("run_accuracy.txt", ios::app);
if (myfile.is_open())
{
if (myfile.is_open()) {
time_t t = time(0); // get time now
struct tm * now = localtime(&t);
struct tm* now = localtime(&t);
char buf[80];
strftime(buf, sizeof(buf), "%Y-%m-%d %X", now);
@ -201,9 +179,8 @@ int acurayTest(const string& test_path)
myfile << "总时间:" << seconds << "秒, ";
myfile << "平均执行时间:" << avgsec << "" << endl;
myfile.close();
}
else
} else {
cout << "Unable to open file";
}
return 0;
}

@ -16,7 +16,7 @@
#if defined (WIN32) || defined (_WIN32)
#include <io.h>
#elif defined (linux) || defined (__linux__)
#include <sys/io.h>
//#include <sys/io.h>
#endif
#include <stdlib.h>
@ -140,8 +140,7 @@ int saveTrainData()
stringstream ss(stringstream::in | stringstream::out);
ss << path << "/" << strCharacters[i];
vector<string> files;
getFiles(ss.str(), files);
auto files = Utils::getFiles(ss.str());
int size = files.size();
for (int j = 0; j < size; j++)
@ -169,8 +168,7 @@ int saveTrainData()
stringstream ss(stringstream::in | stringstream::out);
ss << path << "/" << strChinese[i];
vector<string> files;
getFiles(ss.str(), files);
auto files = Utils::getFiles(ss.str());
int size = files.size();
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";
vector<string> files;
////获取该路径下的所有文件
getFiles(filePath, files );
auto files = Utils::getFiles(filePath);
int size = files.size();
if (0 == size) {
@ -60,10 +59,9 @@ void learn2NoPlate(float bound = 0.7)
{
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();
if (0 == size) {
cout << "File not found in " << filePath << endl;
@ -107,10 +105,9 @@ void getHasPlateTrain(Mat& trainingImages, vector<int>& trainingLabels,
{
int label = 1;
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();
if (0 == size) {
@ -139,10 +136,9 @@ void getNoPlateTrain(Mat& trainingImages, vector<int>& trainingLabels,
{
int label = 0;
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();
if (0 == size) {
@ -169,10 +165,9 @@ void getHasPlateTest(vector<Mat>& testingImages, vector<int>& testingLabels)
{
int label = 1;
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();
if (0 == size) {
@ -194,10 +189,9 @@ void getNoPlateTest(vector<Mat>& testingImages, vector<int>& testingLabels)
{
int label = 0;
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();
if (0 == size) {

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

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

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

@ -9,7 +9,7 @@
#if defined (WIN32) || defined (_WIN32)
#include <io.h>
#elif defined (linux) || defined (__linux__)
#include <sys/io.h>
//#include <sys/io.h>
#endif
#include <opencv/cv.h>
@ -24,8 +24,6 @@ using namespace easypr;
const int LEARANDATA_COUNT = 5000;
void getFiles( string path, vector<string>& files );
//! 省份对应map
map<string, string> mc_map;
@ -195,8 +193,7 @@ void getLearnData()
const char * filePath = "F:/data/easypr-data/rawdata";
////获取该路径下的所有文件
vector<string> files;
getFiles(filePath, files );
auto files = Utils::getFiles(filePath);
int size = files.size();
if (0 == size) {
@ -248,8 +245,7 @@ void changeFileName()
char * filePath = "F:/data/PlateLocate/pic1";
////获取该路径下的所有文件
vector<string> files;
getFiles(filePath, files );
auto files = Utils::getFiles(filePath);
int size = files.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 <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 easypr;
long
Utils::getTimestamp()
{
#if defined (WIN32) || defined (_WIN32)
long Utils::getTimestamp() {
#if defined(WIN32) || defined(_WIN32)
return GetTickCount();
#endif
#if (linux) || defined (__linux__)
#if (linux) || defined(__linux__)
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
@ -45,7 +34,7 @@ Utils::getTimestamp()
return (ts.tv_sec * 1e3 + ts.tv_nsec / 1e6);
#endif
#if defined (__APPLE__)
#if defined(__APPLE__)
// there is no function provided by osx to get system tick count.
// but considering the purpose by using this function,
// we can simply return a millisecond since 1970/1/1 to calc the time elapse.
@ -55,11 +44,10 @@ Utils::getTimestamp()
#endif
}
std::string
Utils::getFileName(const string &path, const bool postfix /* = false */)
{
std::string Utils::getFileName(const string &path,
const bool postfix /* = false */) {
if (!path.empty()) {
#if defined (WIN32) || defined (_WIN32)
#if defined(WIN32) || defined(_WIN32)
size_t last_slash = path.find_last_of('\\');
#else
size_t last_slash = path.find_last_of('/');
@ -83,14 +71,12 @@ Utils::getFileName(const string &path, const bool postfix /* = false */)
return "";
}
vector<string>
Utils::splitString(const string &str, const char delimiter)
{
vector<string> Utils::splitString(const string &str, const char delimiter) {
vector<string> splited;
string s(str);
size_t pos;
while ((pos = s.find(delimiter)) != string::npos ) {
while ((pos = s.find(delimiter)) != string::npos) {
string sec = s.substr(0, pos);
if (!sec.empty()) {
@ -105,64 +91,91 @@ Utils::splitString(const string &str, const char delimiter)
return splited;
}
#if defined (WIN32) || defined (_WIN32)
void getFiles(string path, vector<string>& files)
{
vector<string> Utils::getFiles(const string &folder,
const bool all /* = true */) {
vector<string> files;
#if defined(WIN32) || defined(_WIN32)
//文件句柄
long hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string p;
if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) != -1)
{
do
{
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)
getFiles( p.assign(path).append("\\").append(fileinfo.name), files );
}
else
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
if ((fileinfo.attrib & _A_SUBDIR)) {
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
} else {
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
}while(_findnext(hFile, &fileinfo) == 0);
} while (_findnext(hFile, &fileinfo) == 0);
_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) {
DIR *dirp = opendir(path.c_str());
if (dirp) {
if (!pdir) {
continue;
}
dirent *dir = NULL;
while ((dir = readdir(pdir)) != NULL) {
// iterates the current folder, search file & sub folder
struct stat st;
struct dirent *dir;
char fullpath[512];
while ((dir = readdir(dirp)) != NULL) {
if (!strcmp(dir->d_name, ".") ||
!strcmp(dir->d_name, "..")) {
if (all && (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))) {
// must ignore . & ..
continue;
}
if (!strcmp(dir->d_name, ".DS_Store")) {
// in OSX, 'finder' will create .DS_Store
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) {
//perror("lstat");
if (lstat(file_path.c_str(), &st) < 0) {
// perror("lstat");
continue;
}
if (S_ISDIR(st.st_mode)) {
getFiles(fullpath, files);
} else {
files.push_back(fullpath);
// it's a sub folder
if (all) {
// 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
return files;
}

Loading…
Cancel
Save