Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Cascade Classifier - Can't detect banana(object) with an image

Hi, I have example for detecting banana with opencv realtime and it's work.

but now I want to try detecting banana with an image use the same xample, but i always can't get the MatOfRect objects.

here's my code

    File fillPath = new File(Environment.getExternalStorageDirectory() + "/test/pisang.jpg");
    File caseCadePath = new File(Environment.getExternalStorageDirectory() + "/test/banana_classifier.xml");
    Mat img = Imgcodecs.imread(fillPath.getAbsolutePath());
    MatOfRect objDetections = new MatOfRect();

    Mat dest = new Mat();
    Imgproc.cvtColor(img, dest, Imgproc.COLOR_BGR2GRAY);
    try {
        // load cascade file from application resources
        InputStream is = getResources().openRawResource(R.raw.banana_classifier);
        File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
        mCascadeFile = new File(cascadeDir, "banana_classifier.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();
        String sds = mCascadeFile.getAbsolutePath();
        mJavaDetector = new CascadeClassifier(mCascadeFile.getAbsolutePath());
        if (mJavaDetector.empty()) {
            mJavaDetector = null;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (mAbsoluteFaceSize == 0) {
        int height = dest.rows();
        if (Math.round(height * mRelativeFaceSize) > 0) {
            mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize);
        }
    }

    mJavaDetector.detectMultiScale(dest, objDetections, 1.3,
            3, 2, new Size(dest.rows(), dest.cols()), new Size());
    Rect[] rects1 = objDetections.toArray();

    mJavaDetector.detectMultiScale(dest, objDetections);

    Rect[] rects = objDetections.toArray();
    for (int i=0; i<rects.length; i++){

    }

I don't know why it's not work when I'am using an image.. please help..