cascadeClassifier.load() returns false

I’m try to run my android application on qt, and load haarcascade_frontalface_alt.xml, but everytime i get a error: (-215:Assertion failed) !empty() in function ‘detectMultiScale’
Source info:

  • opencv for android built via cmake from github sources

  • I have tried the full path to cascades: “/home/deymos/project/androidTestBuild/3rdParties/opencv_lbpcascades/haarcascade_frontalface_alt.xml”

  • also have tried define it from .pro file:
    designbuild { PATH_TO_CASCADE_FILE = $${PWD}/QVisionAlgorithms/3rdParties/opencv_lbpcascades/haarcascade_frontalface_alt.xml DEFINES += CASCADE_NAME=\\\"$${PATH_TO_CASCADE_FILE}\\\" PATH_TO_MODEL_FILE = $${PWD}/QVisionAlgorithms/3rdParties/model/tracker DEFINES += MODEL_NAME=\\\"$${PATH_TO_MODEL_FILE}\\\"

Anyway i get the error
E cv::error(): OpenCV(4.5.4-dev) Error: Assertion failed (!empty()) in detectMultiScale, file /home/deymos/opencv-4.x/modules/objdetect/src/cascadedetect.cpp, line 1689
F libc : /buildbot/src/android/ndk-release-r21/external/libcxx/…/…/external/libcxxabi/src/abort_message.cpp:72: abort_message: assertion "terminating with uncaught exception of type cv::Exception: OpenCV(4.5.4-dev) /home/deymos/opencv-4.x/modules/objdetect/src/cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function ‘detectMultiScale’
F libc : " failed
F libc : Fatal signal 6 (SIGABRT), code -6 (SI_TKILL) in tid 8056 (qtMainLoopThrea), pid 7945 (androidTestBuild)

I am also interested in the question why the error occurs in the opencv4.x folder from which I built opencv, is there no cascadedetect.hpp in the folder with the built opencv?

also here is a screenshot that the path is correct
image

loading cascades (or other resources) on android is indeed a little tricky.

first, any path on your development machine is useless
(your phone is a physically seperate box)

the most easy way would be to put the cascade on the SDCard, and load it from there, but - you cannot do this for other ppl’s phones.

in the end, you’ll put it somewhere into the “assets”, so it can be distributed along with your app. however - this is zipped inside your apk, and the C++ code cannot read it from there, so it needs java code to make a copy on the SDCard before you can load it into the Classifier.

again, please have a look, how this is handled in the samples

I don’t know how I could make such a stupid mistake, thank you most likely this is the solution, but with your permission, I won’t close the issue for now, I’ll close it when I implement it, otherwise problems will arise, and I don’t want to create a new topic

1 Like

I added files to qt resources, and checked if they exist, it outputs true, but classifier still doesn’t load, what could be the problem?

    QFileInfo fileInfo(":/haar/3rdParties/opencv_lbpcascades/haarcascade_frontalface_alt.xml");
    qDebug()<<fileInfo.exists()<<fileInfo.isFile();
    qDebug()<<cascadeClassifier.load(":/haar/3rdParties/opencv_lbpcascades/haarcascade_frontalface_alt.xml");

output:
D libQtAndroidToolsDemo_armeabi-v7a.so: true true
D libQtAndroidToolsDemo_armeabi-v7a.so: false

I have to add additional information, in my home directory there is a folder with opencv4.x sources that I downloaded from github, and the build folder with compiled opencv and libraries is in the project folder, but the error comes from the source folder, can not does this cause a problem?

E cv::error(): OpenCV(4.5.4-dev) Error: Assertion failed (!empty()) in detectMultiScale, file /home/deymos/opencv-4.x/modules/objdetect/src/cascadedetect.cpp, line 1689
F libc    : /buildbot/src/android/ndk-release-r21/external/libcxx/../../external/libcxxabi/src/abort_message.cpp:72: abort_message: assertion "terminating with uncaught exception of type cv::Exception: OpenCV(4.5.4-dev) /home/deymos/opencv-4.x/modules/objdetect/src/cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'detectMultiScale'
F libc    : " failed

that’s ok. error messages are generated at compile time, on your dev machine.
(lookup the __FILE__ macro, if you’re curious)

sorry no idea about qt on android, or what a path starting with :/ might be …
can you fopen() it ? if not, the Classifier cant either !

maybe you can try with the SDCard again ?

please also check, if you really have valid xml files, not html error msgs there.

[edit]
ok, i looked it up and im quite sure, that you have to copy it from there to SDCard using qt file functions (similar to the java example) before you can load it into the cascade

So, i did this, and it’s work! Thank you!
but this is another problem, that i can’t google, maybe you know what is it?

E cv::error(): OpenCV(4.5.4-dev) Error: Bad argument (Matrix operand is an empty matrix.) in checkOperandsExist, file /home/deymos/opencv-4.x/modules/core/src/matrix_expressions.cpp, line 24
F libc    : /buildbot/src/android/ndk-release-r21/external/libcxx/../../external/libcxxabi/src/abort_message.cpp:72: abort_message: assertion "terminating with uncaught exception of type cv::Exception: OpenCV(4.5.4-dev) /home/deymos/opencv-4.x/modules/core/src/matrix_expressions.cpp:24: error: (-5:Bad argument) Matrix operand is an empty matrix. in function 'checkOperandsExist'
F libc    : " failed

hard to say without seeing your code.
some mat is empty(), check, what you get from imread() or VideoCapture

ah, sorry, i’ve just forgot to add tracker, thanks a lot for the help, you just saved me from burnout)