#include "deeplab_seg.h"
void Segmantation::ConvertToVecMatP(Blob<float> * blob, std::vector<float> & v_float, cv::Mat & outimg) {int c = blob->channels();
int h = blob->height();
int w = blob->width();
int n = blob->num();
if (blob->shape().size() == 1) {
v_float.resize(n);
for (int i = 0; i < n; i++) {
v_float[i] = blob->data_at(i, 0, 0, 0);
}
}
if (blob->shape().size() == 2) {
outimg = cv::Mat::zeros(n, c, CV_32FC1);
for (int i = 0; i < n; i++) {
for (int j = 0; j < c; j++) {
outimg.at<float>(i, j) = blob->data_at(i, j, 0, 0);
}
}
outimg.convertTo(outimg, CV_8UC1);
}
if (blob->shape().size() == 4) {
vector<Mat> tmpmats;
for (int j = 0; j < c; j++) {
cv::Mat tmp_mat(h, w, CV_32FC1,
(void*)((float*)blob->cpu_data() + (j * h * w)));
tmpmats.push_back(tmp_mat);
}
//merge(tmpmats,outimg);
outimg = tmpmats[0];
//outimg.convertTo(outimg, CV_8UC1);
}
}
void Segmantation::segment(const cv::Mat& frame, cv::Mat& mask){
Mat img = frame.clone();
int ori_height = img.rows;
int ori_width = img.cols;
img.convertTo(img, CV_32FC1);
img = img - cv::Scalar(102.9801, 115.9465, 122.7717);
cv::resize(img, img, cv::Size(513, 513), 0, 0, cv::INTER_LINEAR);
//img = img * 0.017;
std::vector<int > v_shape = { 1, 3, img.rows, img.cols };
net_->blob_by_name("data")->Reshape(v_shape);
// float* data = net_->input_blobs()[0]->mutable_cpu_data();
float *data = net_->blob_by_name("data")->mutable_cpu_data();
std::vector<cv::Mat> v_split;
for (int i = 0; i < 3; ++i) {
/* Extract an individual channel. */
cv::Mat channel(img.rows, img.cols, CV_32FC1, data);
v_split.push_back(channel);
data += img.rows * img.cols;
}
cv::split(img, v_split);
// 约2秒
for (int i = 0; i < 1; ++i) {
double t1 = cv::getTickCount();
net_->Forward();
double t2 = cv::getTickCount();
std::cout << "total time: " << (t2 - t1) / cv::getTickFrequency() << std::endl;
}
/* Copy the output layer to a std::vector */
Blob<float>* result_blob = net_->output_blobs()[0];
// const float* result1 = net_->blob_by_name("fc8_voc12")->cpu_data();
std::vector<float>v_float;
//cv::Mat outputmask;
ConvertToVecMatP(result_blob, v_float, mask);
}
#pragma once
#include <caffe/caffe.hpp>
//#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>
//#ifdef USE_OPENCV
using namespace caffe; // NOLINT(build/namespaces)
using std::string;
using namespace cv;
/* Pair (label, confidence) representing a prediction. */
class Segmantation {
public:
explicit Segmantation(const string& prototxt_file, const string& caffemodel_file){
#ifdef CPU_ONLY
Caffe::set_mode(Caffe::CPU);
#else
Caffe::set_mode(Caffe::GPU);
#endif
/* Load the network. */
net_.reset(new Net<float>(prototxt_file, TEST));
net_->CopyTrainedLayersFrom(caffemodel_file);
};
void segment(const cv::Mat& frame, cv::Mat& mask);
private:
caffe::shared_ptr<Net<float> > net_;
void ConvertToVecMatP(Blob<float> * blob, std::vector<float> & v_float, cv::Mat & outimg);
};