首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >改进人脸识别

改进人脸识别
EN

Stack Overflow用户
提问于 2014-05-15 13:31:53
回答 1查看 1.1K关注 0票数 1

我正试图在android中开发一个人脸识别应用程序。我正在使用JavaCv FaceRecognizer。但到目前为止,我的成绩很差。它识别受过训练的人的形象,但也识别未知的图像。对于已知的面孔,它给了我很大的值作为一个距离,大部分时间从70-90,有时是90+,而未知的图像也得到70-90。

那么,如何提高人脸识别的性能呢?有什么技术?正常情况下,你能获得多大比例的成功?

我从来没有处理过图像处理。如有任何指引,我将不胜感激。

以下是代码:

代码语言:javascript
复制
 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;
    }


    }
EN

回答 1

Stack Overflow用户

发布于 2021-04-03 06:58:45

我最近也遇到了类似的挑战,以下是帮助我取得更好结果的因素:

  1. 从图像中裁剪人脸--这将在推理时删除不必要的像素。
  2. 调整裁剪后的脸部图像--当检测人脸标志时,这会产生影响,在测试集上尝试不同的尺度,以了解什么是最有效的。同时,这也影响了推理时间,减小了推理的大小,加快了推理速度。
  3. 提高了人脸图像的亮度----我发现这真的很有帮助,在较暗的图像中检测人脸地标不是很好,这主要是因为该模型是用大部分白色的面孔进行预训练的--对训练数据的理解将有助于处理偏见。
  4. 转换成灰度图像--我在很多论坛上都见过它,并说这将有助于高效地找到边缘--与彩色图像(3通道-RGB)相比,处理时间更短--然而,这并没有多大帮助。
  5. 尝试捕捉(注册)尽可能多的图像,为个人人在不同的角度,照明和其他变体-这是真正的帮助,因为它是比较编码存储的图像。
  6. 尝试为脸验证实现1-1比较--例如,在我的系统中,我为每个人捕获了10张图片,在验证时,我比较的是10张图片,而不是存储在系统中的所有人员的所有编码。这将提供,假阳性,然而用例在这个设置中是有限的,我使用它进行脸认证,并比较新面孔与现有面孔的移动号码是相同的。

到目前为止,我的理解是,人脸识别系统工作效率很高,但不是100%的精确性,我们需要理解模型的体系结构、训练数据和我们的需求,并相应地部署以获得更好的结果。以下是帮助我改进整个系统的一些要点

  1. 实现回退方法-向用户提供选项,当我们的系统无法正确地检测到它们时,例如,如果face身份验证由于某种原因而失败,则显示它们进入PIN选项。
  2. 在关键系统中--添加周期性的人工干预以确认系统结果--例如,如果系统不允许用户基于FR结果使用人工代理验证失败的结果,则允许用户进行验证。
  3. 为身份验证实现了多个因素--部署人脸识别系统作为现有系统的补充--例如,在用户登录凭据之后,使用人脸识别系统验证其目标人员。
  4. 设计您的用户界面时,在验证时,用户应该如何在不影响用户体验的情况下表现得像睁开眼睛、闭上嘴等。
  5. 在用户处理系统时,向用户提供清晰的指示,例如,让用户知道,FR系统集成了,他们需要在良好的照明条件下显示自己的脸等。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23679793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档