1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| #include <MNN/Interpreter.hpp> #include <MNN/Matrix.h> #include <MNN/ImageProcess.hpp> #include <iostream> #include <opencv2/opencv.hpp> #include <opencv2/dnn/dnn.hpp> #include <sys/time.h>
cv::Mat ShowMat(const cv::Mat& src) { double min; double max; cv::minMaxIdx(src, &min, &max); cv::Mat adjMap;
float scale = 255 / (max - min); src.convertTo(adjMap, CV_8UC1, scale, -min * scale);
cv::Mat falseColorsMap; cv::applyColorMap(adjMap, falseColorsMap, cv::COLORMAP_PINK);
return falseColorsMap; }
int main(int argc, char* argv[]) { std::string img_file = "/Users/luohanjie/Workspace/Vision/depth_estimation/MiDaS/input/squirrel_iphone_sample3.png"; std::string model_file = "/Users/luohanjie/Workspace/Vision/my_slam/data/models/dpt_swin2_tiny_256/dpt_swin2_tiny_256.mnn";
cv::Mat img = cv::imread(img_file); if (img.empty()) { std::cout << "Can not load image: " << img_file << std::endl; return 0; }
int width_ori = img.cols; int height_ori = img.rows;
std::shared_ptr<MNN::Interpreter> net(MNN::Interpreter::createFromFile(model_file.c_str()), MNN::Interpreter::destroy); if (net == NULL) { std::cout << "Can not load model: " << model_file << std::endl; return 0; }
MNN::ScheduleConfig session_config; session_config.type = MNN_FORWARD_AUTO;
MNN::Session* session = net->createSession(session_config);
MNN::Tensor* input = net->getSessionInput(session, "input.1"); MNN::Tensor* output = net->getSessionOutput(session, "3335");
std::vector<int> input_dims = input->shape(); int input_n = input_dims[0]; int input_c = input_dims[1]; int input_h = input_dims[2]; int input_w = input_dims[3]; std::cout << "Model input_n: "<<input_n<<", input_c: " << input_c<<", input_h: " << input_h << ", input_w: " << input_w << std::endl;
std::vector<int> output_dims = output->shape(); int output_c = output_dims[0]; int output_h = output_dims[1]; int output_w = output_dims[2]; std::cout << "Model output_c: "<<output_c<<", output_h: " << output_h << ", output_w: " << output_w << std::endl;
float mean = 0.5f; float std = 0.5f;
cv::Mat img_nchw; cv::dnn::blobFromImage(img, img_nchw, 1 / (255 * std), cv::Size(input_w, input_h), - mean / std, true);
MNN::Tensor* tensor_nchw = new MNN::Tensor(input, MNN::Tensor::CAFFE); MNN::Tensor* tensor_depth = new MNN::Tensor(output, MNN::Tensor::CAFFE);
memcpy(tensor_nchw->host<float>(), img_nchw.data, tensor_nchw->size());
input->copyFromHostTensor(tensor_nchw); net->runSession(session);
output->copyToHostTensor(tensor_depth);
cv::Mat img_depth(output_h, output_w, CV_32FC1); memcpy(img_depth.data, tensor_depth->host<float>(), tensor_depth->size());
cv::resize(img_depth, img_depth, cv::Size(width_ori, height_ori));
cv::Mat img_show = ShowMat(img_depth); cv::imshow("img_depth", img_show); cv::waitKey(0);
delete tensor_nchw; delete tensor_depth;
return 1; }
|