资源简介
该资源为人群计数c++实现,深度框架为caffe,网络模型文件为SaCNN,可对图片和视频进行人群计数,需要安装caffe和opencv库,内有makefile,可直接使用!
代码片段和文件信息
#include
#include
#define USE_OPENCV
#ifdef USE_OPENCV
#include
#include
#include
#endif // USE_OPENCV
#include
#include
#include
#include
#include
#include
#include
#include “featureExtract.h“
#ifdef USE_OPENCV
#define CPU_ONLY
using namespace caffe; // NOLINT(build/namespaces)
using std::string;
using namespace std;
using namespace cv;
/* Pair (label confidence) representing a prediction. */
typedef std::pair Prediction;
float mycosine(float* arrayAfloat* arrayBint length){
if(!arrayA||!arrayB) return 0;
double sumarrayA=0sumarrayB=0;
double cosine=0;
for(int i=0;i sumarrayA+=arrayA[i]*arrayA[i];
sumarrayB+=arrayB[i]*arrayB[i];
cosine+=arrayA[i]*arrayB[i];
}
sumarrayA=sqrt(sumarrayA);
sumarrayB=sqrt(sumarrayB);
if((sumarrayA-0<0.0001)||(sumarrayB-0<0.0001)){
return 0;
}
cosine/=(sumarrayA*sumarrayB);
// cout< return cosine;
}
FeatureExtract::FeatureExtract(const string& model_file
const string& trained_file) {
#ifdef CPU_ONLY
Caffe::set_mode(Caffe::CPU);
#else
Caffe::set_mode(Caffe::GPU);
#endif
net_lock = PTHREAD_MUTEX_INITIALIZER;
/* Load the network. */
net_.reset(new Net(model_file TEST));
net_->CopyTrainedlayersFrom(trained_file);
CHECK_EQ(net_->num_inputs() 1) << “Network should have exactly one input.“;
CHECK_EQ(net_->num_outputs() 1) << “Network should have exactly one output.“;
Blob* input_layer = net_->input_blobs()[0];
num_channels_ = input_layer->channels();
CHECK(num_channels_ == 3 || num_channels_ == 1)
<< “Input layer should have 1 or 3 channels.“;
input_geometry_ = cv::Size(input_layer->width() input_layer->height());
/* Load the binaryproto mean file. */
//SetMean(mean_file);
/* Load labels. */
/* std::ifstream labels(label_file.c_str());
CHECK(labels) << “Unable to open labels file “ << label_file;
string line;
while (std::getline(labels line))
labels_.push_back(string(line));
*/
Blob* output_layer = net_->output_blobs()[0];
//CHECK_EQ(labels_.size() output_layer->channels())
//<< “Number of labels is different from the output layer dimension.“;
}
int FeatureExtract::extractFeature(cv::Mat imgfloat &cnt)
{
cout<<“start extract!“< clock_t t0 = clock();
//std::vector vec_data(img.begin() img.end());
//cv::Mat testMat =cv::imdecode(vec_data 1);
cv::Mat result;
img.convertTo(result CV_32FC3);
//cv::Mat result2;
//transpose(resultresult2);
//cv::Mat result3;
//flip(imgresult31);
float f1[512]={0}f2[512]={0};
//pthread_mutex_lock (&net_lock);
Blob* input_layer = net_->input_blobs()[0];
input_layer->Reshape(1 num_channels_
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 818 2018-08-03 01:16 extracttest\Makefile
文件 91915666 2018-08-02 12:55 extracttest\ShanghaiTech_part_B.caffemodel
文件 631144 2018-08-24 09:32 extracttest\Vulture_Server
文件 9900 2018-08-24 09:26 extracttest\deploy.prototxt
文件 8476 2018-08-24 09:32 extracttest\featureExtract.cpp
文件 1417 2018-08-24 03:30 extracttest\featureExtract.h
文件 1036880 2018-08-24 09:32 extracttest\featureExtract.o
目录 0 2019-12-04 21:44 extracttest\
评论
共有 条评论