Skip to content

Instantly share code, notes, and snippets.

@berak
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save berak/9830239 to your computer and use it in GitHub Desktop.
Save berak/9830239 to your computer and use it in GitHub Desktop.
missing cv::createLBPHFaceRecognizer() code for java/jni wrap
//
// facerec.cpp, this goes into your dll/so.
// this is just an example for lbp, but it will work similar for fisher and eigen, too.
//
#include "jni.h"
#include "opencv2/contrib/contrib.hpp"
//
// the create****FaceRecognizer functions return a cv::Ptr<FaceRecognizer>.
// if this thing leaves the scope(our function returns), it will destroy the instance.
// so we have to something about that (adding a refcount):
//
template<typename Y>
Y * extractPtr(cv::Ptr<Y> & y)
{
#if (CV_MAJOR_VERSION > 2) // 3.0 / master
new (&y)cv::Ptr<Y>(y); // inplace new ;)
return y.get();
#else // 2.4
y.addref();
return y.obj;
#endif
}
#ifdef __cplusplus
extern "C" {
#endif
// don't worry about the _10 , _1 resolves to a single underscore
JNIEXPORT jlong JNICALL Java_LBPHFaceRecognizer_createLBPHFaceRecognizer_10(JNIEnv* env, jclass);
JNIEXPORT jlong JNICALL Java_LBPHFaceRecognizer_createLBPHFaceRecognizer_10(JNIEnv* env, jclass) {
try {
cv::FaceRecognizer * pfr = extractPtr<cv::FaceRecognizer>(cv::createLBPHFaceRecognizer());
return (jlong) pfr;
} catch (...) {
jclass je = env->FindClass("java/lang/Exception");
env->ThrowNew(je, "sorry, dave..");
}
return 0;
}
JNIEXPORT jlong JNICALL Java_LBPHFaceRecognizer_createLBPHFaceRecognizer_11(JNIEnv* env, jclass, jint radius);
JNIEXPORT jlong JNICALL Java_LBPHFaceRecognizer_createLBPHFaceRecognizer_11(JNIEnv* env, jclass, jint radius) {
try {
cv::FaceRecognizer * pfr = extractPtr<cv::FaceRecognizer>(cv::createLBPHFaceRecognizer((int)radius));
return (jlong) pfr;
} catch (...) {
jclass je = env->FindClass("java/lang/Exception");
env->ThrowNew(je, "sorry, dave..");
}
return 0;
}
JNIEXPORT jlong JNICALL Java_LBPHFaceRecognizer_createLBPHFaceRecognizer_12(JNIEnv* env, jclass, jint radius, jint neighbours);
JNIEXPORT jlong JNICALL Java_LBPHFaceRecognizer_createLBPHFaceRecognizer_12(JNIEnv* env, jclass, jint radius, jint neighbours) {
try {
cv::FaceRecognizer * pfr = extractPtr<cv::FaceRecognizer>(cv::createLBPHFaceRecognizer((int)radius,(int)neighbours));
return (jlong) pfr;
} catch (...) {
jclass je = env->FindClass("java/lang/Exception");
env->ThrowNew(je, "sorry, dave..");
}
return 0;
}
// and so on for the remaining args ....
#ifdef __cplusplus
}
#endif
//
// --8<--------- snip here ------------------------
//
// LBPHFaceRecognizer.java
//
import org.opencv.contrib.FaceRecognizer;
import org.opencv.core.Core;
public class LBPHFaceRecognizer extends FaceRecognizer {
static{ System.loadLibrary("facerec"); } // for eclipse, the dll goes toplevel into your project dir
private static native long createLBPHFaceRecognizer_0();
private static native long createLBPHFaceRecognizer_1(int radius);
private static native long createLBPHFaceRecognizer_2(int radius,int neighbours);
public LBPHFaceRecognizer() {
super(createLBPHFaceRecognizer_0());
}
public LBPHFaceRecognizer(int radius) {
super(createLBPHFaceRecognizer_1(radius));
}
public LBPHFaceRecognizer(int radius,int neighbours) {
super(createLBPHFaceRecognizer_2(radius,neighbours));
}
}
//
// --8<--------- snip here ------------------------
//
// main java code:
//
private FaceRecognizer facerec;
public void init()
{
facerec = new LBPHFaceRecognizer(8,8);
}
@nwittstruck
Copy link

This produces a compile error when using clang. Any idea why? How are you creating the library?

facerec.cpp:54:36: error: no matching function for call to 'extractPtr'
    cv::FaceRecognizer * pfr = extractPtr<cv::FaceRecognizer>(cv::createLBPHFaceRecognizer((int)radius,(int)neighbours));
                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
facerec.cpp:16:5: note: candidate function [with Y = cv::FaceRecognizer] not viable: expects an l-value for 1st argument
Y * extractPtr(cv::Ptr<Y> & y)
     ^

@berak
Copy link
Author

berak commented Apr 9, 2014

ouch, i did not get notified. sorry for the delay.

no, i did not try clang (on win here)

hmm. honestly, i have problems understanding the error.

@berak
Copy link
Author

berak commented Apr 9, 2014

that extractPtr function is probably way over the top.

for 2.4, might as well try:

 cv::Ptr<cv::FaceRecognizer> pfr = cv::createLBPHFaceRecognizer();
 pfr.addref();
 return (jlong) pfr.obj;

@pnucci
Copy link

pnucci commented May 22, 2014

would you be kind giving us a link to your x86 compiled .DLL file ?
here there is no C++ environment installed
thanks in advance!

@vladfatu
Copy link

vladfatu commented Sep 8, 2014

Hi, I was able to make the .so file, but now I get a "symbol lookup error:" for "undefined symbol: _ZN2cv24createLBPHFaceRecognizerEiiiid"

@temppost2
Copy link

Is there any fork of opencv repo with implemented JNI wrap like posted here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment