MNN学习笔记(六):配置visual studio项目

2023-12-23 10:48

本文主要是介绍MNN学习笔记(六):配置visual studio项目,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这个其实很简单,原因是MNN项目组已经提供了编译好的库:

1.下载编译好的MNN库

下载地址为:https://github.com/alibaba/MNN/releases

下载两个文件:Source.code和MNN-WindowsX64-0.2.1.7.zip

2.在visual studio上进行配置

注意visual studio版本为2017,我习惯把这些库都跟opencv放一起,具体来讲就是:

首先,把从Source code中解压的include文件如图1所示,复制出来;

然后,在自己的opencv的include文件夹下面新建一个MNN子文件夹,将上面的头文件复制过去:

其次,将解压好的MNN-WindowsX64-0.2.1.7.zip文件中MNN.dll和MNN.lib放到opencv对应位置:

最后,跟配置opencv一样,新建一个项目,配置项目:

3.测试代码

总共三个文件:

第一个文件:mobilenetssd.h

#ifndef _MOBILENET_SSD_H_
#define _MOBILENET_SSD_H_#include <vector>#include "MNN/Interpreter.hpp"
#include "MNN/MNNDefine.h"
#include "MNN/Tensor.hpp"
#include "MNN/ImageProcess.hpp"#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"namespace mirror {
struct ObjectInfo {std::string name_;cv::Rect location_;float score_;
};class MobilenetSSD {
public:MobilenetSSD();~MobilenetSSD();int Init(const char* root_path);int Detect(const cv::Mat& img_src, std::vector<ObjectInfo>* objects);
private:uint8_t* GetImage(const cv::Mat& img_src) {uchar* data_ptr = new uchar[img_src.total() * 4];cv::Mat img_tmp(img_src.size(), CV_8UC4, data_ptr);cv::cvtColor(img_src, img_tmp, CV_BGR2RGBA, 4);return (uint8_t*)img_tmp.data;}private:bool initialized_;const cv::Size inputSize_ = { 300, 300 };std::vector<int> dims_ = { 1, 3, 300, 300 };const float meanVals_[3] = { 0.5f, 0.5f, 0.5f };const float normVals_[3] = { 0.007843f, 0.007843f, 0.007843f };std::vector<std::string> class_names = {"background", "aeroplane", "bicycle", "bird", "boat","bottle", "bus", "car", "cat", "chair","cow", "diningtable", "dog", "horse","motorbike", "person", "pottedplant","sheep", "sofa", "train", "tvmonitor"};std::shared_ptr<MNN::Interpreter> mobilenetssd_interpreter_;MNN::Session* mobilenetssd_sess_ = nullptr;MNN::Tensor* input_tensor_ = nullptr;std::shared_ptr<MNN::CV::ImageProcess> pretreat_data_ = nullptr;};}#endif // !_MOBILENET_SSD_H_

第二个文件:mobilenetssd.cpp

#include "mobilenetssd.h"
#include <iostream>
#include <string>#include "opencv2/imgproc.hpp"namespace mirror {MobilenetSSD::MobilenetSSD() {initialized_ = false;
}MobilenetSSD::~MobilenetSSD() {mobilenetssd_interpreter_->releaseModel();mobilenetssd_interpreter_->releaseSession(mobilenetssd_sess_);
}int MobilenetSSD::Init(const char * root_path) {std::cout << "start Init." << std::endl;std::string model_file = std::string(root_path) + "/mobilenetssd.mnn";mobilenetssd_interpreter_ = std::unique_ptr<MNN::Interpreter>(MNN::Interpreter::createFromFile(model_file.c_str()));if (nullptr == mobilenetssd_interpreter_) {std::cout << "load model failed." << std::endl;return 10000;}MNN::ScheduleConfig schedule_config;schedule_config.type = MNN_FORWARD_CPU;schedule_config.numThread = 4;MNN::BackendConfig backend_config;backend_config.precision = MNN::BackendConfig::Precision_High;backend_config.power = MNN::BackendConfig::Power_High;schedule_config.backendConfig = &backend_config;mobilenetssd_sess_ = mobilenetssd_interpreter_->createSession(schedule_config);// image processerMNN::CV::Matrix trans;trans.setScale(1.0f, 1.0f);MNN::CV::ImageProcess::Config img_config;img_config.filterType = MNN::CV::BICUBIC;::memcpy(img_config.mean, meanVals_, sizeof(meanVals_));::memcpy(img_config.normal, normVals_, sizeof(normVals_));img_config.sourceFormat = MNN::CV::RGBA;img_config.destFormat = MNN::CV::RGB;pretreat_data_ = std::shared_ptr<MNN::CV::ImageProcess>(MNN::CV::ImageProcess::create(img_config));pretreat_data_->setMatrix(trans);std::string input_name = "data";input_tensor_ = mobilenetssd_interpreter_->getSessionInput(mobilenetssd_sess_, input_name.c_str());mobilenetssd_interpreter_->resizeTensor(input_tensor_, dims_);mobilenetssd_interpreter_->resizeSession(mobilenetssd_sess_);initialized_ = true;std::cout << "end Init." << std::endl;return 0;
}int MobilenetSSD::Detect(const cv::Mat & img_src, std::vector<ObjectInfo>* objects) {std::cout << "start detect." << std::endl;if (!initialized_) {std::cout << "model uninitialized." << std::endl;return 10000;}if (img_src.empty()) {std::cout << "input empty." << std::endl;return 10001;}int width = img_src.cols;int height = img_src.rows;// preprocesscv::Mat img_resized;cv::resize(img_src, img_resized, inputSize_);uint8_t* data_ptr = GetImage(img_resized);pretreat_data_->convert(data_ptr, inputSize_.width, inputSize_.height, 0, input_tensor_);mobilenetssd_interpreter_->runSession(mobilenetssd_sess_);std::string output_name = "detection_out";MNN::Tensor* output_tensor = mobilenetssd_interpreter_->getSessionOutput(mobilenetssd_sess_, output_name.c_str());// copy to hostMNN::Tensor output_host(output_tensor, output_tensor->getDimensionType());output_tensor->copyToHostTensor(&output_host);auto output_ptr = output_host.host<float>();for (int i = 0; i < output_host.height(); ++i) {int index = i * output_host.width();ObjectInfo object;object.name_ = class_names[int(output_ptr[index + 0])];object.score_ = output_ptr[index + 1];object.location_.x = output_ptr[index + 2] * width;object.location_.y = output_ptr[index + 3] * height;object.location_.width = output_ptr[index + 4] * width - object.location_.x;object.location_.height = output_ptr[index + 5] * height - object.location_.y;objects->push_back(object);}std::cout << "end detect." << std::endl;return 0;
}}

第三个文件:main.cpp

#include "mobilenetssd.h"
#include "opencv2/opencv.hpp"int main(int argc, char* argv[]){const char* img_path = "./data/images/test.jpg";cv::Mat img_src = cv::imread(img_path);mirror::MobilenetSSD* mobilenetssd = new mirror::MobilenetSSD();const char* root_path = "./data/models";mobilenetssd->Init(root_path);std::vector<mirror::ObjectInfo> objects;mobilenetssd->Detect(img_src, &objects);int num_objects = static_cast<int>(objects.size());for (int i = 0; i < num_objects; ++i) {std::cout << "location: " << objects[i].location_ << std::endl;cv::rectangle(img_src, objects[i].location_, cv::Scalar(255, 0, 255), 2);char text[256];sprintf_s(text, "%s %.1f%%", objects[i].name_.c_str(), objects[i].score_ * 100);int baseLine = 0;cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);cv::putText(img_src, text, cv::Point(objects[i].location_.x,objects[i].location_.y + label_size.height),cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));}cv::imwrite("./data/images/cat.jpg", img_src);cv::imshow("result", img_src);cv::waitKey(0);delete mobilenetssd;system("pause");return 0;
}

最后结果:

需要用到的模型和测试图片下载地址:https://download.csdn.net/download/sinat_31425585/12137855

打完收工!

这篇关于MNN学习笔记(六):配置visual studio项目的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/527761

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

Zookeeper安装和配置说明

一、Zookeeper的搭建方式 Zookeeper安装方式有三种,单机模式和集群模式以及伪集群模式。 ■ 单机模式:Zookeeper只运行在一台服务器上,适合测试环境; ■ 伪集群模式:就是在一台物理机上运行多个Zookeeper 实例; ■ 集群模式:Zookeeper运行于一个集群上,适合生产环境,这个计算机集群被称为一个“集合体”(ensemble) Zookeeper通过复制来实现

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

CentOS7安装配置mysql5.7 tar免安装版

一、CentOS7.4系统自带mariadb # 查看系统自带的Mariadb[root@localhost~]# rpm -qa|grep mariadbmariadb-libs-5.5.44-2.el7.centos.x86_64# 卸载系统自带的Mariadb[root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7

hadoop开启回收站配置

开启回收站功能,可以将删除的文件在不超时的情况下,恢复原数据,起到防止误删除、备份等作用。 开启回收站功能参数说明 (1)默认值fs.trash.interval = 0,0表示禁用回收站;其他值表示设置文件的存活时间。 (2)默认值fs.trash.checkpoint.interval = 0,检查回收站的间隔时间。如果该值为0,则该值设置和fs.trash.interval的参数值相等。

NameNode内存生产配置

Hadoop2.x 系列,配置 NameNode 内存 NameNode 内存默认 2000m ,如果服务器内存 4G , NameNode 内存可以配置 3g 。在 hadoop-env.sh 文件中配置如下。 HADOOP_NAMENODE_OPTS=-Xmx3072m Hadoop3.x 系列,配置 Nam

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

如何用Docker运行Django项目

本章教程,介绍如何用Docker创建一个Django,并运行能够访问。 一、拉取镜像 这里我们使用python3.11版本的docker镜像 docker pull python:3.11 二、运行容器 这里我们将容器内部的8080端口,映射到宿主机的80端口上。 docker run -itd --name python311 -p

学习hash总结

2014/1/29/   最近刚开始学hash,名字很陌生,但是hash的思想却很熟悉,以前早就做过此类的题,但是不知道这就是hash思想而已,说白了hash就是一个映射,往往灵活利用数组的下标来实现算法,hash的作用:1、判重;2、统计次数;