Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

CascadeClassifier with haarcascade apparently not working

Hi there,

First of all thanks for any help !!!

I am trying to detect the nose on a face Bitmap.

For the purpose I use a CascadeClassifier with haarcascade.

Here is the haarcascade: link text

Here is the code:

            //Load image, convert into Bitmap, convert to grayscle
            BitmapFactory.Options op = new BitmapFactory.Options();
            op.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.face, op);
            Bitmap.Config conf = Bitmap.Config.ARGB_8888;
            bm=toGrayscale(bm);

            // Initialize Mat and MatofRec
            mat = new Mat(); 
            rec = new MatOfRect();              

            //Load Bitmap into Map
            ut.bitmapToMat(bm, mat);
            mat.convertTo(mat2, CvType.CV_8U);
            Log.e("","mat è " +Integer.toString(mat.width())+" "+ Integer.toString(mat.height())+" "+ Integer.toString(mat.type()));

            //Create CascadeClassifier loading haarcascade from Android Resources
            CascadeClassifier cc= new CascadeClassifier("android.resource://com.example.test/raw/haarcascade_mcs_nose.xml");

            //Call detectMultiScale
            cc.detectMultiScale(mat2,rec,1.1,2,2,new Size(400,400),new Size(400,400));
            outrec=rec.toArray();

But apparently something is not working.

The MatOfRect rec contains no Rect, indeed the Array outrec is empty: outrec.length=0.

What am I doing wrong?

Thanks a lot for any hint !!!

This is the screenshot. Picture is 480x600. image description

CascadeClassifier with haarcascade apparently not working

Hi there,

First of all thanks for any help !!!

I am trying to detect the nose on a face Bitmap.

For the purpose I use a CascadeClassifier with haarcascade.

Here is the haarcascade: link text

Here is the code:

            //Load image, convert into Bitmap, convert to grayscle
            BitmapFactory.Options op = new BitmapFactory.Options();
            op.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.face, op);
            Bitmap.Config conf = Bitmap.Config.ARGB_8888;
            bm=toGrayscale(bm);

            // Initialize Mat and MatofRec
            mat = new Mat(); 
            rec = new MatOfRect();              

            //Load Bitmap into Map
            ut.bitmapToMat(bm, mat);
            mat.convertTo(mat2, CvType.CV_8U);
            Log.e("","mat è " +Integer.toString(mat.width())+" "+ Integer.toString(mat.height())+" "+ Integer.toString(mat.type()));

            //Create CascadeClassifier loading haarcascade from Android Resources
            CascadeClassifier cc=  new CascadeClassifier("android.resource://com.example.test/raw/haarcascade_mcs_nose.xml");

            //Call detectMultiScale
            cc.detectMultiScale(mat2,rec,1.1,2,2,new Size(400,400),new Size(400,400));
            outrec=rec.toArray();

But apparently something is not working.

The MatOfRect rec contains no Rect, indeed the Array outrec is empty: outrec.length=0.

What am I doing wrong?

Thanks a lot for any hint !!!

This is the screenshot. Picture is 480x600. image description

CascadeClassifier with haarcascade apparently not working

Hi there,

First of all thanks for any help !!!

I am trying to detect the nose on a face Bitmap.

For the purpose I use a CascadeClassifier with haarcascade.

Here is the haarcascade: link text

Here is the code:

            //Load image, convert into Bitmap, convert to grayscle
            BitmapFactory.Options op = new BitmapFactory.Options();
            op.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.face, op);
            Bitmap.Config conf = Bitmap.Config.ARGB_8888;
            bm=toGrayscale(bm);

            // Initialize Mat and MatofRec
            mat = new Mat(); 
            rec = new MatOfRect();              

            //Load Bitmap into Map
            ut.bitmapToMat(bm, mat);
            mat.convertTo(mat2, CvType.CV_8U);
            Log.e("","mat è " +Integer.toString(mat.width())+" "+ Integer.toString(mat.height())+" "+ Integer.toString(mat.type()));

            //Create CascadeClassifier loading haarcascade from Android Resources
            CascadeClassifier cc= 
            new CascadeClassifier("android.resource://com.example.test/raw/haarcascade_mcs_nose.xml");

            //Call detectMultiScale
            cc.detectMultiScale(mat2,rec,1.1,2,2,new Size(400,400),new Size(400,400));
            outrec=rec.toArray();

But apparently something is not working.

The MatOfRect rec contains no Rect, indeed the Array outrec is empty: outrec.length=0.

What am I doing wrong?

Thanks a lot for any hint !!!

This EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT

ISSUE SOLVED.

what I was doing wrong was loading the HaarCascade in the CascadeClassifier.

I changed it accordingly with FaceDetection sample application on OpenCV4Android SDK.

Here is the screenshot. Picture updated code:

            //Load image, convert into Bitmap, convert to grayscle
            BitmapFactory.Options op = new BitmapFactory.Options();
            op.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.face, op);
            Bitmap.Config conf = Bitmap.Config.ARGB_8888;
            bm=toGrayscale(bm);             

            mat = new Mat(); 
            Mat mat2 = new Mat(); 
            rec = new MatOfRect();              
            //Load Bitmap into Map
            ut.bitmapToMat(bm, mat);
            //mat.convertTo(mat2, CvType.CV_8UC1);
            //cvtColor(mat,mat2,CV_RGB2GRAY);
            Imgproc.cvtColor(mat, mat2, Imgproc.COLOR_RGBA2GRAY);
            Log.e("","mat è " +Integer.toString(mat.width())+" "+ Integer.toString(mat.height())+" "+ Integer.toString(mat.type()));

            bmp = Bitmap.createBitmap(480, 600, conf);
            ut.matToBitmap(mat2,bmp);
            iv.setImageBitmap(bmp);
            //Create CascadeClassifier loading haarcascade from Android Resources


            try {
            InputStream is 480x600. 
image description

= getResources().openRawResource(R.raw.haarcascade_mcs_nose); File cascadeDir = getDir("cascade", Context.MODE_PRIVATE); File mCascadeFile = new File(cascadeDir, "haarcascade_mcs_nose.xml"); FileOutputStream os = new FileOutputStream(mCascadeFile); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = is.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } is.close(); os.close(); mJavaDetector = new CascadeClassifier(mCascadeFile.getAbsolutePath()); if (mJavaDetector.empty()) { Log.e(TAG, "Failed to load cascade classifier"); mJavaDetector = null; } else Log.i(TAG, "Loaded cascade classifier from " + mCascadeFile.getAbsolutePath()); } catch (IOException e) { e.printStackTrace(); Log.e(TAG, "Failed to load cascade. Exception thrown: " + e); } //Call detectMultiScale mJavaDetector.detectMultiScale(mat2,rec,1.1,2,2,new Size(10,10),new Size(400,400)); outrec=rec.toArray(); Log.e("","Contiene rettangoli n =" + Integer.toString(outrec.length)); Log.e(TAG, " funziona!!!");

CascadeClassifier with haarcascade apparently not workingworking - SOLVED

Hi there,

First of all thanks for any help !!!

I am trying to detect the nose on a face Bitmap.

For the purpose I use a CascadeClassifier with haarcascade.

Here is the haarcascade: link text

Here is the code:

            //Load image, convert into Bitmap, convert to grayscle
            BitmapFactory.Options op = new BitmapFactory.Options();
            op.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.face, op);
            Bitmap.Config conf = Bitmap.Config.ARGB_8888;
            bm=toGrayscale(bm);

            // Initialize Mat and MatofRec
            mat = new Mat(); 
            rec = new MatOfRect();              

            //Load Bitmap into Map
            ut.bitmapToMat(bm, mat);
            mat.convertTo(mat2, CvType.CV_8U);
            Log.e("","mat è " +Integer.toString(mat.width())+" "+ Integer.toString(mat.height())+" "+ Integer.toString(mat.type()));

            //Create CascadeClassifier loading haarcascade from Android Resources
            CascadeClassifier cc= 
            new CascadeClassifier("android.resource://com.example.test/raw/haarcascade_mcs_nose.xml");

            //Call detectMultiScale
            cc.detectMultiScale(mat2,rec,1.1,2,2,new Size(400,400),new Size(400,400));
            outrec=rec.toArray();

But apparently something is not working.

The MatOfRect rec contains no Rect, indeed the Array outrec is empty: outrec.length=0.

What am I doing wrong?

Thanks a lot for any hint !!!

EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT

ISSUE SOLVED.

what I was doing wrong was loading the HaarCascade in the CascadeClassifier.

I changed it accordingly with FaceDetection sample application on OpenCV4Android SDK.

Here is the updated code:

            //Load image, convert into Bitmap, convert to grayscle
            BitmapFactory.Options op = new BitmapFactory.Options();
            op.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.face, op);
            Bitmap.Config conf = Bitmap.Config.ARGB_8888;
            bm=toGrayscale(bm);             

            mat = new Mat(); 
            Mat mat2 = new Mat(); 
            rec = new MatOfRect();              
            //Load Bitmap into Map
            ut.bitmapToMat(bm, mat);
            //mat.convertTo(mat2, CvType.CV_8UC1);
            //cvtColor(mat,mat2,CV_RGB2GRAY);
            Imgproc.cvtColor(mat, mat2, Imgproc.COLOR_RGBA2GRAY);
            Log.e("","mat è " +Integer.toString(mat.width())+" "+ Integer.toString(mat.height())+" "+ Integer.toString(mat.type()));

            bmp = Bitmap.createBitmap(480, 600, conf);
            ut.matToBitmap(mat2,bmp);
            iv.setImageBitmap(bmp);
            //Create CascadeClassifier loading haarcascade from Android Resources


            try {
            InputStream is = getResources().openRawResource(R.raw.haarcascade_mcs_nose);
            File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
            File mCascadeFile = new File(cascadeDir, "haarcascade_mcs_nose.xml");
            FileOutputStream os = new FileOutputStream(mCascadeFile);

            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = is.read(buffer)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            is.close();
            os.close();

            mJavaDetector = new CascadeClassifier(mCascadeFile.getAbsolutePath());
            if (mJavaDetector.empty()) {
                Log.e(TAG, "Failed to load cascade classifier");
                mJavaDetector = null;
            } else
                Log.i(TAG, "Loaded cascade classifier from " + mCascadeFile.getAbsolutePath());


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

            //Call detectMultiScale
            mJavaDetector.detectMultiScale(mat2,rec,1.1,2,2,new Size(10,10),new Size(400,400));
            outrec=rec.toArray();
            Log.e("","Contiene rettangoli n =" + Integer.toString(outrec.length));
            Log.e(TAG, " funziona!!!");

CascadeClassifier with haarcascade apparently not working - SOLVEDworking

Hi there,

First of all thanks for any help !!!

I am trying to detect the nose on a face Bitmap.

For the purpose I use a CascadeClassifier with haarcascade.

Here is the haarcascade: link text

Here is the code:

            //Load image, convert into Bitmap, convert to grayscle
            BitmapFactory.Options op = new BitmapFactory.Options();
            op.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.face, op);
            Bitmap.Config conf = Bitmap.Config.ARGB_8888;
            bm=toGrayscale(bm);

            // Initialize Mat and MatofRec
            mat = new Mat(); 
            rec = new MatOfRect();              

            //Load Bitmap into Map
            ut.bitmapToMat(bm, mat);
            mat.convertTo(mat2, CvType.CV_8U);
            Log.e("","mat è " +Integer.toString(mat.width())+" "+ Integer.toString(mat.height())+" "+ Integer.toString(mat.type()));

            //Create CascadeClassifier loading haarcascade from Android Resources
            CascadeClassifier cc= 
            new CascadeClassifier("android.resource://com.example.test/raw/haarcascade_mcs_nose.xml");

            //Call detectMultiScale
            cc.detectMultiScale(mat2,rec,1.1,2,2,new Size(400,400),new Size(400,400));
            outrec=rec.toArray();

But apparently something is not working.

The MatOfRect rec contains no Rect, indeed the Array outrec is empty: outrec.length=0.

What am I doing wrong?

Thanks a lot for any hint !!!

EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT

ISSUE SOLVED.

what I was doing wrong was loading the HaarCascade in the CascadeClassifier.

I changed it accordingly with FaceDetection sample application on OpenCV4Android SDK.

Here is the updated code:

            //Load image, convert into Bitmap, convert to grayscle
            BitmapFactory.Options op = new BitmapFactory.Options();
            op.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.face, op);
            Bitmap.Config conf = Bitmap.Config.ARGB_8888;
            bm=toGrayscale(bm);             

            mat = new Mat(); 
            Mat mat2 = new Mat(); 
            rec = new MatOfRect();              
            //Load Bitmap into Map
            ut.bitmapToMat(bm, mat);
            //mat.convertTo(mat2, CvType.CV_8UC1);
            //cvtColor(mat,mat2,CV_RGB2GRAY);
            Imgproc.cvtColor(mat, mat2, Imgproc.COLOR_RGBA2GRAY);
            Log.e("","mat è " +Integer.toString(mat.width())+" "+ Integer.toString(mat.height())+" "+ Integer.toString(mat.type()));

            bmp = Bitmap.createBitmap(480, 600, conf);
            ut.matToBitmap(mat2,bmp);
            iv.setImageBitmap(bmp);
            //Create CascadeClassifier loading haarcascade from Android Resources


            try {
            InputStream is = getResources().openRawResource(R.raw.haarcascade_mcs_nose);
            File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
            File mCascadeFile = new File(cascadeDir, "haarcascade_mcs_nose.xml");
            FileOutputStream os = new FileOutputStream(mCascadeFile);

            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = is.read(buffer)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            is.close();
            os.close();

            mJavaDetector = new CascadeClassifier(mCascadeFile.getAbsolutePath());
            if (mJavaDetector.empty()) {
                Log.e(TAG, "Failed to load cascade classifier");
                mJavaDetector = null;
            } else
                Log.i(TAG, "Loaded cascade classifier from " + mCascadeFile.getAbsolutePath());


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

            //Call detectMultiScale
            mJavaDetector.detectMultiScale(mat2,rec,1.1,2,2,new Size(10,10),new Size(400,400));
            outrec=rec.toArray();
            Log.e("","Contiene rettangoli n =" + Integer.toString(outrec.length));
            Log.e(TAG, " funziona!!!");