使用OpenCV的不稳定人脸识别

22

我正在开发一款用于人脸识别的安卓应用程序,使用的是 OpenCV 的非官方封装库 JavaCV。在导入了com.googlecode.javacv.cpp.opencv_contrib.FaceRecognizer之后,我使用并测试了以下已知方法:

  • 使用 createLBPHFaceRecognizer() 方法的 LBPH 方法
  • 使用 createFisherFaceRecognizer() 方法的 Fisher 面孔方法
  • 使用 createEigenFaceRecognizer() 方法的 Eigen 面孔方法

在识别检测到的面孔之前,我会校正旋转的面孔,并裁剪合适的区域,参考这个方法

通常情况下,当我在相机中捕捉到的面孔已经存在于数据库中时,识别结果是正确的。但这并不总是正确的。有时候,它会高概率地识别出未知的面孔(未在训练样本的数据库中找到)。当数据库中具有相似特征(胡须、小胡子、眼镜等)的两个或多个人脸时,它们之间的识别可能会极其错误!

为了预测使用测试面孔图像的结果,我应用以下代码:

public String predict(Mat m) {

        int n[] = new int[1];
        double p[] = new double[1];
        IplImage ipl = MatToIplImage(m,WIDTH, HEIGHT);

        faceRecognizer.predict(ipl, n, p);

        if (n[0]!=-1)
         mProb=(int)p[0];
        else
            mProb=-1;
            if (n[0] != -1)
            return labelsFile.get(n[0]);
        else
            return "Unkown";
    }

我无法控制概率p的阈值,因为:

  • 小的p〈50可以预测正确结果。
  • 高的p〉70可能会预测错误的结果。
  • 中等的p可预测正确或错误。

另外,我不理解为什么在使用LBPH的情况下,predict()函数有时会给出大于100的概率?而在使用Fisher和Eigen时,它会给出非常大的值(>2000)??

有人能帮忙找到这些奇怪问题的解决方案吗? 是否有任何建议来改善识别的稳健性?尤其是在两个不同人脸相似的情况下。

以下是使用Facerecognizer的整个类:

package org.opencv.javacv.facerecognition;

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.FileOutputStream;
import java.io.FilenameFilter;
import java.util.ArrayList;

import org.opencv.android.Utils;
import org.opencv.core.Mat;

import com.googlecode.javacv.cpp.opencv_imgproc;
import com.googlecode.javacv.cpp.opencv_contrib.FaceRecognizer;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_core.MatVector;

import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

public  class PersonRecognizer {

    public final static int MAXIMG = 100;
    FaceRecognizer faceRecognizer;
    String mPath;
    int count=0;
    labels labelsFile;

     static  final int WIDTH= 128;
     static  final int HEIGHT= 128;;
     private int mProb=999;


    PersonRecognizer(String path)
    {
      faceRecognizer =  com.googlecode.javacv.cpp.opencv_contrib.createLBPHFaceRecognizer(2,8,8,8,200);
     // path=Environment.getExternalStorageDirectory()+"/facerecog/faces/";
     mPath=path;
     labelsFile= new labels(mPath);


    }

    void changeRecognizer(int nRec)
    {
        switch(nRec) {
        case 0: faceRecognizer = com.googlecode.javacv.cpp.opencv_contrib.createLBPHFaceRecognizer(1,8,8,8,100);
                break;
        case 1: faceRecognizer = com.googlecode.javacv.cpp.opencv_contrib.createFisherFaceRecognizer();
                break;
        case 2: faceRecognizer = com.googlecode.javacv.cpp.opencv_contrib.createEigenFaceRecognizer();
                break;
        }
        train();

    }

    void add(Mat m, String description) {
        Bitmap bmp= Bitmap.createBitmap(m.width(), m.height(), Bitmap.Config.ARGB_8888);

        Utils.matToBitmap(m,bmp);
        bmp= Bitmap.createScaledBitmap(bmp, WIDTH, HEIGHT, false);

        FileOutputStream f;
        try {
            f = new FileOutputStream(mPath+description+"-"+count+".jpg",true);
            count++;
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, f);
            f.close();

        } catch (Exception e) {
            Log.e("error",e.getCause()+" "+e.getMessage());
            e.printStackTrace();

        }
    }

    public boolean train() {

        File root = new File(mPath);
        Log.i("mPath",mPath);
        FilenameFilter pngFilter = new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.toLowerCase().endsWith(".jpg");

        };
        };

        File[] imageFiles = root.listFiles(pngFilter);

        MatVector images = new MatVector(imageFiles.length);

        int[] labels = new int[imageFiles.length];

        int counter = 0;
        int label;

        IplImage img=null;
        IplImage grayImg;

        int i1=mPath.length();


        for (File image : imageFiles) {
            String p = image.getAbsolutePath();
            img = cvLoadImage(p);

            if (img==null)
                Log.e("Error","Error cVLoadImage");
            Log.i("image",p);

            int i2=p.lastIndexOf("-");
            int i3=p.lastIndexOf(".");
            int icount=Integer.parseInt(p.substring(i2+1,i3)); 
            if (count<icount) count++;

            String description=p.substring(i1,i2);

            if (labelsFile.get(description)<0)
                labelsFile.add(description, labelsFile.max()+1);

            label = labelsFile.get(description);

            grayImg = IplImage.create(img.width(), img.height(), IPL_DEPTH_8U, 1);

            cvCvtColor(img, grayImg, CV_BGR2GRAY);

            images.put(counter, grayImg);

            labels[counter] = label;

            counter++;
        }
        if (counter>0)
            if (labelsFile.max()>1)
                faceRecognizer.train(images, labels);
        labelsFile.Save();
    return true;
    }

    public boolean canPredict()
    {
        if (labelsFile.max()>1)
            return true;
        else
            return false;

    }

    public String predict(Mat m) {
        if (!canPredict())
            return "";
        int n[] = new int[1];
        double p[] = new double[1];
        IplImage ipl = MatToIplImage(m,WIDTH, HEIGHT);
//      IplImage ipl = MatToIplImage(m,-1, -1);

        faceRecognizer.predict(ipl, n, p);

        if (n[0]!=-1)
         mProb=(int)p[0];
        else
            mProb=-1;
    //  if ((n[0] != -1)&&(p[0]<95))
        if (n[0] != -1)
            return labelsFile.get(n[0]);
        else
            return "Unkown";
    }




      IplImage MatToIplImage(Mat m,int width,int heigth)
      {


           Bitmap bmp=Bitmap.createBitmap(m.width(), m.height(), Bitmap.Config.ARGB_8888);


           Utils.matToBitmap(m, bmp);
           return BitmapToIplImage(bmp,width, heigth);

      }

    IplImage BitmapToIplImage(Bitmap bmp, int width, int height) {

        if ((width != -1) || (height != -1)) {
            Bitmap bmp2 = Bitmap.createScaledBitmap(bmp, width, height, false);
            bmp = bmp2;
        }

        IplImage image = IplImage.create(bmp.getWidth(), bmp.getHeight(),
                IPL_DEPTH_8U, 4);

        bmp.copyPixelsToBuffer(image.getByteBuffer());

        IplImage grayImg = IplImage.create(image.width(), image.height(),
                IPL_DEPTH_8U, 1);

        cvCvtColor(image, grayImg, opencv_imgproc.CV_BGR2GRAY);

        return grayImg;
    }



    protected void SaveBmp(Bitmap bmp,String path)
      {
            FileOutputStream file;
            try {
                file = new FileOutputStream(path , true);

            bmp.compress(Bitmap.CompressFormat.JPEG,100,file);  
            file.close();
            }
            catch (Exception e) {
                // TODO Auto-generated catch block
                Log.e("",e.getMessage()+e.getCause());
                e.printStackTrace();
            }

      }


    public void load() {
        train();

    }

    public int getProb() {
        // TODO Auto-generated method stub
        return mProb;
    }


}

2
是的,您需要3个不同的阈值,每种方法一个,因为它们的特征空间不同。此外,您在预测中的“p”值不是概率,而是从测试图像到数据库中找到的最接近匹配项的距离(因此,有点相反),因此根本不在[0..100]范围内。 - berak
@berak 我们传递三个参数来预测Iplimage,int[],double[]。好的,我们传递了Iplimage,我们在double[]中得到了距离,但是int[]呢?它代表什么?因为我真的不太理解它,所以我在其中得到了不同的值,比如1、4、8。 - umerk44
不,这不是我的代码。 - umerk44
2
Dervish,你需要对你的数据进行测试。说实话,让代码运行起来是容易的部分。优化结果才是真正的工作开始的地方。 - berak
1
我同意Berak的观点,你需要一定数量来自同一个人的不同图像,以便模型可以进行训练。最终预测结果会更好。 - Spindizzy
显示剩余3条评论
1个回答

2

我认为你需要实现一些内容来更好地应对光照变化。请参考:OpenCV中的光照归一化

然后,为了管理图片之间的相似性,也许可以使用主成分分析等方法。


1
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - user3529407

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接