From c1a485dfe3ecac17b006f834c0aad5eea6d8237e Mon Sep 17 00:00:00 2001 From: Micooz Date: Sun, 15 Feb 2015 13:04:13 +0800 Subject: [PATCH] Add: class Utils Remove: unnecessary vars --- src/core/chars_segment.cpp | 2 +- src/include/util.h | 25 ++++++++++++++++-------- src/train/ann_train.cpp | 6 +++--- src/util/deface.cpp | 4 +--- src/util/util.cpp | 40 +++++++++++++++++++++++++++----------- 5 files changed, 51 insertions(+), 26 deletions(-) diff --git a/src/core/chars_segment.cpp b/src/core/chars_segment.cpp index 2206df4..762d77c 100644 --- a/src/core/chars_segment.cpp +++ b/src/core/chars_segment.cpp @@ -304,7 +304,7 @@ int CCharsSegment::SortRect(const vector& vecRect, vector& out) xpositions.push_back(vecRect[i].x); } - float min=xpositions[0]; + float min; int minIdx=0; for(int i=0; i< xpositions.size(); i++) { diff --git a/src/include/util.h b/src/include/util.h index 6661edb..657a37a 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -2,8 +2,17 @@ #ifndef __UTIL_H__ #define __UTIL_H__ +namespace easypr { + + class Utils { + public: + static long getTimestamp(); + }; + +} + //C++的获取文件夹函数 -void getFiles(string path, vector& files); +void getFiles(std::string path, std::vector& files); //Linux下的GetTickCount函数 #if defined (linux) || defined (__linux__) @@ -11,24 +20,24 @@ double GetTickCount(); #endif //C++的spilt函数 -void SplitString(const string& s, vector& v, const string& c); +void SplitString(const std::string& s, std::vector& v, const std::string& c); //C++的从文件路径名称到文件名称(不包括后缀)的方法 -void getFileName(const string& filepath, string& name); +void getFileName(const std::string& filepath, std::string& name); //! levenshtein距离,用于计算两个车牌的距离 //!EasyPR中用levenshtein距离衡量车牌识别与真实车牌的误差 template unsigned int levenshtein_distance(const T &s1, const T & s2) { const size_t len1 = s1.size(), len2 = s2.size(); - vector col(len2+1), prevCol(len2+1); + std::vector col(len2+1), prevCol(len2+1); for (unsigned int i = 0; i < prevCol.size(); i++) prevCol[i] = i; for (unsigned int i = 0; i < len1; i++) { col[0] = i+1; for (unsigned int j = 0; j < len2; j++) - col[j+1] = min( min(prevCol[1 + j] + 1, col[j] + 1), \ + col[j+1] = std::min(std::min(prevCol[1 + j] + 1, col[j] + 1), \ prevCol[j] + (s1[i]==s2[j] ? 0 : 1) ); col.swap(prevCol); } @@ -40,13 +49,13 @@ unsigned int levenshtein_distance(const T &s1, const T & s2) { int testMain(); /// accuracy_test.cpp中方法 -int acurayTest(const string&); +int acurayTest(const std::string&); /// mc_data_prepare.cpp中方法 void getLearnData(); -void Code2Province(const string& code, string& province); +void Code2Province(const std::string& code, std::string& province); void changeFileName(); -void getPlateLicense(const string& filepath, string& plateLicense); +void getPlateLicense(const std::string& filepath, std::string& plateLicense); /// learn_prepare.cpp中方法 void label_data(); diff --git a/src/train/ann_train.cpp b/src/train/ann_train.cpp index 91a25fd..690f8bc 100644 --- a/src/train/ann_train.cpp +++ b/src/train/ann_train.cpp @@ -232,10 +232,10 @@ void saveModel(int _predictsize, int _neurons) cout << "Begin to saveModelChar predictSize:" << _predictsize << " neurons:" << _neurons << endl; - double start = GetTickCount(); + long start = Utils::getTimestamp(); annTrain(TrainingData, Classes, _neurons); - double end = GetTickCount(); - cout << "GetTickCount:" << (end-start)/1000 << endl; + long end = Utils::getTimestamp(); + cout << "Elapse:" << (end-start)/1000 << endl; cout << "End the saveModelChar" << endl; diff --git a/src/util/deface.cpp b/src/util/deface.cpp index 0007484..6965ef9 100644 --- a/src/util/deface.cpp +++ b/src/util/deface.cpp @@ -74,14 +74,12 @@ int deface() Mat detectAndMaskFace(Mat& img, CascadeClassifier& cascade, double scale) { - - double t = 0; vector faces; Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 ); cvtColor( img, gray, COLOR_BGR2GRAY ); resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR ); equalizeHist( smallImg, smallImg ); - t = (double)cv::getTickCount(); + cascade.detectMultiScale( smallImg, faces, 1.1, 2, 0 //|CASCADE_FIND_BIGGEST_OBJECT diff --git a/src/util/util.cpp b/src/util/util.cpp index eee25c2..b5fc65b 100644 --- a/src/util/util.cpp +++ b/src/util/util.cpp @@ -18,13 +18,42 @@ #include #include #include + #include #endif #include #include #include +#include "../include/util.h" + using namespace std; +using namespace easypr; + +long +Utils::getTimestamp() +{ +#if defined (WIN32) || defined (_WIN32) + return GetTickCount(); +#endif + +#if (linux) || defined (__linux__) + struct timespec ts; + + clock_gettime(CLOCK_MONOTONIC, &ts); + + return (ts.tv_sec * 1e3 + ts.tv_nsec / 1e6); +#endif + +#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. + struct timeb tb; + ftime(&tb); + return tb.time * 1e3 + tb.millitm; +#endif +} #if defined (WIN32) || defined (_WIN32) @@ -88,17 +117,6 @@ void getFiles(string path, vector& files) { #endif -#if (linux) || defined (__linux__) -double GetTickCount() { - struct timespec ts; - - clock_gettime(CLOCK_MONOTONIC, &ts); - - return (ts.tv_sec * 1e3 + ts.tv_nsec / 1e6); -} -#endif - - //C++的spilt函数 void SplitString(const string& s, vector& v, const string& c) {