概述
SaCNN原理可自行参考论文,它算是目前人群计数相对来说较好的几个模型之一,后期会写有关CSRNET人群计数,本文主要是用c++对模型扩展,进行实际测试,由于是很早之前所作,懒得再次测试,只贴部分代码,完整部分可依据链接下载SaCNN完整代码
2019-12-18添加:
shanghaiTech-PartB训练好的模型
代码实现
#ifndef FEATUREEXTRACT_H_
#define FEATUREEXTRACT_H_
#include <caffe/caffe.hpp>
#define USE_OPENCV
#ifdef USE_OPENCV
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#endif // USE_OPENCV
#include <algorithm>
#include <iosfwd>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <pthread.h>
#ifdef USE_OPENCV
using namespace caffe; // NOLINT(build/namespaces)
using std::string;
using namespace std;
/* Pair (label, confidence) representing a prediction. */
class FeatureExtract {
public:
FeatureExtract(const string& model_file,
const string& trained_file);
int extractFeature(cv::Mat img,float &cnt);
// std::vector<Prediction> Classify(const cv::Mat& img, int N = 5);
private:
// void SetMean(const string& mean_file);
pthread_mutex_t net_lock;
int Predict(const cv::Mat& img,float *output);
void WrapInputLayer(std::vector<cv::Mat>* input_channels);
void Preprocess(const cv::Mat& img,
std::vector<cv::Mat>* input_channels);
private:
boost::shared_ptr<Net<float> > net_;
cv::Size input_geometry_;
int num_channels_;
//std::vector<string> labels_;
};
float mycosine(float* arrayA,float* arrayB,int length);
#else
/*
int main(int argc, char** argv) {
LOG(FATAL) << "This example requires OpenCV; compile with USE_OPENCV.";
}*/
#endif // USE_OPENCV
#endif