使用OpenCV 2.4.8编写Java FaceRecognizer的问题 [英] Issue with writing FaceRecognizer for Java with OpenCV 2.4.8

查看:98
本文介绍了使用OpenCV 2.4.8编写Java FaceRecognizer的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个基于JavaCV编写的,基于面部识别的项目,该项目有待改进.这个想法是要么使用C ++重写程序,要么编写JNI绑定以仍然使用Java.我做了一些研究,根据2.4.4版的官方网站OpenCV,它具有Java和Python的绑定.因此,由于官方网站已声明,因此我决定继续使用它.请注意,我以前没有使用C ++编程,过去也没有编写任何JNI包装器.如果官方网站未声明它包含Java绑定,我将使用Qt Digia用C ++编写.

I have been given a project based around face recognition written in JavaCV to be improved. The idea was to either rewrite the program using C++ or write JNI bindings to still use Java. I did some research and according to the official website OpenCV as of version 2.4.4 has bindings for Java and Python. And so, since the official website stated so I decided to go with it. Please note I haven't programmed in C++ before nor have I written any JNI wrappers in the past. If the official website didn't state that it includes bindings for Java I would just write it with C++ using Qt Digia.

通过这种方式或其他方式,我做了我想对Java项目做的所有其他事情,剩下人脸识别部分.不幸的是,根据(相关问题#1

This way or another I did everything else that I wanted to do with the Java project and was left with the face recognition part. Unfortunately according (Relevant question #1, Relevant question #2) I found out that there is a bug with FaceRecognizer and that JNI wrapper for face recognition has to be written by hand.

我发现了一个相当不错的 jni c ++ Java教程,我试图使用它和上面相关问题#1中提到的代码.下面的屏幕快照显示了我现在所掌握的内容.
代码如下: LBPHFaceRecognizer.java

I found a pretty good jni c++ java tutorial and I tried to use that with the code mentioned in Relevant question #1 linked above. The screenshot below shows what I have got right now.
The code is as follows: LBPHFaceRecognizer.java

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

LBPHFaceRecognizer.c

LBPHFaceRecognizer.c

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


extern "C" {


JNIEXPORT jlong JNICALL Java_org_matxx_n_createLBPHFaceRecognizer(JNIEnv* env, jclass, jint);

JNIEXPORT jlong JNICALL Java_org_matxx_n_createLBPHFaceRecognizer(JNIEnv* env, jclass, jint)
{
    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, "sorry, dave..");
}
    return 0;
    }
} // extern "C"

makefile

# Define a variable for classpath
CLASS_PATH = ../bin

# Define a virtual path for .class in the bin directory
vpath %.class $(CLASS_PATH)

all : facerec.dll

# $@ matches the target, $< matches the first dependancy
facerec.dll : LBPHFaceRecognizer.o
gcc -m64 -Wl,--add-stdcall-alias -shared -o $@ $<

# $@ matches the target, $< matches the first dependancy
LBPHFaceRecognizer.o : LBPHFaceRecognizer.c LBPHFaceRecognizer.h
gcc -m64 -I"C:\Program Files\Java\jdk1.7.0_51\include" -I"C:\Program Files\Java\jdk1.7.0_51\include\win32"  -I"C:\Users\User\Desktop\OPENCVINSTALLATION\opencv" -I"C:\Users\User\Desktop\OPENCVINSTALLATION\opencv\build\java\x64" -c $< -o $@

# $* matches the target filename without the extension
LBPHFaceRecognizer.h : LBPHFaceRecognizer.class
javah -classpath $(CLASS_PATH) $*

clean :
rm LBPHFaceRecognizer.h LBPHFaceRecognizer.o facerec.dll

总而言之,我设法创建了facerec.dll,但是存在许多问题.首先,当我创建facerec.dll时,LBPHFacerecognizer.java在顶部没有导入,因为它在抱怨它.我后来添加它只是为了不看到错误. 其次,.c代码抱怨所有内容,如下面的屏幕快照所示.

All in all I managed to create facerec.dll, but there are a number of problems. First when I was creating the facerec.dll the LBPHFacerecognizer.java did not have the import at the top, because it was complaining about it. I added it afterwards just not to see the error. Secondly the .c code complains about everything as seen on the screenshot below.

有趣的是,它并没有抱怨jni.h导入,而下面的屏幕快照中显示的头文件却没有.

and interestingly enough it doesn't complain about the jni.h import whereas the header file as seen on screenshot below does.

就是这样,有人可以看看它,然后告诉我是否正确完成了吗?有什么我应该改变的吗?考虑到已经创建了dll的事实,这些错误可以吗?或者,也许有人过去曾经这样做过,并且愿意共享dll文件.我还没有用与JavaCV代码等效的Java进行测试.

That's it, can someone have a look at it and tell me if I've done it correctly? Is there something I should change? Are those errors ok considering the fact the the dll has been created? Or maybe someone has done it in the past and is willing to share the dll file. I haven't gotten around to testing it with the java equivalent of JavaCV code yet.

我使用OpenCV 2.4.8,Win 7 64位,Mingw 64位,Java 64位7_0_51版本,Eclipse 64位.

I use OpenCV 2.4.8, Win 7 64bit, Mingw 64 bit, Java 64 bit 7_0_51 release, Eclipse 64 bit.

先谢谢了.

推荐答案

  1. #include "jni.h"#include <jni.h>不同,这就是为什么它抱怨一个而不是另一个..引用表示相对路径.换句话说,该文件与您的文件位于同一文件夹中项目.或子文件夹.否则,您将使用<...>,并且该文件将文件包含在编译器的搜索目录中.

  1. #include "jni.h" isn't the same as #include <jni.h> That's why it complains about one and not the other.. Quotations means relative path.. In other words, the file is located in the same folder as your project. Or a sub-folder. Otherwise you use <...> and it includes files in the compiler's search directory.

签名采用参数:JNIEnv*, jclass, jint,应采用以下参数:采用JNIEnv*, jclass.为什么?因为根据签名,您在Java端private static native long n_createLBPHFaceRecognizer(); ..没有任何参数..但是,您在本机端将其声明为采用int.

The signature is taking parameters: JNIEnv*, jclass, jint when it should be: taking JNIEnv*, jclass. Why? Because according to the signature you have on the Java side private static native long n_createLBPHFaceRecognizer();.. it takes no parameters.. However, you have it declared on the native side as taking an int.

根据此签名名称:Java_org_matxx_n_createLBPHFaceRecognizern_createLBPHFaceRecognizer在名为Java_org_matxxx的程序包中,但是您的Java代码似乎不在默认"程序包之外的任何程序包中.

According to this signature name: Java_org_matxx_n_createLBPHFaceRecognizer, n_createLBPHFaceRecognizer is in a package called Java_org_matxxx but your java code doesn't seem to be in any package other than "default" package.

如果jni函数在Java方面的名称中带有下划线,则必须在本机方面将其下划线替换为_1.示例:

If a jni function has an underscore in its name on the Java side, you must replace the underscore with an _1 on the native side.. Example:

Java端(在com.foo.bar包中):n_createLBPHFaceRecognizer

Java side (in package com.foo.bar): n_createLBPHFaceRecognizer

母语:Java_com_foo_bar_n_1createLBPHFaceRecognizer

作为一个简单的解决方法,明智的做法是名称中不要包含下划线.

As a simple work around, it is wise to just not have underscores in the name..

这篇关于使用OpenCV 2.4.8编写Java FaceRecognizer的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆