改善面部识别

时间:2014-05-15 13:31:53

标签: opencv image-processing javacv

我正在尝试在Android中开发面部识别应用程序。我正在使用JavaCv FaceRecognizer。但到目前为止,我的结果非常糟糕。它识别被训练的人的图像,但也识别未知的图像。对于已知的面部,它给我很大的距离值,大部分时间是70-90,有时是90+,而未知图像也是70-90。

那么我怎样才能提高人脸识别的表现呢?有什么技术?通常情况下你能获得多大比例的成功?

我从未使用图像处理。我会感谢任何指导方针。

以下是代码:

 public  class PersonRecognizer {

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

     static  final int WIDTH= 70;
     static  final int HEIGHT= 70;
     private static final String TAG = "PersonRecognizer";
     private int mProb=999;


    PersonRecognizer(String path)
    {
      faceRecognizer =  com.googlecode.javacv.cpp.opencv_contrib.createLBPHFaceRecognizer(2,8,8,8,100);
     // 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);

        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 = 0;
            try
            {
               icount=Integer.parseInt(p.substring(i2+1,i3)); 
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
            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];
        //conver Mat to black and white
        /*Mat gray_m = new Mat();
        Imgproc.cvtColor(m, gray_m, Imgproc.COLOR_RGBA2GRAY);*/
        IplImage ipl = MatToIplImage(m, WIDTH, HEIGHT);

        faceRecognizer.predict(ipl, n, p);

        if (n[0]!=-1)
        {
         mProb=(int)p[0];
         Log.v(TAG, "Distance = "+mProb+"");
         Log.v(TAG, "N = "+n[0]);
        }
        else
        {
            mProb=-1;
            Log.v(TAG, "Distance = "+mProb);
        }



        if (n[0] != -1)
        {
            return labelsFile.get(n[0]);
        }
        else
        {
            return "Unknown";
        }
    }




      IplImage MatToIplImage(Mat m,int width,int heigth)
      {
          Bitmap bmp;

         try
         {
           bmp = Bitmap.createBitmap(m.width(), m.height(), Bitmap.Config.RGB_565);
         }
         catch(OutOfMemoryError er)
         {
             bmp = Bitmap.createBitmap(m.width()/2, m.height()/2, Bitmap.Config.RGB_565);
             er.printStackTrace();

         }

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


    }

1 个答案:

答案 0 :(得分:1)

我最近遇到了类似的挑战,以下是帮助我取得更好成绩的事情:

  1. 从图像中裁剪人脸 - 这将在推理时删除不必要的像素
  2. 调整裁剪后的人脸图像的大小 - 这会在检测人脸标志时产生影响,请在测试集上尝试不同的比例以了解什么最有效。此外,这也会影响推理时间,尺寸越小,推理速度越快。
  3. 提高人脸图像的亮度 - 我发现这真的很有帮助,在较暗的图像中检测人脸地标不是很好,这主要是由于模型,它主要是用预训练的白脸 - 了解训练数据将有助于处理偏见。
  4. 转换为灰度图像 - 我在很多论坛上都看到过,我说过,这将有助于有效地找到边缘 - 与彩色图像相比,处理时间更短(3 个通道 - RGB) - 然而,这并没有多大帮助。
  5. 尝试捕捉(注册)尽可能多的个人在不同角度、灯光和其他变化下的图像 - 这确实很有帮助,因为它与存储图像的编码进行了比较。
  6. 尝试进行人脸验证的1-1比对——例如,在我的系统中,我为每个人拍摄了10张图片,在验证时,我正在与10张图片进行比较,而不是存储在系统中的所有人员的所有编码。这将提供误报,但此设置中的用例受到限制,我将其用于人脸身份验证,并将新人脸与手机号码相同的现有人脸进行比较。

我今天的理解是,人脸识别系统运行良好,但不是 100% 准确,我们必须了解模型架构、训练数据和我们的要求,并相应地进行部署以获得更好的结果。以下是帮助我改进整个系统的一些要点:

  1. 实施回退方法 - 向用户提供选项,当我们的系统未能正确检测到他们时,例如,如果由于某种原因人脸身份验证失败,则显示他们输入 PIN 选项
  2. 在关键系统中 - 添加定期人工干预以确认系统结果 - 例如,如果系统不允许基于 FR 结果的用户 - 与人工代理验证失败的结果并允许用户
  3. 实施多种身份验证因素 - 将人脸识别系统部署为现有系统的补充 - 例如,在用户使用凭据登录后 - 使用人脸识别系统验证其目标人员
  4. 以某种方式设计您的用户界面,在验证时,用户应该如何在不影响用户体验的情况下表现出睁眼、闭嘴等行为
  5. 向用户提供明确的指示,当他们与系统打交道时——例如,让用户知道,集成了FR系统,他们需要在良好的照明条件下展示他们的脸等。立>
相关问题