我正在尝试在Android上开发人脸识别应用程序,并且由于我不想在该项目上使用NDK(根本没有时间进行切换),因此我坚持使用Java开发整个应用程序,因此我遇到一些问题:

  • 似乎Contrib模块未包含在OpenCV 2.4.2中。无论如何在项目中使用它?
  • 我尝试使用JavaCV来使用Contrib模块的“FaceRecognizer”类。有两个可用的类,分别称为“FaceRecognizer”和“FaceRecognizerPtr”。有人知道这两者之间有什么区别吗?
  • 上面提到的类具有一种称为“Train”的方法,该方法(在C++中)接收两个类型为“Mat&Integer”(model->train(images,labels) & train(Vector<mat> theImages, Vector<int> theLabels)的Vector。我尝试在Java中将它们传递给ArrayList<mat> & ArrayList<integer>和Vector,但似乎该方法明确接受了“CvArr”数据类型,我不确定该如何获取...这是错误消息:



  • 有人知道如何将我的ArrayList更改为CvArr吗?

    这是我的第一篇文章,我不确定是要在一个帖子中还是在三个帖子中问所有三个问题,因此给您带来的不便深表歉意...如果您需要有关该项目的任何其他信息,请随时提问。

    最佳答案

    更新资料

    以下文章是由Petter Christian Bjelland撰写的,因此,所有功劳都是他的。我将其发布在这里,因为他的博客目前似乎处于维护模式,但我认为值得分享。

    使用JavaCV进行人脸识别(来自http://pcbje.com)

    我找不到任何有关如何使用OpenCV和Java进行面部识别的教程,因此我决定在这里分享一个可行的解决方案。由于每次运行都会建立训练模型,因此该解决方案的当前形式效率很低,但是它说明了使该模型起作用的必要条件。

    下面的类有两个参数:包含训练脸的目录的路径和要分类的图像的路径。并非所有图像都必须具有相同的尺寸,并且面部必须已经从原始图像中裁剪出来(如果您尚未进行面部检测,请在此处查看)。

    为了简化本文,该类还要求训练图像具有文件名格式:<label>-rest_of_filename.png。例如:

    1-jon_doe_1.png
    1-jon_doe_2.png
    2-jane_doe_1.png
    2-jane_doe_2.png
    

    ... and so on.

    The code:

    import com.googlecode.javacv.cpp.opencv_core;
    import static com.googlecode.javacv.cpp.opencv_highgui.*;
    import static com.googlecode.javacv.cpp.opencv_core.*;
    import static com.googlecode.javacv.cpp.opencv_imgproc.*;
    import static com.googlecode.javacv.cpp.opencv_contrib.*;
    import java.io.File;
    import java.io.FilenameFilter;
    
    public class OpenCVFaceRecognizer {
      public static void main(String[] args) {
        String trainingDir = args[0];
        IplImage testImage = cvLoadImage(args[1]);
    
        File root = new File(trainingDir);
    
        FilenameFilter pngFilter = new FilenameFilter() {
          public boolean accept(File dir, String name) {
            return name.toLowerCase().endsWith(".png");
          }
        };
    
        File[] imageFiles = root.listFiles(pngFilter);
    
        MatVector images = new MatVector(imageFiles.length);
    
        int[] labels = new int[imageFiles.length];
    
        int counter = 0;
        int label;
    
        IplImage img;
        IplImage grayImg;
    
        for (File image : imageFiles) {
          // Get image and label:
          img = cvLoadImage(image.getAbsolutePath());
          label = Integer.parseInt(image.getName().split("\\-")[0]);
          // Convert image to grayscale:
          grayImg = IplImage.create(img.width(), img.height(), IPL_DEPTH_8U, 1);
          cvCvtColor(img, grayImg, CV_BGR2GRAY);
          // Append it in the image list:
          images.put(counter, grayImg);
          // And in the labels list:
          labels[counter] = label;
          // Increase counter for next image:
          counter++;
        }
    
        FaceRecognizer faceRecognizer = createFisherFaceRecognizer();
        // FaceRecognizer faceRecognizer = createEigenFaceRecognizer();
        // FaceRecognizer faceRecognizer = createLBPHFaceRecognizer()
    
        faceRecognizer.train(images, labels);
    
        // Load the test image:
        IplImage greyTestImage = IplImage.create(testImage.width(), testImage.height(), IPL_DEPTH_8U, 1);
        cvCvtColor(testImage, greyTestImage, CV_BGR2GRAY);
    
        // And get a prediction:
        int predictedLabel = faceRecognizer.predict(greyTestImage);
        System.out.println("Predicted label: " + predictedLabel);
      }
    }
    

    该类需要OpenCV Java接口(interface)。如果您使用的是Maven,则可以使用以下pom.xml检索所需的库:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.pcbje</groupId>
      <artifactId>opencvfacerecognizer</artifactId>
      <version>0.1-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>opencvfacerecognizer</name>
      <url>http://pcbje.com</url>
    
      <dependencies>
        <dependency>
          <groupId>com.googlecode.javacv</groupId>
          <artifactId>javacv</artifactId>
          <version>0.3</version>
        </dependency>
    
        <!-- For Linux x64 environments -->
        <dependency>
          <groupId>com.googlecode.javacv</groupId>
          <artifactId>javacv</artifactId>
          <classifier>linux-x86_64</classifier>
          <version>0.3</version>
        </dependency>
    
        <!-- For OSX environments -->
        <dependency>
          <groupId>com.googlecode.javacv</groupId>
          <artifactId>javacv</artifactId>
          <classifier>macosx-x86_64</classifier>
          <version>0.3</version>
        </dependency>
      </dependencies>
    
      <repositories>
        <repository>
          <id>javacv</id>
          <name>JavaCV</name>
          <url>http://maven2.javacv.googlecode.com/git/</url>
        </repository>
      </repositories>
    </project>
    

    原始帖子

    引用我对http://answers.opencv.org/question/865/the-contrib-module-problem的答复。

    无需使用javacv,让我们看看仅看接口(interface)就能走多远!该项目位于googlecode上,可以轻松浏览以下代码:http://code.google.com/p/javacv

    首先看一下如何包装cv::FaceRecognizer(opencv_contrib.java, line 845 at time of writing this):
    @Namespace("cv") public static class FaceRecognizer extends Algorithm {
        static { Loader.load(); }
        public FaceRecognizer() { }
        public FaceRecognizer(Pointer p) { super(p); }
    
        public /*abstract*/ native void train(@ByRef MatVector src, @Adapter("ArrayAdapter") CvArr labels);
        public /*abstract*/ native int predict(@Adapter("ArrayAdapter") CvArr src);
        public /*abstract*/ native void predict(@Adapter("ArrayAdapter") CvArr src, @ByRef int[] label, @ByRef double[] dist);
        public native void save(String filename);
        public native void load(String filename);
        public native void save(@Adapter("FileStorageAdapter") CvFileStorage fs);
        public native void load(@Adapter("FileStorageAdapter") CvFileStorage fs);
    }
    

    啊哈,所以您需要为图像传递MatVector!您可以以CvArr(一行或一列)传递标签。 MatVectoropencv_core, line 4629 (at time of writing this)中定义,看起来像这样:
    public static class MatVector extends Pointer {
        static { load(); }
        public MatVector()       { allocate();  }
        public MatVector(long n) { allocate(n); }
        public MatVector(Pointer p) { super(p); }
        private native void allocate();
        private native void allocate(@Cast("size_t") long n);
    
        public native long size();
        public native void resize(@Cast("size_t") long n);
    
        @Index @ValueGetter public native @Adapter("MatAdapter") CvMat getCvMat(@Cast("size_t") long i);
        @Index @ValueGetter public native @Adapter("MatAdapter") CvMatND getCvMatND(@Cast("size_t") long i);
        @Index @ValueGetter public native @Adapter("MatAdapter") IplImage getIplImage(@Cast("size_t") long i);
        @Index @ValueSetter public native MatVector put(@Cast("size_t") long i, @Adapter("MatAdapter") CvArr value);
    }
    

    同样,仅通过查看代码,我想它可以像这样使用:
    int numberOfImages = 10;
    // Allocate some memory:
    MatVector images = new MatVector(numberOfImages);
    // Then fill the MatVector, you probably want to do something useful instead:
    for(int idx = 0; idx < numberOfImages; idx++){
       // Load an image:
       CvArr image = cvLoadImage("/path/to/your/image");
       // And put it into the MatVector:
       images.put(idx, image);
    }
    

    您可能想编写一个方法,该方法可以完成从Java ArrayListMatVector的转换(如果javacv中尚不存在这样的函数)。

    现在是您的第二个问题。 FaceRecognizercv::FaceRecognizer等效。本机OpenCV C++类返回cv::Ptr<cv::FaceRecognizer>,它是cv::FaceRecognizer的(智能)指针。这也必须包装。在这里看到图案吗?
    FaceRecognizerPtr的界面现在如下所示:
    @Name("cv::Ptr<cv::FaceRecognizer>")
    public static class FaceRecognizerPtr extends Pointer {
        static { load(); }
        public FaceRecognizerPtr()       { allocate();  }
        public FaceRecognizerPtr(Pointer p) { super(p); }
        private native void allocate();
    
        public native FaceRecognizer get();
        public native FaceRecognizerPtr put(FaceRecognizer value);
    }
    

    因此,您可以从此类中获取FaceRecognizer或将FaceRecognizer放入其中。您只需要关心get(),因为Pointer由创建具体FaceRecognizer算法的方法填充:
    @Namespace("cv") public static native @ByVal FaceRecognizerPtr createEigenFaceRecognizer(int num_components/*=0*/, double threshold/*=DBL_MAX*/);
    @Namespace("cv") public static native @ByVal FaceRecognizerPtr createFisherFaceRecognizer(int num_components/*=0*/, double threshold/*=DBL_MAX*/);
    @Namespace("cv") public static native @ByVal FaceRecognizerPtr createLBPHFaceRecognizer(int radius/*=1*/,
            int neighbors/*=8*/, int grid_x/*=8*/, int grid_y/*=8*/, double threshold/*=DBL_MAX*/);
    

    因此,一旦获得FaceRecognizerPtr,您就可以执行以下操作:
    // Holds your training data and labels:
    MatVector images;
    CvArr labels;
    // Do something with the images and labels... Probably fill them?
    // ...
    // Then get a Pointer to a FaceRecognizer (FaceRecognizerPtr).
    // Java doesn't have default parameters, so you have to add some yourself,
    // if you pass 0 as num_components to the EigenFaceRecognizer, the number of
    // components is determined by the data, for the threshold use the maximum possible
    // value if you don't want one. I don't know the constant in Java:
    FaceRecognizerPtr model = createEigenFaceRecognizer(0, 10000);
    // Then train it. See how I call get(), to get the FaceRecognizer inside the FaceRecognizerPtr:
    model.get().train(images, labels);
    

    这将为您学习一个Eigenfaces模型。就是这样!

    08-27 02:44