First time here? Check out the FAQ!

Ask Your Question
1

FaceRecognizer.java in Opencv4Android 2.4.3

asked Nov 23 '12

Afnman gravatar image

updated Nov 23 '12

Hello, I am trying to use FaceRecognizer class (from contrib folder).

I have created LBPFaceRecognizer class that inherits from FaceRecognizer class with one method nativecreateObject.

the corresponding JNI nativecreateOBject function:

   JNIEXPORT jlong JNICALL Java_org_opencv_samples_fd_LBPFaceRecognizer_nativeCreateObject
(JNIEnv * jenv, jclass)
{
     Ptr<FaceRecognizer> model;
              model= createLBPHFaceRecognizer();   
return  (jlong)model.obj; //Problem here!?!?!?!? What should I return?
}

Then I call the nativeCreateObject in the constructor of LBPFaceRecognizer:

public LBPFaceRecognizer() {
        super(nativeCreateObject());}

The problem is here: (inside the OnManagerConnected()):

BPFaceRecognizer model= new LBPFaceRecognizer();    
                 int r=model.getInt("radius"); //!?!?! CRASH HERE

I get Fatal signal 11 (SIGSEGV) at 0x00000028. when the native method getInt0(nativeObject,"radious") is called.

how can I use the FaceRecognizer ??

Preview: (hide)

4 answers

Sort by » oldest newest most voted
2

answered Nov 24 '12

  1. FaceRecognizer like all Java wrappers has nativeObj field that stores pointer to native OpenCV object. So you do not need to construct native object manually. You just can pass value of nativeObj to any JNI function and use it as pointer on conrespondent C++ object in JNI code.
  2. cv::Ptr is a smart pointer with reference counting. In JNI code it calls object destruction after on exit from function body. That's why you get invalid object and strange segfault. You need to reorganize JNI part in way like this:

    JNIEXPORT jlong JNICALL Java_org_opencv_samples_fd_LBPFaceRecognizer_nativeCreateObject (JNIEnv * jenv, jclass) { FaceRecognizer* model = createLBPHFaceRecognizer();
    return (jlong)model; }

Also you need to implement some release() method for native object destruction.

Preview: (hide)
1

answered Nov 26 '12

Afnman gravatar image

I have found a solution to my problem by doing this trick:

 Ptr<FaceRecognizer> model =createLBPHFaceRecognizer();
 model.addref();
 jlong ptr =(jlong)model.obj;

where ptr is the nativeObject's address.

Preview: (hide)

Comments

Same issue I found in android while using javacv. Any suggestion to get rid of it in java way

bunta gravatar imagebunta (Oct 9 '13)edit
0

answered Dec 14 '12

V.G. gravatar image

If you believe, that this issue has been resolved, could you please accept the most appropriate answer (even if it's your own one) and close this question. It would greatly improve navigation and overall experience with OpenCV Q&A.

Thanks.

Preview: (hide)

Comments

Sorry, I can't accept my own answer due to......50 points thing.

Afnman gravatar imageAfnman (Jan 16 '13)edit
0

answered Nov 26 '12

Afnman gravatar image

Thank you very much for your answers. Actually, after this(in JNI function):

 FaceRecognizer* model = createLBPHFaceRecognizer();
int k=model->getInt("radius");  //error here

I get segfault but if I use Ptr<facerecognizer> model=createLBPHFaceRecognizer() then I don't. I still have issues with the pointer returned by createLBPHFaceRecognizer.

I have done this:

-First I call

 JNIEXPORT jlong JNICALL Java_org_opencv_samples_fd_LBPFaceRecognizer_nativeCreateObject (JNIEnv * jenv, jclass) { FaceRecognizer* model = createLBPHFaceRecognizer();
return (jlong)model; }

that returns jlong memory address and stored it in my wrapper object.

Then I call

JNIEXPORT int JNICALL Java_org_opencv_samples_fd_LBPFaceRecognizer_nativeGetNeighbor
(JNIEnv * jenv, jclass, jlong ptr) {
int neigh=((FaceRecognizer*)ptr)->getInt("radius"),
return neigh;
}

with ptr equal to model's address returned by the previous function (nativeCreateObject) I still get segfault.

Preview: (hide)

Comments

did this really solved your problem?? I am also working on opencv 2.4.9 and having same problem getting segfault. Can you please explain where did you created LBPHFaceRecognizer class ?? it will be very helpful thanks in advance :)

ZOHAIBKHAN gravatar imageZOHAIBKHAN (Jul 23 '15)edit

Question Tools

Stats

Asked: Nov 23 '12

Seen: 3,927 times

Last updated: Dec 14 '12