使用Opencv4Android SDK教程识别人脸?

时间:2014-09-14 06:24:28

标签: android opencv face-recognition

我是学生。最近我使用opencv构建了一个Face识别项目,但我不知道从哪里开始。

通过阅读opencv人脸检测样本,我使用OpenCv4Android成功构建了人脸检测。

现在我开始构建面部识别(使用LBPH算法)部分,我阅读Opencv文档并搜索谷歌我可以实际遵循的教程但我失败了(有很多教程使用javacv但我想改用OpenCv4Android):(

任何人都可以帮助我逐步完成教程,了解如何在OpenCV4Android SDK中使用face识别?非常感谢你。

其他:

  • 我在opencv / contrib
  • 中找到了FaceRecognizer.java类
  • 我在OpenCV4android文件夹中找到了facerec.java
  • 我在某地读过并尝试使用方法FaceRecognize model = createLBPHFaceRecognizer()--->但是找不到createLBPHFaceRecognizer()返回错误的方法。我在哪里可以找到并使用这种方法?

请帮助我接下来需要做什么?非常感谢!!!!!

1 个答案:

答案 0 :(得分:2)

在java包装器代码生成期间跳过了createFisherFaceRecognizer()方法(以及另外2个createXXXFaceRecognizer()),这是一个已知但尚未解决的问题。

最好的解决方案是使用jni / ndk实现它。你必须建立:

  • 带有本机c ++代码的.so文件,我们称之为facerec.so 额外的java包装类调用,FisherFaceRecognizer.java
遗憾的是,对ndk(这里没有这样的东西)帮不了多少,但它在桌面/ eclipse上运行得很好(dll / so会直接进入你的项目文件夹),所以这里是代码(相当一堵墙)它)。

// --- 8< --------- facerec.cpp -------------------------------

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

#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT jlong JNICALL Java_FisherFaceRecognizer_createFisherFaceRecognizer_10(JNIEnv* env, jclass);
JNIEXPORT jlong JNICALL Java_FisherFaceRecognizer_createFisherFaceRecognizer_10(JNIEnv* env, jclass) {
    try {
        cv::Ptr<cv::FaceRecognizer> pfr = cv::createFisherFaceRecognizer();
        pfr.addref(); // this is for the 2.4 branch, 3.0 would need a different treatment here
        return (jlong) pfr.obj;
    } catch (...) {
        jclass je = env->FindClass("java/lang/Exception");
        env->ThrowNew(je, "sorry, dave..");
    }
    return 0;
}

JNIEXPORT jlong JNICALL Java_FisherFaceRecognizer_createFisherFaceRecognizer_11(JNIEnv* env, jclass, jint num_components);
JNIEXPORT jlong JNICALL Java_FisherFaceRecognizer_createFisherFaceRecognizer_11(JNIEnv* env, jclass, jint num_components) {
    try {
        cv::Ptr<cv::FaceRecognizer> pfr = cv::createFisherFaceRecognizer(num_components);
        pfr.addref();
        return (jlong) pfr.obj;
    } catch (...) {
        jclass je = env->FindClass("java/lang/Exception");
        env->ThrowNew(je, "sorry, dave..");
    }
    return 0;
}

JNIEXPORT jlong JNICALL Java_FisherFaceRecognizer_createFisherFaceRecognizer_12(JNIEnv* env, jclass, jint num_components, jdouble threshold);
JNIEXPORT jlong JNICALL Java_FisherFaceRecognizer_createFisherFaceRecognizer_12(JNIEnv* env, jclass, jint num_components, jdouble threshold) {
    try {
        cv::Ptr<cv::FaceRecognizer> pfr = cv::createFisherFaceRecognizer(num_components,threshold);
        pfr.addref();
        return (jlong) pfr.obj;
    } catch (...) {
        jclass je = env->FindClass("java/lang/Exception");
        env->ThrowNew(je, "sorry, dave..");
    }
    return 0;
}

#ifdef __cplusplus
}
#endif

// --- 8&lt; --------- FisherFaceRecognizer.java -----------------

import org.opencv.contrib.FaceRecognizer;
import org.opencv.core.Core;

public class FisherFaceRecognizer extends FaceRecognizer {

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

    private static native long createFisherFaceRecognizer_0();
    private static native long createFisherFaceRecognizer_1(int num_components);
    private static native long createFisherFaceRecognizer_2(int num_components, double threshold);

    public FisherFaceRecognizer () {
        super(createFisherFaceRecognizer_0());
    }
    public FisherFaceRecognizer (int num_components) {
        super(createFisherFaceRecognizer_1(num_components));
    }
    public FisherFaceRecognizer (int num_components, double threshold) {
        super(createFisherFaceRecognizer_2(num_components, threshold));
    }
}

一旦你完成所有这些(恭喜!!),你会这样称呼它:

FaceRecognizer  facerec = new FisherFaceRecognizer();
   facerec.load("/sdcard/smile.yml"); // note, that it can't read from apk or zip, so you need to copy it somewhere

   Mat img = ...//test face
   int [] label = new int[1];
   double [] conf = new double[1];
   facerec.predict(img, label, conf);