Deploy the model with MNN on Android Studio

1. Pre-environmental preparation

1.1 MNN environment compilation

See the article I wrote earlier for details: Article address

1.2 Installation of opencv for android

Enter the official website of opencv, download the installation package: download address
and unzip it after the download is complete
insert image description here

2. Android project environment configuration

2.1 Create an Android project

Open Android Studio and create a new Native C++ project
insert image description here

2.2 MNN environment configuration

  • Copy the libMNN.so file under the build_64 folder generated by the previous MNN compilation of Android to the app/libs/arm64-v8a folder under the Android project (the arm64-v8a folder is created by itself)

libs folder not found Switch mode to Project
insert image description here

insert image description here

  • Use CMakeLists.txt to compile
    and edit CMakeLists.txt under the cpp folder
    to add the following content
#----------------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
)
#----------------------------------------------
  • Modify the build.gradle file under app
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 environment configuration

  • Edit CMakeLists.txt under the cpp folder
#-----------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)
#-----------------------------------------------

Complete 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. Write MNN for model reasoning

According to your own model, write jni to call MNN for model reasoning.
Details will be updated in the future

Guess you like

Origin blog.csdn.net/qq_40042726/article/details/123818928