forked from zonekey/img_classifier_prepare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
classifier.h
75 lines (60 loc) · 1.83 KB
/
classifier.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#ifndef CLASSIFIER_H
#define CLASSIFIER_H
#include <caffe/caffe.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <algorithm>
#include <iosfwd>
#include <memory>
#include <string>
#include <sstream>
#include <utility>
#include <vector>
#include <fstream>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
using namespace caffe; // NOLINT(build/namespaces)
using std::string;
/* Pair (label, confidence) representing a prediction. */
typedef std::pair < string, float >Prediction;
class Classifier {
public:
explicit Classifier(const string & model_file,
const string & trained_file,
const string & mean_file, const string & label_file);
std::vector < Prediction > Classify(const cv::Mat & img, int N = 5);
int catalog_num() const {
return labels_.size();
}
int catalog_from_label(const std::string & label) const {
for (size_t i = 0; i < labels_.size(); i++) {
if (label == labels_[i]) {
return i;
}
} return -1;
}
cv::Mat title_from_catalog(int c) const
{
return titles_[c];
}
std::string label(int c) const
{
return labels_[c];
}
private:
void SetMean(const string & mean_file);
std::vector < float >Predict(const cv::Mat & img);
void WrapInputLayer(std::vector < cv::Mat > *input_channels);
void Preprocess(const cv::Mat & img,
std::vector < cv::Mat > *input_channels);
private:
shared_ptr < Net < float > > net_;
cv::Size input_geometry_;
int num_channels_;
cv::Mat mean_;
std::vector < string > labels_;
std::vector<cv::Mat> titles_; // 位图,用于显示 ..
};
#endif // CLASSIFIER_H