1 #include "itracker/itracker.h"
7 #include "caffe/caffe.hpp"
8 #include "opencv2/opencv.hpp"
41 caffe::BlobProto blob_proto;
42 caffe::ReadProtoFromBinaryFileOrDie(mean_file.
c_str(), &blob_proto);
44 caffe::Blob<float> mean_blob;
45 mean_blob.FromProto(blob_proto);
48 float* data = mean_blob.mutable_cpu_data();
49 for (
int i = 0; i < 3; ++i) {
52 data += mean_blob.height() * mean_blob.width();
59 return cv::Mat(224, 224, mean.
type(), channel_mean);
64 ITrackerImpl::ITrackerImpl(
const std::string& model_file,
71 new caffe::Net<float>(model_file, caffe::TEST));
73 this->net->CopyTrainedLayersFrom(weights_file);
76 for (
auto i = 0; i < 4; ++i) {
77 auto input = this->net->input_blobs()[i];
80 input->Reshape(shape);
86 util::load_mean(mean_left_eye_file),
87 util::load_mean(mean_right_eye_file),
88 util::load_mean(mean_face_file)
99 for (
int i = 0; i < 4; ++i) {
100 auto input = this->net->input_blobs()[i];
105 float* input_data = input->mutable_cpu_data();
106 for (
int j = 0; j < shape[1]; ++j) {
107 cv::Mat channel(shape[2], shape[3], CV_32FC1, input_data);
109 input_data += shape[2] * shape[3];
115 cv::Mat resized(shape[2], shape[3], CV_32FC3);
116 input_images[i].convertTo(input_image, CV_32FC3);
121 input_images[i].convertTo(input_image, CV_32FC1);
122 for (
int j = 0; j < shape[1]; ++j) {
123 channels[j] = input_image.
at<
float>(j);
129 this->net->Forward();
132 caffe::Blob<float>* output_layer = this->net->output_blobs()[0];
133 const float* begin = output_layer->cpu_data();
145 : impl(new
ITrackerImpl(model_file, weights_file, mean_left_eye_file,
146 mean_left_eye_file, mean_face_file)) {
151 return this->impl->predict(left_eye, right_eye, face, face_mask);
154 ITracker::~ITracker() {
Scalar mean(InputArray src, InputArray mask=noArray())
void split(const Mat &src, Mat *mvbegin)
cv::Vec2f predict(const cv::Mat &left_eye, const cv::Mat &right_eye, const cv::Mat &face, const cv::Mat &face_mask)
void push_back(const value_type &__x)
void subtract(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray(), int dtype=-1)
void merge(const Mat *mv, size_t count, OutputArray dst)
const char * c_str() const noexcept
A private implementation of ITracker to hide dependencies from including classes. ...
ITracker(const std::string &model_file, const std::string &weights_file, const std::string &mean_left_eye_file, const std::string &mean_right_eye_file, const std::string &mean_face_file)