Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Haar Cascade in android

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("dhara", "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.

Haar Cascade in android

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("dhara", 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.

Haar Cascade in android

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