Inprove: more effecient SplitString.

1.3
Micooz 10 years ago
parent eb1f565074
commit b856dbc65d

@ -7,6 +7,18 @@ namespace easypr {
class Utils {
public:
static long getTimestamp();
/*
* Get file name from a given path
* bool postfix: including the postfix
*/
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);
};
}
@ -14,17 +26,6 @@ namespace easypr {
//C++的获取文件夹函数
void getFiles(std::string path, std::vector<std::string>& files);
//Linux下的GetTickCount函数
#if defined (linux) || defined (__linux__)
double GetTickCount();
#endif
//C++的spilt函数
void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c);
//C++的从文件路径名称到文件名称(不包括后缀)的方法
void getFileName(const std::string& filepath, std::string& name);
//! levenshtein距离用于计算两个车牌的距离
//EasyPR中用levenshtein距离衡量车牌识别与真实车牌的误差
template<class T>

@ -54,8 +54,7 @@ int acurayTest(const string& test_path)
cout << "------------------" << endl;
// 获取真实的车牌
string plateLicense = "";
getFileName(filepath, plateLicense);
string plateLicense = Utils::getFileName(filepath);
cout << "原牌:" << plateLicense << endl;
@ -83,9 +82,8 @@ int acurayTest(const string& test_path)
string colorplate = plateVec[j];
// 计算"蓝牌:苏E7KU22"中冒号后面的车牌大小"
vector<string> spilt_plate;
SplitString(colorplate, spilt_plate, ":");
vector<string> spilt_plate = Utils::splitString(colorplate, ':');
int size = spilt_plate.size();
if (size == 2)
{
@ -112,9 +110,8 @@ int acurayTest(const string& test_path)
string colorplate = plateVec[j];
// 计算"蓝牌:苏E7KU22"中冒号后面的车牌大小"
vector<string> spilt_plate;
SplitString(colorplate, spilt_plate, ":");
vector<string> spilt_plate = Utils::splitString(colorplate, ':');
int size = spilt_plate.size();
if (size == 2)
{

@ -65,8 +65,7 @@ int general_test()
string colorplate = plateVec[0];
// 输出"蓝牌:苏E7KU22"中冒号后面的车牌
vector<string> spilt_plate;
SplitString(colorplate, spilt_plate, ":");
vector<string> spilt_plate = Utils::splitString(colorplate, ':');
int size = spilt_plate.size();
if (size == 2)

@ -59,8 +59,8 @@ int generate_gdts()
Mat dst = detectAndMaskFace(img, cascade, 1.5);
// 将图片导出到新路径
vector<string> spilt_path;
SplitString(filepath, spilt_path, "\\");
vector<string> spilt_path = Utils::splitString(filepath, '\\');
int spiltsize = spilt_path.size();
string filename = "";

@ -16,14 +16,15 @@
#include <opencv/highgui.h>
#include <opencv/cvaux.h>
#include <algorithm>
#include "../include/util.h"
using namespace std;
using namespace cv;
using namespace easypr;
const int LEARANDATA_COUNT = 5000;
void getFiles( string path, vector<string>& files );
void SplitString(const string& s, vector<string>& v, const string& c);
//! 省份对应map
map<string, string> mc_map;
@ -96,9 +97,8 @@ Mat cutBottom(Mat img)
//MCfilepath示例F:\data\easypr-data\learndata\20150110132005-210028-S18-H3952K.jpg
bool isNotNight(const string& filepath)
{
vector<string> spilt_path;
SplitString(filepath, spilt_path, "\\");
vector<string> spilt_path = Utils::splitString(filepath, '\\');
int spiltsize = spilt_path.size();
string filename = "";
if (spiltsize != 0)
@ -106,9 +106,8 @@ bool isNotNight(const string& filepath)
filename = spilt_path[spiltsize-1];
if (filename != "")
{
vector<string> spilt_name;
SplitString(filename, spilt_name, "-");
vector<string> spilt_name = Utils::splitString(filename, '-');
int name_size = spilt_name.size();
string datestr = "";
if (name_size != 0)
@ -133,8 +132,7 @@ bool isNotNight(const string& filepath)
bool getNewPath(const string& filepath, string& newfilepath)
{
string writePath = "F:/data/easypr-data/learndata_dl/";
vector<string> spilt_path;
SplitString(filepath, spilt_path, "\\");
vector<string> spilt_path = Utils::splitString(filepath, '\\');
int spiltsize = spilt_path.size();
string filename = "";
@ -151,9 +149,8 @@ bool getNewPath(const string& filepath, string& newfilepath)
//! MC通过filepath获取车牌号码
void getPlateLicense(const string& filepath, string& plateLicense)
{
vector<string> spilt_path;
SplitString(filepath, spilt_path, "\\");
vector<string> spilt_path = Utils::splitString(filepath, '\\');
int spiltsize = spilt_path.size();
string filename = "";
if (spiltsize != 0)
@ -161,9 +158,8 @@ void getPlateLicense(const string& filepath, string& plateLicense)
filename = spilt_path[spiltsize-1];
if (filename != "")
{
vector<string> spilt_name;
SplitString(filename, spilt_name, "-");
vector<string> spilt_name = Utils::splitString(filename, '-');
int name_size = spilt_name.size();
string plateStr = "";
string provinceCode = "";
@ -174,8 +170,8 @@ void getPlateLicense(const string& filepath, string& plateLicense)
plateStr = spilt_name[name_size-1];
// 将".jpg"去掉
vector<string> spilt_plate;
SplitString(plateStr, spilt_plate, ".");
vector<string> spilt_plate = Utils::splitString(plateStr, '.');
int plate_size = spilt_plate.size();
string rawplate = "";
if (plate_size != 0)

@ -55,6 +55,56 @@ Utils::getTimestamp()
#endif
}
std::string
Utils::getFileName(const string &path, const bool postfix /* = false */)
{
if (!path.empty()) {
#if defined (WIN32) || defined (_WIN32)
size_t last_slash = path.find_last_of('\\');
#else
size_t last_slash = path.find_last_of('/');
#endif
size_t last_dot = path.find_last_of('.');
if (last_dot < last_slash || last_dot == string::npos) {
// not found the right dot of the postfix,
// return the file name directly
return path.substr(last_slash + 1);
} else {
// the path has a postfix
if (postfix) {
// return the file name including postfix
return path.substr(last_slash + 1);
}
// without postfix
return path.substr(last_slash + 1, last_dot - last_slash - 1);
}
}
return "";
}
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 ) {
string sec = s.substr(0, pos);
if (!sec.empty()) {
splited.push_back(s.substr(0, pos));
}
s = s.substr(pos + 1);
}
splited.push_back(s);
return splited;
}
#if defined (WIN32) || defined (_WIN32)
void getFiles(string path, vector<string>& files)
@ -116,45 +166,3 @@ void getFiles(string path, vector<string>& files) {
}
#endif
//C++的spilt函数
void SplitString(const string& s, vector<string>& v, const string& c)
{
std::string::size_type pos1, pos2;
pos2 = s.find(c);
pos1 = 0;
while(std::string::npos != pos2)
{
v.push_back(s.substr(pos1, pos2-pos1));
pos1 = pos2 + c.size();
pos2 = s.find(c, pos1);
}
if(pos1 != s.length())
v.push_back(s.substr(pos1));
}
//! 通过文件夹名称获取文件名,不包括后缀
void getFileName(const string& filepath, string& name)
{
vector<string> spilt_path;
SplitString(filepath, spilt_path, "\\");
int spiltsize = spilt_path.size();
string filename = "";
if (spiltsize != 0)
{
filename = spilt_path[spiltsize-1];
vector<string> spilt_name;
SplitString(filename, spilt_name, ".");
int name_size = spilt_name.size();
if (name_size != 0)
{
name = spilt_name[0];
}
}
}

Loading…
Cancel
Save