2

I have this problem, my code doesn't work when i try to find eyes with glasses with openCv library for android.
My code is the following:

         try {
            InputStream is = this.getResources().openRawResource(R.raw.haarcascade_eye_tree_eyeglasses);
            File cascadeDir = this.getDir("cascade"+"occhiOcchiali", Context.MODE_PRIVATE);
            File cascadeFile = new File(cascadeDir, "haarcascade_eye_tree_eyeglasses.xml");
            FileOutputStream os = new FileOutputStream(cascadeFile);
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = is.read(buffer)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            is.close();
            os.close();
            mioClassificatoreOcchiOcchiali = new CascadeClassifier(cascadeFile.getAbsolutePath());
            Log.d("metodo1", cascadeFile.getAbsolutePath());
            if (mioClassificatoreOcchiOcchiali.empty()) {
                Log.d("metodo1", "Failed to load cascade classifier");
                mioClassificatoreOcchiOcchiali = null;
            } else
                Log.d("metodo1", "Loaded cascade classifier from " + cascadeFile.getAbsolutePath());

            cascadeFile.delete();
            cascadeDir.delete();

        } catch (IOException e) {
            e.printStackTrace();
            Log.d("metodo1" , "Failed to load cascade. Exception thrown: " + e);
        }

In this way I take my Classifier and with this code i try to find eyes with glasses:

LinkedList<org.opencv.core.Rect> occhi = new LinkedList<org.opencv.core.Rect>();
                    org.opencv.core.Size sOcchi = new org.opencv.core.Size(15, 15);
                    mioClassificatoreOcchiOcchiali.detectMultiScale(matOcchi, occhi,1.1, 2,0,sOcchi);  

The result is always 0 insted if i leave my glasses, the result is always 2.
Anyone can help me??
Thanks in advance.

Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
Marco Gallella
  • 793
  • 1
  • 11
  • 18

3 Answers3

4

I haven't played with the Android implementation of OpenCV, but I have used the normal version. If your code works well with the Haar cascade for eyes and not for the one with glasses, that makes me suspect that the eyes+glasses cascade packaged with openCV isn't very good. I had the same issue trying to differentiate between front face and side face.

You can either train a new cascade (which is labor intensive) or look around for cascades that other people have trained for this case.

sarwar
  • 2,835
  • 1
  • 26
  • 30
  • This this indeed the problem with face/eye detection in opencv. We are dependant from the way the classifier has been trained – jlengrand Feb 23 '12 at 23:01
2

I, too, was having a similar issue with my OpenCV Windows clients not seeing people with glasses. After a few days of digging and debugging I finally determined that the glare generated from the monitor itself (and my large white form) was causing the problems. This glare was preventing OpenCV from even detecting a face let alone ID it. Why was this? If OpenCV can't see the eyes then it can't see a face.

I don't know if the much smaller Android screen could be causing this same issue or not, but it might be worth your time.

Regarding the Haar cascases... I went through every cascade file I could find including the ones referenced in other responses. The best file, by far, is: haarcascade_frontalface_alt2.xml. For my environment and for all types of people this file is by far the best.

Here's a some examples.


No glasses and OpenCV can detect my face just fine:

No glasses and OpenCV can detect a face just fine


Glare from my white form on my monitor is now preventing OpenCV from detecting my face:

Glare from my white form on my monitor is now preventing OpenCV from detecting a face


Finally, with my webcam rotated all the up, there is no glare on my eyeglasses and OpenCV can detect my face just fine:

Rotated webcam

Haluska
  • 117
  • 2
  • 6
2

I found that the Opencv classifier named haarcascade_mcs_eyepair_small give good result in android project in case with and without glasses. In fact it found the couple of eyes with one coordinates x and y.
Hope this could be helpful...

Marco Gallella
  • 793
  • 1
  • 11
  • 18