Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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!!!");