Skip to content

Commit a613182

Browse files
committed
set classifer to singleton model
1 parent a2d8853 commit a613182

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

include/Classification.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,33 @@ typedef std::pair<string, float> Prediction;
2121
class Classifier
2222
{
2323
public:
24-
Classifier(const string &model_file,
24+
/***设置成单例模式,避免每次都需要重新加载模型**/
25+
static Classifier* getInstance(const string &model_file,
26+
const string &trained_file,
27+
const string &mean_file,
28+
const string &label_file)
29+
{
30+
if(m_pInstance == NULL)
31+
{
32+
m_pInstance = new Classifier(model_file,
33+
trained_file,
34+
mean_file,
35+
label_file);
36+
return m_pInstance;
37+
}
38+
}
39+
40+
Classifier (const string &model_file,
2541
const string &trained_file,
2642
const string &mean_file,
2743
const string &label_file);
2844

2945
std::vector<Prediction> Classify(const cv::Mat &img, int N = 5);
3046

3147
private:
48+
49+
static Classifier* m_pInstance;
50+
3251
void SetMean(const string &mean_file);
3352

3453
std::vector<float> Predict(const cv::Mat &img);

sources/Classification.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include<algorithm>
33
#include <vector>
44

5+
Classifier* Classifier::m_pInstance = nullptr;
56

67
Classifier::Classifier(const string& model_file,
78
const string& trained_file,

0 commit comments

Comments
 (0)