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

asked 2018-06-08 01:43:19 -0500

akbar gravatar 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..

edit retag flag offensive close merge delete

Comments

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

i very much doubt, that it is so. if you got that idea from one of those various silly blogposts -- they are all lying, and you simply got fooled.

berak gravatar imageberak ( 2018-06-08 02:04:22 -0500 )edit

oh, and you also got minSize and maxSize swapped here, so it can't detect anything:

mJavaDetector.detectMultiScale(dest, objDetections, 1.3,
        3, 2, new Size(dest.rows(), dest.cols()), new Size());

oh, and it must be Size(dest.cols(), dest.rows())

berak gravatar imageberak ( 2018-06-08 02:06:41 -0500 )edit