Pure virtual method called extending FaceRecognizer

asked 2016-03-09 15:46:57 -0600

foobar gravatar image

updated 2016-03-09 17:07:00 -0600

Hi everyone, i want to add a new method predict to the class FaceRecognizer in order to obtain all the labels and all the distances from the prediction both with FisherFace algorithm and LBPH algorithm. I use OpenCV 3.0.0 and my idea is to extend the class FaceRecognizer creating two classes ExtFisherFaceRecognizer and ExtLBPHFaceRecognizer showed below (for brevity i'll show only the code regarding FisherFace):

public class ExtFisherFaceRecognizer extends BasicFaceRecognizer{

protected ExtFisherFaceRecognizer(long addr)
{
   super(addr);
   System.out.println("Fisher Face Recognizer created");
}

public Vector predictVector(Mat image)
{
    return predictVec(nativeObj,image.nativeObj);
}

@Override
protected void finalize() throws Throwable {
    System.out.println("Java deleting fisher face recognizer");
    delete(nativeObj);
}

 private static native Vector predictVec(long nativeObj,long src_nativeObj);
 private static native void delete(long nativeObj);
}

I create the model using the class FaceExt that call the respective native methods:

public class FaceExt {

public static ExtFisherFaceRecognizer createExtFisherFaceRecognizer()
{
    ExtFisherFaceRecognizer recognizer = new ExtFisherFaceRecognizer(createFisherFaceRecognizer());
    return recognizer;
}

public static ExtLBPHFaceRecognizer createExtLBPHFaceRecognizer()
{
    ExtLBPHFaceRecognizer recognizer = new ExtLBPHFaceRecognizer(createLBPHFaceRecognizer());
    return recognizer;
}

private static native long createFisherFaceRecognizer();
private static native long createLBPHFaceRecognizer();
}

And this is the cpp file that implements the native methods; i omit the details regarding building of result vector to java side:

/*includes*/
Ptr<FaceRecognizer> fisherRecognizer;
Ptr<FaceRecognizer> lbphRecognizer;
extern "C" 
{
    JNIEXPORT jlong JNICALL Java_facerecognition_FaceExt_createFisherFaceRecognizer(JNIEnv *env, jclass    thisObj)
    {
        fisherRecognizer = createFisherFaceRecognizer();
        return (jlong) &fisherRecognizer;
    }
    JNIEXPORT jlong JNICALL Java_facerecognition_FaceExt_createLBPHFaceRecognizer(JNIEnv *env, jclass thisObj)
    {
      lbphRecognizer = createLBPHFaceRecognizer();
      return (jlong) &lbphRecognizer;
    }
   JNIEXPORT jobject JNICALL Java_facerecognition_ExtFisherFaceRecognizer_predictVec(JNIEnv *env, jclass thisObj, jlong model, jlong image)
   {
      try
      {
          Ptr<FaceRecognizer> faceRec = *((Ptr<FaceRecognizer>*) model);
          Mat& img = *((Mat*) image);
          faceRec->predict(img,label,dist);
          vector<int> labels;
          vector<double> confidences;
          faceRec->predictVec(img,labels,confidences);
          jclass classVector = env->FindClass("java/util/Vector");
                      ...
          jobject objVectorResult = env->NewObject(classVector,cntVector);
          ...
          return objVectorResult;
      }
      catch(cv::Exception e)
      { }
      catch (...)
      { }
  }
  JNIEXPORT jobject JNICALL Java_facerecognition_ExtLBPHFaceRecognizer_predictVec(JNIEnv *env, jclass thisObj, jlong model, jlong image)
  {
      try
      {
          Ptr<FaceRecognizer> faceRec = *((Ptr<FaceRecognizer>*) model);
          Mat& img = *((Mat*) image);
          vector<int> labels;
          vector<double> confidences;
          faceRec->predictVec(img,labels,confidences);
          jclass classVector = env->FindClass("java/util/Vector");
                      ...
          jobject objVectorResult = env->NewObject(classVector,cntVector);
          ...
          return objVectorResult;
      }
      catch(cv::Exception e)
      { }
      catch (...)
      { }
  }
  JNIEXPORT void JNICALL Java_facerecognition_ExtFisherFaceRecognizer_delete(JNIEnv *env, jclass thisObj, jlong model)
  {
      Ptr<FaceRecognizer> faceRec = *((Ptr<FaceRecognizer>*) model);
      delete faceRec;
      cout<<"C++ deleted fisher face recognizer"<<endl;
      return;
  }
  JNIEXPORT void JNICALL Java_facerecognition_ExtLBPHFaceRecognizer_delete(JNIEnv *env, jclass thisObj, jlong model)
  {
      Ptr<FaceRecognizer> faceRec = *((Ptr<FaceRecognizer>*) model);
      delete faceRec;
      cout<<"C++ deleted lbph face recognizer"<<endl;
      return;
  }
}

As you can see from the code above the prototype of the native method predictVec is the follow:

void predictVec(InputArray _src, vector<int>& labels, vector<double>& confidences)

I modified the source of OpenCV face module adding this new method in such way that works for what i want. Until i create the models all works fine but when i try to call an inherited method from BasicFaceRecognizer that extends the class FaceRecognizer or this new method predictVec, at the end of execution i obtain the error: pure virtual method called terminate called without an active exception. I have no ... (more)

edit retag flag offensive close merge delete