在Android Studio上使用MNN部署模型

1.前期环境准备

1. 1 MNN环境编译

具体看我前面写的文章:文章地址

1.2 opencv for android的安装

进入opencv官网,下载安装包:下载地址
下载完成后解压即可
在这里插入图片描述

2.Android工程环境配置

2.1 创建Android工程

打开Android Studio新建一个Native C++工程
在这里插入图片描述

2.2 MNN环境配置

  • 将之前MNN编译Android的生成的build_64文件夹下的libMNN.so文件复制到Android工程下的app/libs/arm64-v8a文件夹下(arm64-v8a文件夹自己创建)

libs文件夹找不到将模式切换到Project
在这里插入图片描述

在这里插入图片描述

  • 使用CMakeLists.txt编译
    编辑cpp文件夹下的CMakeLists.txt
    添加如下内容
#----------------MNN环境---------------------
# MNN_DIR为自己安装的MNN的路径
set(MNN_DIR D:/APP/MNN)
# mnn的头文件
include_directories(${MNN_DIR}/include)
include_directories(${MNN_DIR}/include/MNN)
include_directories(${MNN_DIR}/tools)
include_directories(${MNN_DIR}/tools/cpp)
include_directories(${MNN_DIR}/source)
include_directories(${MNN_DIR}/source/backend)
include_directories(${MNN_DIR}/source/core)

set(dis_DIR ../../../../libs)
add_library(
        MNN
        SHARED
        IMPORTED
)
set_target_properties(
        MNN
        PROPERTIES IMPORTED_LOCATION
        ${dis_DIR}/arm64-v8a/libMNN.so
)
#----------------------------------------------
  • 修改app下的build.gradle文件
plugins {
    id 'com.android.application'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.cjpnice.lcnet_mnn"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags '-std=c++14'
                arguments "-DANDROID_STL=c++_shared"
                abiFilters  "arm64-v8a"
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    externalNativeBuild {
        cmake {
            path file('src/main/cpp/CMakeLists.txt')
            version '3.18.1'
        }
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

opencv环境配置

  • 编辑cpp文件夹下的CMakeLists.txt
#-----------opencv环境配置-----------------------
#设置OpenCV-android-sdk路径
set( OpenCV_DIR D:/APP/OpenCV-android-sdk/sdk/native/jni )
find_package(OpenCV REQUIRED )
if(OpenCV_FOUND)
    include_directories(${OpenCV_INCLUDE_DIRS})
    message(STATUS "OpenCV library status:")
    message(STATUS "    version: ${OpenCV_VERSION}")
    message(STATUS "    libraries: ${OpenCV_LIBS}")
    message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")
else(OpenCV_FOUND)
    message(FATAL_ERROR "OpenCV library not found")
endif(OpenCV_FOUND)
#-----------------------------------------------

完整的CMakeLists.txt

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.18.1)

# Declares and names the project.

project("lcnet_mnn")

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
#----------------MNN环境---------------------
# MNN_DIR为自己安装的MNN的路径
set(MNN_DIR D:/APP/MNN)
# mnn的头文件
include_directories(${MNN_DIR}/include)
include_directories(${MNN_DIR}/include/MNN)
include_directories(${MNN_DIR}/tools)
include_directories(${MNN_DIR}/tools/cpp)
include_directories(${MNN_DIR}/source)
include_directories(${MNN_DIR}/source/backend)
include_directories(${MNN_DIR}/source/core)

set(dis_DIR ../../../../libs)
add_library(
        MNN
        SHARED
        IMPORTED
)
set_target_properties(
        MNN
        PROPERTIES IMPORTED_LOCATION
        ${dis_DIR}/arm64-v8a/libMNN.so
)
#----------------------------------------------

#-----------opencv环境配置-----------------------
#设置OpenCV-android-sdk路径
set( OpenCV_DIR D:/APP/OpenCV-android-sdk/sdk/native/jni )
find_package(OpenCV REQUIRED )
if(OpenCV_FOUND)
    include_directories(${OpenCV_INCLUDE_DIRS})
    message(STATUS "OpenCV library status:")
    message(STATUS "    version: ${OpenCV_VERSION}")
    message(STATUS "    libraries: ${OpenCV_LIBS}")
    message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")
else(OpenCV_FOUND)
    message(FATAL_ERROR "OpenCV library not found")
endif(OpenCV_FOUND)
#-----------------------------------------------



aux_source_directory(. SRCS)
add_library( # Sets the name of the library.
        lcnet_mnn
        # Sets the library as a shared library.
        SHARED
        # Provides a relative path to your source file(s).
        ${SRCS})

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
        log-lib
        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
        lcnet_mnn
        # Links the target library to the log library
        # included in the NDK.
        MNN
        jnigraphics
        ${OpenCV_LIBS}
        ${log-lib})

3.编写MNN进行模型推理

根据自己的模型,编写jni调用MNN进行模型推理
具体日后更新

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
使用MNN部署YOLOv5,首先需要将YOLOv5模型转换为MNN格式。可以使用GitHub上的代码将YOLOv5模型转换为MNN模型。根据给定的命令,可以通过运行以下命令将YOLOv5s模型转换为MNN模型: ``` ./MNN-1.1.0/build/MNNConvert -f ONNX --modelFile yolov5s.onnx --MNNModel yolov5s.mnn --bizCode MNN ``` 同样地,将YOLOv5ss模型转换为MNN模型的命令如下: ``` ./MNN-1.1.0/build/MNNConvert -f ONNX --modelFile yolov5ss.onnx --MNNModel yolov5ss.mnn --bizCode MNN ``` 这些命令会将YOLOv5模型转换为MNN模型,并且可以根据需要进行相应的调整。 接下来,可以使用MNN库来加载和运行转换后的MNN模型。可以使用MNN提供的API来进行目标检测。具体的部署方式可以参考中的相关文档和示例代码。 对于导出YOLOv5的onnx文件,可以使用以下命令: ``` python export.py --weights weights/yolov5m.pt --include onnx ``` 这将导出yolov5m.pt为onnx文件,以供后续的模型转换和部署使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [【深度学习】YOLOv5 工程落地部署过程,MNN转化,使用细节](https://blog.csdn.net/x1131230123/article/details/125620342)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_-CHEN-_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值