Use ncnn darknet and transplanted into android

Project uses version is old version darknet2ncnn , after the author adapted to ncnn latest version .

Compile Android version ncnn

In linux compile, check out the official tutorial can be. Note ndk use the same version of android studio. Provided android studio ndk: Review local.properties, ndk.dir = D: \ softback \ Android \ android-ndk-r15c

Add the relevant source file

The darknet, ncnn, darknet2ncnn relevant source and header files in a directory added to the cpp directory, the directory structure is as follows:

├─darknet
│  ├─include
│  └─src
├─darknet2ncnn
│  ├─include
│  └─src
└─ncnn
    ├─include
    └─src

darknet Source given: compare.c:17:13 error: initializing 'network' (aka 'struct network') with an expression of incompatible type 'network *' (aka 'struct network *'); dereference with *.Review compare.c, many types and reference pointer corresponding change it on OK.

In the new darknet and darknet2ncnn package package, add the relevant source code, but compiled android studio in the source directory will be displayed in the cpp directory, it is unclear what the reason, but does not affect the compilation.

Write Cmakelists.txt

This is the focus of many of the problems that is not written Cmakelists.txt caused.

cmake_minimum_required(VERSION 2.8.10)

set(CMAKE_BUILD_TYPE RELEASE)

set(libs "${CMAKE_SOURCE_DIR}/src/main/jniLibs")
include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/darknet2ncnn/include
        ${CMAKE_SOURCE_DIR}/src/main/cpp/ncnn/include
        ${CMAKE_SOURCE_DIR}/src/main/cpp/darknet/include
        ${CMAKE_SOURCE_DIR}/src/main/cpp/darknet2ncnn/src
        ${CMAKE_SOURCE_DIR}/src/main/cpp/ncnn/src)

set(CMAKE_STATIC_LINKER_FLAGS "-lm  -pthread -fopenmp -lstdc++")
set(CMAKE_C_FLAGS  "${CMAKE_C_FLAGS} -Ofast -Wno-unused-result  -Wfatal-errors -fPIC -fno-rtti -fno-exceptions")
set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -std=c++11 -Ofast -Wno-unused-result  -Wfatal-errors -fPIC -fno-rtti -fno-exceptions")

add_library (libncnn STATIC IMPORTED)
set_target_properties(libncnn PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi-v7a/libncnn.a)

file(GLOB_RECURSE darknet_src ${CMAKE_SOURCE_DIR}/src/main/cpp/darknet/src/*.c)

set(darknet2ncnn_dir ${CMAKE_SOURCE_DIR}/src/main/cpp/darknet2ncnn/src)
set(darknet2ncnn_src ${darknet2ncnn_dir}/layer/darknet_activation.cpp
    ${darknet2ncnn_dir}/layer/darknet_shortcut.cpp
    ${darknet2ncnn_dir}/layer/yolov1_detection.cpp
    ${darknet2ncnn_dir}/layer/yolov3_detection.cpp
    ${darknet2ncnn_dir}/object_detection.cpp
    ${darknet2ncnn_dir}/register_darknet.cpp
    ${darknet2ncnn_dir}/darknet2ncnn.cpp)

set(ncnn_src ${CMAKE_SOURCE_DIR}/src/main/cpp/ncnn/src)

set(lib_src ${darknet_src} ${darknet2ncnn_src} ${CMAKE_SOURCE_DIR}/src/main/cpp/yolov3-tiny-jni.cpp)

add_library( # Sets the name of the library.
        yolov3_tiny_jni

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        ${lib_src})

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)

target_link_libraries( # Specifies the target library.
        yolov3_tiny_jni
        libncnn
        jnigraphics

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

Error Resolution

Error: undefined reference to 'typeinfo for ncnn :: Layer', encountered in this arm-linux cross compiling, adding -fno-rtti compiler option.

Error: fatal error: use of undeclared identifier 'nullptr', adding -std = c ++ 11 compiler option.

make project successful compilation, so generating location: App \ Build \ Intermediates \ CMake \ Debug \ obj \ armeabi-V7a
Build APK given: Cause: org.jetbrains.plugins.gradle.tooling.util.ModuleComponentIdentifierImpl.getModuleIdentifier () Lorg / gradle / api / artifacts / ModuleIdentifier; update to android studio.

ex.extract return -100 generally did not find the target, there are two possibilities, one is the model correctly detected correctly, test images had no goals other cases, the model in question, can not be detected properly to our goal . I have repeatedly checked the test pictures and models no problem, and finally found still loaded model error:

private String getPathFromAssets(String assetsFileName){
    File f = new File(getCacheDir()+"/"+assetsFileName);
    if (!f.exists())
        try {
        InputStream is = getAssets().open(assetsFileName);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        FileOutputStream fos = new FileOutputStream(f);
        fos.write(buffer);
        fos.close();
    } catch (Exception e) { throw new RuntimeException(e); }
    return f.getPath();
}

Previous models have been loaded into the cache, and then I replaced a model, but did not delete the cache, or the previous model, it has not detected the target, will be if (!f.exists())commented out, each time re-initialize all write caching, and finally successfully detected to the target.

to sum up

In fact, relatively simple, mainly to write a Cmakelists.txt. Refer to the following two items, thank the author chiefs.

reference

Android application layer

darknet2ncnn

Guess you like

Origin www.cnblogs.com/hjmuses/p/11483116.html