Issue
I am using NDK to perform some opencv functions to see if using c++ will speed up the processing (I used Java Wrappers in another project). All was compiling fine until I added the detectMultiScale function in my class where suddenly the linker failed and produced the undefined reference error. The error will go away if I comment out that line.
I searched all over the net but no one seems to have the same problem aside from https://stackoverflow.com/questions/32022597/undefined-reference-to-cascadeclassifierdetectmultiscale where no answers were suggested. If anyone can find another relevant post, feel free to direct me there :)
Platform: Windows 10, Android Studio 2, OpenCV 3.1 (recompiled myself to add the face module)
main.cpp
#include <jni.h>
#include <string>
#include <android/log.h>
#include "com_yk_iskl_jnitest_FaceRecognition.h"
#define LOG_TAG "libnativeface"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
class FaceRecognition{
private:
cv::Ptr<cv::face::LBPHFaceRecognizer> lbph_model;
cv::Ptr<cv::face::FaceRecognizer> fisher_model;
cv::Ptr<cv::face::FaceRecognizer> eigen_model;
cv::CascadeClassifier face_cascade;
cv::CascadeClassifier eye_cascade;
public:
void loadLBPHModel(std::string& filepath)
{
lbph_model=cv::face::createLBPHFaceRecognizer();
try{
lbph_model->load(filepath);
}catch(cv::Exception e){
LOGE("%s",(e.msg).c_str());
}
lbph_model->setThreshold(999);
}
void loadFaceCascade(std::string& filepath)
{
try{
face_cascade.load(filepath);
}catch(cv::Exception e){
LOGE("%s",(e.msg).c_str());
}
}
void loadEyeCascade(std::string& filepath)
{
try{
eye_cascade.load(filepath);
}catch(cv::Exception e){
LOGE("%s",(e.msg).c_str());
}
}
double getThreshold()
{
return lbph_model->getThreshold();
}
std::vector<cv::Rect> detectFace(cv::Mat src)
{
std::vector<cv::Rect> faces;
face_cascade.detectMultiScale(src, faces, 1.1, 2, 0 | cv::CASCADE_SCALE_IMAGE, cv::Size(30, 30)); <--------------------------Heres the problem
return faces;
}
};
//---------------------------
JNIEXPORT jlong JNICALL
Java_com_yk_iskl_jnitest_FaceRecognition_createNativeFaceRecognition(JNIEnv *, jclass)
{
return (jlong)(new FaceRecognition());
}
JNIEXPORT void JNICALL
Java_com_yk_iskl_jnitest_FaceRecognition_loadLBPHModel(JNIEnv * env, jclass, jlong native_addr, jstring filename)
{
std::string fn((const char*)env->GetStringUTFChars(filename,0));
((FaceRecognition*)native_addr)->loadEyeCascade(fn);
}
JNIEXPORT void JNICALL
Java_com_yk_iskl_jnitest_FaceRecognition_loadEyeCascade(JNIEnv * env, jclass, jlong native_addr, jstring filename)
{
std::string fn((const char*)env->GetStringUTFChars(filename,0));
((FaceRecognition*)native_addr)->loadLBPHModel(fn);
}
JNIEXPORT jdouble JNICALL
Java_com_yk_iskl_jnitest_FaceRecognition_getThreshold(JNIEnv *env, jclass, jlong native_addr)
{
return (jdouble)((FaceRecognition*)native_addr)->getThreshold();
}
JNIEXPORT void JNICALL
Java_com_yk_iskl_jnitest_FaceRecognition_process(JNIEnv *env, jclass, jlong native_addr, jlong frame_addr)
{
cv::Mat* pframe_addr = (cv::Mat*)frame_addr;
cv::cvtColor(*pframe_addr,*pframe_addr,cv::COLOR_YUV420sp2GRAY);
std::vector<cv::Rect> faces=((FaceRecognition*)native_addr)->detectFace(*pframe_addr);
}
JNIEXPORT void JNICALL
Java_com_yk_iskl_jnitest_FaceRecognition_testReturns(JNIEnv *env, jclass, jlong native_addr, jobjectArray string, jintArray num)
{
//Get pointer from num
jint* parray=env->GetIntArrayElements(num,0);
parray[0]=888;
//Copy back to num
env->SetIntArrayRegion(num,0,1,parray);
}
//---------------------------
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
#opencv
OPENCVROOT:= C:\Users\<hidden>\Desktop\PROGRAMMING\OPENCV_ANDROID_BUILD\install
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
include ${OPENCVROOT}\sdk\native\jni\OpenCV.mk
#include $(CLEAR_VARS)
LOCAL_SRC_FILES := main.cpp
LOCAL_LDLIBS += -lz -lm -llog -landroid -ldl
LOCAL_MODULE := nativeface
include $(
And I got the following error while performing ndk build
C:/Users/<hidden>/AndroidStudioProjects/JNITEST/app/src/main/jni/main.cpp:59: error: undefined reference to 'cv::CascadeClassifier::detectMultiScale(cv::_InputArray const&, std::__ndk1::vector<cv::Rect_<int>, std::__ndk1::allocator<cv::Rect_<int> > >&, double, int, int, cv::Size_<int>, cv::Size_<int>)'
collect2.exe: error: ld returned 1 exit status
make: *** [C:\Users\<hidden>\AndroidStudioProjects\JNITEST\app/build/intermediates/ndk/obj/local/armeabi-v7a/libnativeface.so] Error 1
If there's more information you all need, just ask, I'll try to provide as much information as possible.
Solution
After long investigation and testing, I found the problem located in my Application.mk. Adding the following line fix the problem;
APP_STL:=gnustl_shared
Got the idea from this response that make me think whether I'm missing stl.
Answered By - Red Dango
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.