Opencv 改进人脸识别

Opencv 改进人脸识别,opencv,image-processing,javacv,Opencv,Image Processing,Javacv,我正在尝试在android上开发人脸识别应用程序。我正在使用JavaCvFaceRecognizer。但到目前为止,我的成绩很差。它可以识别经过训练的人的图像,但也可以识别未知图像。对于已知的人脸,它给了我很大的距离值,大部分时间是70-90,有时是90+,而未知的图像也会得到70-90 那么如何提高人脸识别的性能呢?有什么技巧?通常情况下,你能从中获得多少成功 我从未从事过图像处理工作。如果有任何指导方针,我将不胜感激 代码如下: public class PersonRecognizer

我正在尝试在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;
    }


    }
公共类人员识别器{
公共最终静态整数最大值=100;
人脸识别器;
字符串mPath;
整数计数=0;
标签标签文件;
静态最终整数宽度=70;
静态最终内部高度=70;
私有静态最终字符串标记=“PersonRecognizer”;
私有int mProb=999;
个人识别器(字符串路径)
{
faceRecognizer=com.googlecode.javacv.cpp.opencv_contrib.createLBPHFaceRecognizer(2,8,8,8100);
//path=Environment.getExternalStorageDirectory()+“/facerecog/faces/”;
mPath=路径;
labelsFile=新标签(mPath);
}
无效变更识别器(int nRec)
{
开关(nRec){
案例0:faceRecognizer=com.googlecode.javacv.cpp.opencv_contrib.createLBPHFaceRecognizer(1,8,8,8100);
打破
案例1:faceRecognizer=com.googlecode.javacv.cpp.opencv_contrib.createFisherFaceRecognizer();
打破
案例2:faceRecognizer=com.googlecode.javacv.cpp.opencv_contrib.createEigenFaceRecognizer();
打破
}
火车();
}
无效添加(材料m,字符串描述)
{
位图bmp=Bitmap.createBitmap(m.width(),m.height(),Bitmap.Config.ARGB_8888);
matToBitmap(m,bmp);
bmp=Bitmap.createScaledBitmap(bmp,宽度,高度,false);
文件输出流f;
尝试
{
f=新的FileOutputStream(mPath+description+“-”+count+“.jpg”,true);
计数++;
bmp.compress(Bitmap.CompressFormat.JPEG,100,f);
f、 close();
}捕获(例外e){
Log.e(“错误”,e.getCause()+“”+e.getMessage());
e、 printStackTrace();
}
}
公共布尔序列(){
文件根=新文件(mPath);
FilenameFilter pngFilter=新FilenameFilter(){
公共布尔接受(文件目录,字符串名称){
返回name.toLowerCase().endsWith(“.jpg”);
};
};
File[]imageFiles=root.listFiles(pngFilter);
MatVector images=新的MatVector(imageFiles.length);
int[]labels=新的int[imageFiles.length];
int计数器=0;
int标签;
IplImage img=null;
IplImage-grayImg;
int i1=mPath.length();
用于(文件图像:imageFiles){
字符串p=image.getAbsolutePath();
img=cvLoadImage(p);
如果(img==null)
Log.e(“错误”,“错误cVLoadImage”);
Log.i(“图像”,p);
int i2=p.lastIndexOf(“-”);
int i3=p.lastIndexOf(“.”);
int-icount=0;
尝试
{
icount=Integer.parseInt(p.substring(i2+1,i3));
}
捕获(例外情况除外)
{
例如printStackTrace();
}
if(count1)
人脸识别器。序列(图像、标签);
labelfile.Save();
返回true;
}
公共布尔值canPredict()
{
如果(labelfile.max()>1)
返回true;
其他的
返回false;
}
公共字符串预测(Mat m){
如果(!canPredict())
返回“”;
int n[]=新int[1];
双p[]=新的双p[1];
//把垫子换成黑色和白色
/*Mat gray_m=新Mat();
Imgproc.cvt颜色(m,灰色,Imgproc.COLOR\u RGBA2GRAY)*/
IplImage ipl=MatToIplImage(米、宽、高);
人脸识别器预测(ipl,n,p);
如果(n[0]!=-1)
{
mProb=(int)p[0];
Log.v(标记“Distance=“+mProb+”);
Log.v(标签,“N=”+N[0]);
}
其他的
{
mProb=-1;
Log.v(标签,“距离=”+mProb);
}
如果(n[0]!=-1)
{
返回labelsFile.get(n[0]);
}
其他的
{
返回“未知”;
}
}
IplImage MatToIplImage(材料m,内部宽度,内部高度)
{
位图bmp;
尝试
{
bmp=Bitmap.createBitmap(m.width(),m.height(),Bitmap.Config.RGB_565);
}
捕获(OutOfMemoryer错误)
{
bmp=Bitmap.createBitmap(m.width()/2,m.height()/2,Bitmap.Config.RGB_565);
printStackTrace();
}
matToBitmap(m,bmp);
返回位图到位图(bmp、宽度、高度);
}
IplImage位图到位图(位图bmp、整型宽度、整型高度){
如果((宽度!=-1)| |(高度!=-1)){
位图bmp2=Bitmap.createScaledBitmap(bmp,宽度,高度,false);
bmp=bmp2;
}
IplImage image=IplImage.create(bmp.getWidth(),bmp.getHeight(),
IPL_深度_8U,4);
copyPixelsToBuffer(image.getByteBuffer());
IplImage grayImg=IplImage.create(image.width(),image.height(),
IPL_深度_8U,1);
CVT颜色(图像、灰度、opencv_imgproc.CV_bgr2灰度);
返回灰度图;
}
受保护的void SaveBmp(位图bmp,字符串路径)
{
FileOutputStream文件;
尝试
{
file=newfileoutputstream(路径,true);
bmp.compress(Bitmap.CompressFormat.JPEG,100,文件);
file.close();
}
捕获(例外e){
//TODO自动生成的捕获块