Search code examples
androidopencvclassificationeye-detection

Find eyes with glasses OpenCv


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.


Solution

  • 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.