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();
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.
source
share