bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more
Other
7.53k stars 1.58k forks source link

Improving accurary of face recognition #1110

Open OparaAL opened 5 years ago

OparaAL commented 5 years ago

I'm new in javacv. I'm trying to create a system for face recognition and faced to a problem of low accurary of recognition. I use EigenFaceRecognizer for identification and Georgia Tech Face Database as a database for photos. All my photos are with 640x480 resolution(I have 12 photos per person and 20 people in total).

Result is very unstable and it doesn't work sometimes. How can I increase level of recognition accuracy? Should I do any extra editting for photos or the problem is in my code?

Train method:

public void train() {
        File imagesDir = new File("C:\\Java_Eclipse\\FaceRecognitionWebcam\\src\\image");
        FilenameFilter imgFilter = new FilenameFilter() {
            public boolean accept(File dir, String name) {
                name = name.toLowerCase();
                return name.endsWith(".jpg") || name.endsWith(".pgm") || name.endsWith(".png");
            }
        };
        File[] imageFiles = imagesDir.listFiles(imgFilter);
        MatVector images = new MatVector(imageFiles.length);
        Mat labels = new Mat(imageFiles.length, 1, CV_32SC1);
        IntBuffer labelsBuf = labels.createBuffer();

        int counter = 0;

        for (File image : imageFiles) {
            Mat img = imread(image.getAbsolutePath(), CV_LOAD_IMAGE_GRAYSCALE);
            int label = Integer.parseInt(image.getName().split("\\-")[0]);
            images.put(counter, img);
            labelsBuf.put(counter, label);
            counter++;
        }

        // FaceRecognizer faceRecognizer = FisherFaceRecognizer.create();
         FaceRecognizer faceRecognizer = EigenFaceRecognizer.create();
        // FaceRecognizer faceRecognizer = LBPHFaceRecognizer.create();
        System.out.println("Train started");
        faceRecognizer.train(images, labels);
        faceRecognizer.save("C:\\Java_Eclipse\\FaceRecognitionWebcam\\src\\train_result_eigen.xml");
        System.out.println("Train completed");

    }

Recognition method:

public void findFaces(IplImage currentFrame) {
        IntPointer labels = new IntPointer(1);
        DoublePointer confidences = new DoublePointer(1);

        opencv_core.CvMemStorage storage = new opencv_core.CvMemStorage().create();
        opencv_core.CvSeq faces =
                cvHaarDetectObjects(currentFrame, classifierFace, storage, 1.6, 8, opencv_objdetect.CV_HAAR_DO_CANNY_PRUNING);
        int total = faces.total();

        if(total > 0) {
            System.out.println("Total faces: " + total);
            for(int i = 0; i < total; i++) {
                opencv_core.CvRect r = new opencv_core.CvRect(cvGetSeqElem(faces, i));
                int x = r.x(); int y = r.y(); int w = r.width(); int h = r.height();
                IplImage greyImg = IplImage.create(currentFrame.width(), currentFrame.height(), IPL_DEPTH_8U, 1);
                IplImage smooth = IplImage.create(currentFrame.width(), currentFrame.height(), IPL_DEPTH_8U, 1);
                IplImage equalize = IplImage.create(currentFrame.width(), currentFrame.height(), IPL_DEPTH_8U, 1);
                cvCvtColor(currentFrame, greyImg, CV_RGB2GRAY);//resize
                cvSmooth(greyImg, smooth);
                cvEqualizeHist(smooth, equalize);
                rectangle(cvarrToMat(currentFrame), new Rect(x, y, w, h), new Scalar(0, 255, 0, 0), 2, 0, 0);
                faceRecognizer.predict(cvarrToMat(equalize), labels, confidences);
                int label = labels.get(0);
                double confidence = confidences.get(0);
                String labelInfo = faceRecognizer.getLabelInfo(label).toString();
                System.out.println("---------");
                System.out.println("Person: " + i);
                System.out.println("label = " + label);
                System.out.println("confidence = " + confidence);

            }
        }
    }
saudet commented 5 years ago

There's a good tutorial for that here: https://docs.opencv.org/4.0.0/da/d60/tutorial_face_main.html Try to start with that.