gpt4 book ai didi

android - 人脸识别使用Opencv4Android SDK教程?

转载 作者:行者123 更新时间:2023-12-02 17:13:08 24 4
gpt4 key购买 nike

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

通过阅读 opencv 人脸检测示例,我成功地使用 OpenCv4Android 构建了我的人脸检测。

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

任何人都可以帮助我一步一步地指导我应该如何在 OpenCV4Android SDK 中使用人脸识别?非常感谢你。

额外的:

  • 我在 opencv/contrib
  • 中发现了 FaceRecognizer.java 类
  • 我在 OpenCV4android 文件夹中找到 facerec.java
  • 我在某处阅读并尝试了方法 FaceRecognize model = createLBPHFaceRecognizer() ---> 但方法 createLBPHFaceRecognizer() 未找到返回错误。我在哪里可以找到和使用这个方法?

  • 请帮我下一步我需要做什么?非常感谢!!!!!

    最佳答案

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

    最好的解决方案可能是使用 jni/ndk 来实现它。您将必须构建:

  • 一个带有 native 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< --------- 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);

    关于android - 人脸识别使用Opencv4Android SDK教程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25830660/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com