Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Fail to build FaceRecognizer JNI package

I use the guide in this link http://stackoverflow.com/questions/22948553/issue-with-writing-facerecognizer-for-java-with-opencv-2-4-8, I use opencv2.4.11.

When I run the makefile, it reports the error with c code,and here is the error I got? What's wrong with the code I have? Do we have some sample code on OpenCV FaceRecognizer JNI part?

gcc -m64 -I"C:\Program Files\Java\jdk1.8.0_40\include" -I"C:\Program Files\Java\jdk1.8.0_40\include\win32" -I"D:\Project\javacv\opencv\opencv\build\include" -I"D:\Project\javacv\opencv\opencv\build\java\x64" -c LBPHFaceRecognizer.c -o LBPHFaceRecognizer.o LBPHFaceRecognizer.c: In function 'Java_com_test_LBPHFaceRecognizer_n_1createLBPHFaceRecognizer': LBPHFaceRecognizer.c:6:1: error: parameter name omitted JNIEXPORT jlong JNICALL Java_com_test_LBPHFaceRecognizer_n_1createLBPHFaceRecognizer(JNIEnv , jclass){ ^ LBPHFaceRecognizer.c:6:1: error: parameter name omitted LBPHFaceRecognizer.c:7:1: error: 'try' undeclared (first use in this function) try { ^ LBPHFaceRecognizer.c:7:1: note: each undeclared identifier is reported only once for each function it appears in LBPHFaceRecognizer.c:7:5: error: expected ';' before '{' token try { ^ makefile:18: recipe for target 'LBPHFaceRecognizer.o' failed make: ** [LBPHFaceRecognizer.o] Error 1

12:16:19 Build Finished (took 1s.633ms)

For java code I have is, package com.test; import org.opencv.contrib.FaceRecognizer;

public class LBPHFaceRecognizer extends FaceRecognizer {

static {
    System.loadLibrary("opencv_java248");
    System.loadLibrary("facerec");
}

private static native long n_createLBPHFaceRecognizer();

public LBPHFaceRecognizer() {
    super(n_createLBPHFaceRecognizer());
}

FaceRecognizer facerec = new LBPHFaceRecognizer();

}

The generate com_test_LBPHFaceRecognizer.h is "/* DO NOT EDIT THIS FILE - it is machine generated / include <jni.h> / Header for class com_test_LBPHFaceRecognizer */

ifndef _Included_com_test_LBPHFaceRecognizer define _Included_com_test_LBPHFaceRecognizer ifdef __cplusplus extern "C" { endif /* * Class: com_test_LBPHFaceRecognizer * Method: n_createLBPHFaceRecognizer * Signature: ()J */ JNIEXPORT jlong JNICALL Java_com_test_LBPHFaceRecognizer_n_1createLBPHFaceRecognizer (JNIEnv *, jclass);

ifdef __cplusplus } endif endif"

The LBPHFaceRecognizer.c I have is, "// facerec.dll include "jni.h" include "opencv2/contrib/contrib.hpp"

JNIEXPORT jlong JNICALL Java_com_test_LBPHFaceRecognizer_n_1createLBPHFaceRecognizer(JNIEnv *, jclass); JNIEXPORT jlong JNICALL Java_com_test_LBPHFaceRecognizer_n_1createLBPHFaceRecognizer(JNIEnv *, jclass){ try {

    cv::Ptr<cv::FaceRecognizer> ptr = cv::createLBPHFaceRecognizer();
    cv::FaceRecognizer * pf = ptr.get();
    ptr.addref(); //don't let it self-destroy here..
    return (jlong) pf;
} catch (...) {
    jclass je = env->FindClass("java/lang/Exception");
    env->ThrowNew(je, "Exception occurs to create LBPH face recognizer..");
}
return 0;

}“

The makefile to build c code and dll file, facerec.dll : LBPHFaceRecognizer.o gcc -m64 -Wl,--add-stdcall-alias -shared -o $@ $<

LBPHFaceRecognizer.o : LBPHFaceRecognizer.c LBPHFaceRecognizer.h gcc -m64 -I"$(JAVA_HOME)\include" -I"$(JAVA_HOME)\include\win32" -I"$(OPENCV_HOME)\build\include" -I"$(OPENCV_HOME)\build\java\x64" -c $< -o $@

I also tried with cpp code, and the error I got is FisherFaceRecognizer.o:FisherFaceRecognizer.cpp:(.text+0x4c): undefined reference to cv::createFisherFaceRecognizer(int, double)' FisherFaceRecognizer.o:FisherFaceRecognizer.cpp:(.text+0x101): undefined reference tocv::createFisherFaceRecognizer(int, double)' FisherFaceRecognizer.o:FisherFaceRecognizer.cpp:(.text+0x1b5): undefined reference to cv::createFisherFaceRecognizer(int, double)' C:/Program Files/mingw-w64/x86_64-4.9.2-posix-seh-rt_v3-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: FisherFaceRecognizer.o: bad reloc address 0x0 in section.pdata$_ZN7JNIEnv_9FindClassEPKc'

Then in the gcc build, I add -LC:$(OPENCV_HOME)/build/x64/vc12/lib -lopencv_contrib2411 -lopencv_core2411 -lopencv_highgui2411

C:/Program Files/mingw-w64/x86_64-4.9.2-posix-seh-rt_v3-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lopencv_contrib2411 C:/Program Files/mingw-w64/x86_64-4.9.2-posix-seh-rt_v3-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lopencv_core2411 C:/Program Files/mingw-w64/x86_64-4.9.2-posix-seh-rt_v3-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lopencv_highgui2411 collect2.exe: error: ld returned 1 exit status makefile:13: recipe for target 'facerec.dll' failed

Anyone can help to give some tips on this issue? Do we have some github project which is already implement this?

BTW, I use the download opencv package, I did not build with mingw again ( I use mingw build my JNI code), is there a need to rebuild all this all?

click to hide/show revision 2
No.2 Revision

updated 2015-08-24 02:02:42 -0600

berak gravatar image

Fail to build FaceRecognizer JNI package

I use the guide in this link http://stackoverflow.com/questions/22948553/issue-with-writing-facerecognizer-for-java-with-opencv-2-4-8, I use opencv2.4.11.

When I run the makefile, it reports the error with c code,and here is the error I got? What's wrong with the code I have? Do we have some sample code on OpenCV FaceRecognizer JNI part?

gcc -m64 -I"C:\Program Files\Java\jdk1.8.0_40\include" -I"C:\Program Files\Java\jdk1.8.0_40\include\win32"  -I"D:\Project\javacv\opencv\opencv\build\include" -I"D:\Project\javacv\opencv\opencv\build\java\x64" -c LBPHFaceRecognizer.c -o LBPHFaceRecognizer.o
LBPHFaceRecognizer.c: In function 'Java_com_test_LBPHFaceRecognizer_n_1createLBPHFaceRecognizer':
LBPHFaceRecognizer.c:6:1: error: parameter name omitted
 JNIEXPORT jlong JNICALL Java_com_test_LBPHFaceRecognizer_n_1createLBPHFaceRecognizer(JNIEnv , *, jclass){
 ^
LBPHFaceRecognizer.c:6:1: error: parameter name omitted
LBPHFaceRecognizer.c:7:1: error: 'try' undeclared (first use in this function)
 try {
 ^
LBPHFaceRecognizer.c:7:1: note: each undeclared identifier is reported only once for each function it appears in
LBPHFaceRecognizer.c:7:5: error: expected ';' before '{' token
 try {
     ^
makefile:18: recipe for target 'LBPHFaceRecognizer.o' failed
make: ** *** [LBPHFaceRecognizer.o] Error 1

1 12:16:19 Build Finished (took 1s.633ms)

1s.633ms)

For java code I have is, is,

package com.test;
import org.opencv.contrib.FaceRecognizer;

org.opencv.contrib.FaceRecognizer; public class LBPHFaceRecognizer extends FaceRecognizer {

{

    static {
     System.loadLibrary("opencv_java248");
     System.loadLibrary("facerec");
 }

 private static native long n_createLBPHFaceRecognizer();

 public LBPHFaceRecognizer() {
     super(n_createLBPHFaceRecognizer());
 }

 FaceRecognizer facerec = new LBPHFaceRecognizer();
}

}

The generate com_test_LBPHFaceRecognizer.h is

"/* DO NOT EDIT THIS FILE - it is machine generated /
*/
include <jni.h>
/ /* Header for class com_test_LBPHFaceRecognizer */

*/ ifndef _Included_com_test_LBPHFaceRecognizer define _Included_com_test_LBPHFaceRecognizer ifdef __cplusplus extern "C" { endif /* * Class: com_test_LBPHFaceRecognizer * Method: n_createLBPHFaceRecognizer * Signature: ()J */ JNIEXPORT jlong JNICALL Java_com_test_LBPHFaceRecognizer_n_1createLBPHFaceRecognizer (JNIEnv *, jclass);

jclass); ifdef __cplusplus } endif endif"

endif"

The LBPHFaceRecognizer.c I have is, is,

"// facerec.dll
include "jni.h"
include "opencv2/contrib/contrib.hpp"

"opencv2/contrib/contrib.hpp" JNIEXPORT jlong JNICALL Java_com_test_LBPHFaceRecognizer_n_1createLBPHFaceRecognizer(JNIEnv *, jclass); JNIEXPORT jlong JNICALL Java_com_test_LBPHFaceRecognizer_n_1createLBPHFaceRecognizer(JNIEnv *, jclass){ try {

{

        cv::Ptr<cv::FaceRecognizer> ptr = cv::createLBPHFaceRecognizer();
     cv::FaceRecognizer * pf = ptr.get();
     ptr.addref(); //don't let it self-destroy here..
     return (jlong) pf;
 } catch (...) {
     jclass je = env->FindClass("java/lang/Exception");
     env->ThrowNew(je, "Exception occurs to create LBPH face recognizer..");
 }
 return 0;
}“

}“

The makefile to build c code and dll file, file,

facerec.dll : LBPHFaceRecognizer.o
    gcc -m64 -Wl,--add-stdcall-alias -shared -o $@ $<

$< LBPHFaceRecognizer.o : LBPHFaceRecognizer.c LBPHFaceRecognizer.h gcc -m64 -I"$(JAVA_HOME)\include" -I"$(JAVA_HOME)\include\win32" -I"$(OPENCV_HOME)\build\include" -I"$(OPENCV_HOME)\build\java\x64" -c $< -o $@

$@

I also tried with cpp code, and the error I got is

FisherFaceRecognizer.o:FisherFaceRecognizer.cpp:(.text+0x4c): undefined reference to cv::createFisherFaceRecognizer(int, `cv::createFisherFaceRecognizer(int, double)'
FisherFaceRecognizer.o:FisherFaceRecognizer.cpp:(.text+0x101): undefined reference tocv::createFisherFaceRecognizer(int, to `cv::createFisherFaceRecognizer(int, double)'
FisherFaceRecognizer.o:FisherFaceRecognizer.cpp:(.text+0x1b5): undefined reference to cv::createFisherFaceRecognizer(int, `cv::createFisherFaceRecognizer(int, double)'
C:/Program Files/mingw-w64/x86_64-4.9.2-posix-seh-rt_v3-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: FisherFaceRecognizer.o: bad reloc address 0x0 in sectionsection `.pdata$_ZN7JNIEnv_9FindClassEPKc'
.pdata$_ZN7JNIEnv_9FindClassEPKc'

Then in the gcc build, I add

-LC:$(OPENCV_HOME)/build/x64/vc12/lib -lopencv_contrib2411 -lopencv_core2411 -lopencv_highgui2411

-lopencv_highgui2411 C:/Program Files/mingw-w64/x86_64-4.9.2-posix-seh-rt_v3-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lopencv_contrib2411 C:/Program Files/mingw-w64/x86_64-4.9.2-posix-seh-rt_v3-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lopencv_core2411 C:/Program Files/mingw-w64/x86_64-4.9.2-posix-seh-rt_v3-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lopencv_highgui2411 collect2.exe: error: ld returned 1 exit status makefile:13: recipe for target 'facerec.dll' failed

failed

Anyone can help to give some tips on this issue? Do we have some github project which is already implement this?

BTW, I use the download opencv package, I did not build with mingw again ( I use mingw build my JNI code), is there a need to rebuild all this all?

Fail to How to construct the parameters required by FaceRecognizer in Java

I build FaceRecognizer a JNI package

I use the guide in this link http://stackoverflow.com/questions/22948553/issue-with-writing-facerecognizer-for-java-with-opencv-2-4-8, I use opencv2.4.11.

When I run the makefile, to create LBPHFaceRecognizer, and I can get dll created as expected, but I met issue when call it reports the error with c code,and here is the error I got? What's wrong with during train,

Here are the code I have? Do we have some sample code on OpenCV FaceRecognizer JNI part?construct the parameters for train and the exception I got,

gcc -m64 -I"C:\Program Files\Java\jdk1.8.0_40\include" -I"C:\Program Files\Java\jdk1.8.0_40\include\win32"  -I"D:\Project\javacv\opencv\opencv\build\include" -I"D:\Project\javacv\opencv\opencv\build\java\x64" -c LBPHFaceRecognizer.c -o LBPHFaceRecognizer.o
LBPHFaceRecognizer.c: In function 'Java_com_test_LBPHFaceRecognizer_n_1createLBPHFaceRecognizer':
LBPHFaceRecognizer.c:6:1: error: parameter name omitted
 JNIEXPORT jlong JNICALL Java_com_test_LBPHFaceRecognizer_n_1createLBPHFaceRecognizer(JNIEnv *, jclass){
 ^
LBPHFaceRecognizer.c:6:1: error: parameter name omitted
LBPHFaceRecognizer.c:7:1: error: 'try' undeclared (first use in this function)
 try     List<Mat> images = new ArrayList<Mat>(imageFiles.length);
    Mat labels = new Mat(imageFiles.length, 1,  CvType.CV_32SC1);
    int counter = 0;
    for (File image : imageFiles) {
 ^
LBPHFaceRecognizer.c:7:1: note: each undeclared identifier is reported only once for each function it appears in
LBPHFaceRecognizer.c:7:5: error: expected ';' before '{' token
 try {
     ^
makefile:18: recipe for target 'LBPHFaceRecognizer.o' failed
make: *** [LBPHFaceRecognizer.o] Error 1

12:16:19 Build Finished (took 1s.633ms)

For java code I have is,

package com.test;
import org.opencv.contrib.FaceRecognizer;

public class LBPHFaceRecognizer extends FaceRecognizer {

    static {
        System.loadLibrary("opencv_java248");
        System.loadLibrary("facerec");
    }

    private static native long n_createLBPHFaceRecognizer();

    public LBPHFaceRecognizer() {
        super(n_createLBPHFaceRecognizer());
    }

    FaceRecognizer facerec = new LBPHFaceRecognizer();
       Mat img = Highgui.imread(image.getAbsolutePath());
        int label = Integer.parseInt(image.getName().split("\\-")[0]);
        images.add(counter,img);
        labels.put(counter, 1, label);
        counter++;
    }

The generate com_test_LBPHFaceRecognizer.h is OpenCV Error: Bad argument (The images are expected as InputArray::STD_VECTOR_MAT (a std::vector<mat>) or _InputArray::STD_VECTOR_VECTOR (a std::vector< vector<...> >).) in train, file D:\Project\javacv\opencv\opencv\sources\modules\contrib\src\facerec.cpp, line 913

Exception in thread "main" java.lang.Exception: unknown exception

"/* DO NOT EDIT THIS FILE - it is machine generated */
include <jni.h>
/* Header for class com_test_LBPHFaceRecognizer */

ifndef _Included_com_test_LBPHFaceRecognizer
define _Included_com_test_LBPHFaceRecognizer
ifdef __cplusplus
extern "C" {
endif
/*
 * Class:     com_test_LBPHFaceRecognizer
 * Method:    n_createLBPHFaceRecognizer
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL Java_com_test_LBPHFaceRecognizer_n_1createLBPHFaceRecognizer
  (JNIEnv *, jclass);

ifdef __cplusplus
}
endif
endif"
at org.opencv.contrib.FaceRecognizer.predict_0(Native Method)
at org.opencv.contrib.FaceRecognizer.predict(FaceRecognizer.java:173)
at OpenCV2411FaceRecognizer.main(OpenCV2411FaceRecognizer.java:149)

Can anyone help me on this?

And when I tried to load a trained result, I just get exception with no meaningful message, any tips on how to fix this? The LBPHFaceRecognizer.c I have is,exception is

Exception in thread "main" java.lang.Exception: unknown exception

"// facerec.dll
include "jni.h"
include "opencv2/contrib/contrib.hpp"

JNIEXPORT jlong JNICALL Java_com_test_LBPHFaceRecognizer_n_1createLBPHFaceRecognizer(JNIEnv *, jclass);
JNIEXPORT jlong JNICALL Java_com_test_LBPHFaceRecognizer_n_1createLBPHFaceRecognizer(JNIEnv *, jclass){
try {

        cv::Ptr<cv::FaceRecognizer> ptr = cv::createLBPHFaceRecognizer();
        cv::FaceRecognizer * pf = ptr.get();
        ptr.addref(); //don't let it self-destroy here..
        return (jlong) pf;
    } catch (...) {
        jclass je = env->FindClass("java/lang/Exception");
        env->ThrowNew(je, "Exception occurs to create LBPH face recognizer..");
    }
    return 0;
}“
at org.opencv.contrib.FaceRecognizer.load_0(Native Method)
at org.opencv.contrib.FaceRecognizer.load(FaceRecognizer.java:104)
at OpenCV2411FaceRecognizer.main(OpenCV2411FaceRecognizer.java:131)

The makefile to build c code and dll file,

facerec.dll : LBPHFaceRecognizer.o
    gcc -m64 -Wl,--add-stdcall-alias -shared -o $@ $<


LBPHFaceRecognizer.o : LBPHFaceRecognizer.c LBPHFaceRecognizer.h
    gcc -m64 -I"$(JAVA_HOME)\include" -I"$(JAVA_HOME)\include\win32"  -I"$(OPENCV_HOME)\build\include" -I"$(OPENCV_HOME)\build\java\x64" -c $< -o $@

I also tried with cpp code, and the error I got is

FisherFaceRecognizer.o:FisherFaceRecognizer.cpp:(.text+0x4c): undefined reference to `cv::createFisherFaceRecognizer(int, double)'
FisherFaceRecognizer.o:FisherFaceRecognizer.cpp:(.text+0x101): undefined reference to `cv::createFisherFaceRecognizer(int, double)'
FisherFaceRecognizer.o:FisherFaceRecognizer.cpp:(.text+0x1b5): undefined reference to `cv::createFisherFaceRecognizer(int, double)'
C:/Program Files/mingw-w64/x86_64-4.9.2-posix-seh-rt_v3-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: FisherFaceRecognizer.o: bad reloc address 0x0 in section `.pdata$_ZN7JNIEnv_9FindClassEPKc'

Then in the gcc build, I add

-LC:$(OPENCV_HOME)/build/x64/vc12/lib -lopencv_contrib2411 -lopencv_core2411 -lopencv_highgui2411

C:/Program Files/mingw-w64/x86_64-4.9.2-posix-seh-rt_v3-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lopencv_contrib2411
C:/Program Files/mingw-w64/x86_64-4.9.2-posix-seh-rt_v3-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lopencv_core2411
C:/Program Files/mingw-w64/x86_64-4.9.2-posix-seh-rt_v3-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lopencv_highgui2411
collect2.exe: error: ld returned 1 exit status
makefile:13: recipe for target 'facerec.dll' failed

Anyone can help to give some tips on this issue? Do we have some github project which is already implement this?

BTW, I use the download opencv package, I did not build with mingw again ( I use mingw build my JNI code), is there a need to rebuild all this all?