在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进行模型推理
具体日后更新

猜你喜欢

转载自blog.csdn.net/qq_40042726/article/details/123818928