- #include < vector > #include < string.h > #include < caffe / caffe.hpp > #include < opencv2 / core / core.hpp > #include < opencv2 / imgproc / imgproc.hpp > #include < opencv2 / highgui / highgui.hpp >
- using namespace caffe;
- using namespace std;
- int main() {
- /*Load the network*/
- string proto = "scripts/deploy_resnet50by2_pool.prototxt";
- Phase phase = TEST;
- Caffe: :set_mode(Caffe: :GPU);
- Caffe: :SetDevice(0);
- boost: :shared_ptr < Net < float > >net(new caffe: :Net < float > (proto, phase));
- string model = "model/train_iter_40000.caffemodel";
- net - >CopyTrainedLayersFrom(model);
- CHECK_EQ(net - >num_inputs(), 1) << "Network should have exactly one input.";
- CHECK_EQ(net - >num_outputs(), 1) << "Network should have exactly one output.";
- /*Convert image to input blob. Blob: num, channel, height, width*/
- Blob < float > *input_blob = net - >input_blobs()[0];
- int input_channel = input_blob - >channels();
- int input_height = input_blob - >height();
- int input_width = input_blob - >width();
- cout << "The size of input image should be " << input_width << "*" << input_height << endl;
- vector < cv: :Mat > input_channels; //The value of input image‘s each channel
- float * input_data = input_blob - >mutable_cpu_data();
- for (int i = 0; i < input_channel; i++) {
- cv: :Mat channel(input_height, input_width, CV_32FC1, input_data);
- input_channels.push_back(channel);
- input_data += input_width * input_height;
- }
- string imgPath = "images/1.png";
- cv: :Mat image = cv: :imread(imgPath);
- cv: :Size input_size = cv: :Size(input_width, input_height);
- cv: :Mat image_resized;
- cv: :resize(image, image_resized, input_size);
- cv: :imshow("image", image_resized);
- cv: :waitKey(0);
- cv: :Mat image_float;
- image_resized.convertTo(image_float, CV_32FC3);
- cv: :Mat image_normalized;
- cv: :Mat mean(input_height, input_width, CV_32FC3, cv: :Scalar(104, 117, 123));
- cv: :subtract(image_float, mean, image_normalized);
- cv: :split(image_normalized, input_channels);
- net - >Forward();
- /*Convert output blob to image*/
- Blob < float > *output_blob = net - >output_blobs()[0];
- int output_height = output_blob - >height();
- int output_width = output_blob - >width();
- float * output_data = output_blob - >mutable_cpu_data();
- cv: :Mat depth(output_height, output_width, CV_32FC1, output_data);
- cout << depth.at < float > (5, 5);
- cv: :Mat depth_uint8;
- depth.convertTo(depth_uint8, CV_8UC1);
- cv: :imshow("depth", depth_uint8);
- cv: :waitKey(0);
- /*Generate 3D image with rgb image and estimated depth*/
- return 0;
- }
来源: http://www.bubuko.com/infodetail-2315662.html