Ask Your Question
1

FaceRecognizer.java in Opencv4Android 2.4.3

asked 2012-11-23 03:38:29 -0600

Afnman gravatar image

updated 2012-11-23 03:44:52 -0600

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 ??

edit retag flag offensive close merge delete

4 answers

Sort by ยป oldest newest most voted
2

answered 2012-11-24 10:53:32 -0600

  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.

edit flag offensive delete link more
1

answered 2012-11-26 05:42:35 -0600

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.

edit flag offensive delete link more

Comments

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

bunta gravatar imagebunta ( 2013-10-09 06:42:27 -0600 )edit
0

answered 2012-12-14 02:57:12 -0600

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.

edit flag offensive delete link more

Comments

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

Afnman gravatar imageAfnman ( 2013-01-16 03:10:03 -0600 )edit
0

answered 2012-11-26 03:54:28 -0600

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.

edit flag offensive delete link more

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 ( 2015-07-23 03:09:59 -0600 )edit

Question Tools

Stats

Asked: 2012-11-23 03:38:29 -0600

Seen: 3,634 times

Last updated: Dec 14 '12