Haar Cascade in android

asked 2014-06-30 04:08:39 -0500

Dhara Shah gravatar image

updated 2014-06-30 06:07:27 -0500

Hello,

i am new to opencv for android and all that it has to offer. I would like to experiment with detecting pens using opencv and android (no native code in cpp), i have found a haarcascade file for pen detection, but i do not know how to detect the pens using the cascade classifier in pure android code.

This is what i have so far :

try {
            // load cascade file from application resources
            InputStream is = getResources().openRawResource(R.raw.haarcascade);
            File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
            mCascadeFile = new File(cascadeDir, "haarcascade.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());

            mNativeDetector = new DectionBasedTracker(mCascadeFile.getAbsolutePath(), 0);
            cascadeDir.delete();

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

        mOpenCvCameraView.enableView();

This code is in the onCreate since i am using static linking of opencv library.

While i am calling a method detectPens that takes up the grey mat, the haarcascade file path and also the MatOfRect variable. I do not want to call native methods but rather if possible want to use the methods available in Imgproc file.

Please assist. Thanks.

EDIT: I used the following to get the pens.toArray() length, but i am not sure if it is correct :(

mJavaDetector.detectMultiScale(mGreyMat, pens);
edit retag flag offensive close merge delete

Comments

looks good, so far. you probably don't need the mNativeDetector at all

berak gravatar imageberak ( 2014-06-30 06:22:19 -0500 )edit

yes true, i removed that piece of line. But i do not know how the detectMultiScale works.. since at times i get the correct count but at times i dont.

Dhara Shah gravatar imageDhara Shah ( 2014-06-30 06:37:54 -0500 )edit