Cannot load CascadeClassifier

I tried loading the cascading classifier in an Android application, but the following condition always returns true, and therefore the code cannot be successfully executed:

cascadeClassifier.empty()

The code is as follows:

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

            // Load the cascade classifier
            cascadeClassifier = new CascadeClassifier(mCascadeFile.getAbsolutePath());
            if (cascadeClassifier.empty()) {
                Log.e(TAG, "Failed to load cascade classifier");
                cascadeClassifier = null;
            }
        }
        catch (Exception e)
        {
            Log.e("OpenCVActivity", "Error loading cascade", e);
        }

The cascade.xml file is stored in the source folder, and I successfully tested it with a python script - it successfully detects objects.

If this answer is correct, then I don’t know what might be wrong in the code above, since the tested cascade has been tested, and the input stream seems to indicate the correct location (R.raw.cascade autocomplete lists).

I would be very grateful if anyone could help solve the problem.

+4
source share
1

CascadeClassifier:

cascadeClassifier.load(mCascadeFile.getAbsolutePath());

:

InputStream is = getResources().openRawResource(R.raw.object_detector);
File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
mCascadeFile = new File(cascadeDir, "cascade.xml");
FileOutputStream os = new FileOutputStream(mCascadeFile);


byte[] buffer = new byte[4096];
int bytesRead;

while ((bytesRead = is.read(buffer)) != -1)
{
    os.write(buffer, 0, bytesRead);
    Log.d(TAG, "buffer: " + buffer.toString());
}
is.close();
os.close();
// Load the cascade classifier
cascadeClassifier = new CascadeClassifier(mCascadeFile.getAbsolutePath());
cascadeClassifier.load(mCascadeFile.getAbsolutePath());
if (cascadeClassifier.empty()) {
    Log.e(TAG, "Failed to load cascade classifier");
    cascadeClassifier = null;
}
+4

All Articles