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);
}
@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