Last active
August 29, 2015 13:57
-
-
Save berak/9830239 to your computer and use it in GitHub Desktop.
missing cv::createLBPHFaceRecognizer() code for java/jni wrap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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); | |
} |
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.
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;
would you be kind giving us a link to your x86 compiled .DLL file ?
here there is no C++ environment installed
thanks in advance!
Hi, I was able to make the .so file, but now I get a "symbol lookup error:" for "undefined symbol: _ZN2cv24createLBPHFaceRecognizerEiiiid"
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
This produces a compile error when using clang. Any idea why? How are you creating the library?