Ask Your Question
0

dnn::net forward in native C++ Android Studio

asked 2018-10-23 11:21:54 -0500

mats86 gravatar image

updated 2018-10-23 11:36:32 -0500

berak gravatar image

Hallo,

I want entwickelt native c++ App in Android studio but I come cross undefined reference to 'cv::dnn.Net::forward(out, layerNames)".

I need help how can I use net.forward(out, layerNames) in native c++.

thanks

hier ist my Code

#include <jni.h>
#include <string>

#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/dnn.hpp>

using namespace std;
using namespace cv;
using namespace cv::dnn;


extern "C" JNIEXPORT jstring JNICALL
Java_com_example_mats86_myapplication_MainActivity_stringFromJNI(
    JNIEnv *env,
    jobject /* this */) {
std::string hello = "Hello from C++";

float confThreshold = 0.5;
float nmsThreshold = 0.4;
int newW = 320;
int newH = 320;
String model = "/storage/emulated/0/DCIM/ImageProcessing/tessdata/frozen_east_text_detection.pb";

string imPath = "/storage/emulated/0/DCIM/ImageProcessing/test.jpg";

Mat im = imread(imPath, IMREAD_COLOR);
Mat orig;
im.copyTo(im);
int origH = im.rows;
int origW = im.cols;

int rW = (int) (origW / float(newW));
int rH = (int) (origH / float(newH));

resize(im, im, Size(newW, newH));
int H = im.rows;
int W = im.cols;

vector<Mat> outs;
vector<String> layerNames(2);
layerNames[0] = "feature_fusion/Conv_7/Sigmoid";
layerNames[1] = "feature_fusion/concat_3";

Net net = readNet(model);

// construct a blob from the image and then perform a forward pass of
// the model to obtain the two output layer sets
Mat frame, blob;
blobFromImage(frame, blob, 1.0, Size(newW, newH), Scalar(123.68, 116.78, 103.94), true, false);
net.setInput(blob);
net.forward(outs, layerNames);

return env->NewStringUTF(hello.c_str());
}

error

undefined reference to `cv::dnn::experimental_dnn_34_v7::Net::forward(cv::_OutputArray const&,     std::__ndk1::vector<cv::String, std::__ndk1::allocator<cv::String> > const&)'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

hier is my Cmakelist

cmake_minimum_required(VERSION 3.4.1)

# 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.

set(CMAKE_VERBOSE_MAKEFILE on)
set(libs "${CMAKE_SOURCE_DIR}/src/main/jniLibs")
include_directories(/Users/mats86/androidTools/opencv-3.4.3-android-sdk/sdk/native/jni/include)

add_library(libopencv_java3 SHARED IMPORTED)
set_target_properties(libopencv_java3 PROPERTIES IMPORTED_LOCATION "${libs}/${ANDROID_ABI}/libopencv_java3.so")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -frtti -fexceptions")

add_library( # Sets the name of the library.
    native-lib

    # Sets the library as a shared library.
    SHARED

    # Provides a relative path to your source file(s).
    src/main/cpp/native-lib.cpp)

# 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 ...
(more)
edit retag flag offensive close merge delete

Comments

I'm having the same issue. Were you able to resolve it?

JPaul gravatar imageJPaul ( 2018-11-29 14:20:46 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-11-30 09:23:14 -0500

JPaul gravatar image

I was able to get past this issue. The problem appears to have been caused because the NDK I was using was built against a different standard template library (libc++) than OpenCV was built against (gnustl). I changed my Android Studio project to use gnustl and then had to downgrade my NDK to v16 because the gnustl library is no longer part of newer NDKs. Doing this resolved all my linker issues. See the following discussions for more details. These are what helped me:

https://stackoverflow.com/questions/5...

https://stackoverflow.com/questions/5...

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-10-23 11:21:54 -0500

Seen: 380 times

Last updated: Oct 23 '18