I have problem with detectMultiScale method in my project

Hi everyone. Please if someone have any information about how to find solve for this Exception, you will he;lp me very much.

E/cv::error(): OpenCV(3.4.15) Error: Assertion failed (!empty()) в void cv::CascadeClassifier::detectMultiScale(cv::InputArray, std::vector<cv::Rect_ < int> >&, double, int, int, cv::Size, cv::Size), file/build/3_4_pack-android/opencv/modules/objdetect/src/cascadedetect.cpp, line 1729

E/org.opencv . objdetect: objdetect::detectMultiScale_15() caught cv::Exception: OpenCV(3.4.15) /build/3_4_pack-android/opencv/modules/objdetect/src/cascadedetect.cpp:1729: error: (-215: Assertion failed ) !empty() in function 'void cv::CascadeClassifier::detectMultiScale(cv::InputArray, std::vector<cv::Rect_<int> >&, double, int, int, cv::Size, cv: : Size)'

E/AndroidRuntime: FATAL EXCEPTION: Process Thread-2: com.example.eyecontrolmediaplayer, PID: 28922 CvException [org.opencv.core.CvException: cv:: Exception: OpenCV(3.4.15) /build/3_4_pack-android/opencv/modules/objdetect/src/cascadedetect.cpp:1729: error: (-215: Assertion failed) !empty() in function'void cv:: CascadeClassifier::detectMultiScale(cv: :InputArray, std::vector<cv::Rect_<int> >&, double, int, int, cv::Size, cv::Size)'

This error appear when project steps into detectMultiScale method in my project. I’m using the haarcascade_eye.xml file in my CascadeClassifier class. I have this xml file in assets folder in to project folder, but I don’t know why I have this error?

problem is, it was not found / loaded (so the cascade is ‘empty()’)

opencv cannot read directly from your assets folder
(which is zipped inside your apk, right ?)

before you can use it, you need to copy it to /tmp , /sdcard or similar, then read from there.

please have a look at how it is done in the sample app

My haarcascade file is not zipped, I put it right in My project folder like this C:\Users\user\MyProject\app\main\src\assets\haarcascade_eye.xml file, and use this path in my code.

that path may exist on your computer, but not on your android device.

does that make sense to you?

1 Like

again, when building the apk, that asset folder will end up, zipped, inside of the apk.
c-code, like fopen() cannot reach it there (different to java !)

similar problem for nn models, pretrained tracker files, anything you’d just load ‘from disk’ on a pc.

when you compile / build your program, your phone is not present, so no hd / sdcard to put it, that’s why such files end up (temporary) in your ‘assets’ folder in your project

Yes, this is sense to me very much. I’m real didn’t know how I can use the cascade file in my android project on android device. Have you some samples how it’s sovle by someone else, or maybe you have some links when I can find the solution of this problem?

So what I need to do? I see the sample in the link which you are send me, but I not sure that I can use this in my android project. I copy/paste this code, but there is many moments wich I can uderstand, like InputStream is = getResources().openRawResource(R.raw.lbpcascade_frontalface). I’m real don’t know what are raw is this…

in the sample, the file is put under /res/raw (not into assets)

I don’t have the /res/raw folder in my project, the studio ask me to create the raw resouse file haarcascade_eye.xml but there no raw folder, I can create that file in some others folders like values, xml, drawable and etc. May I create the raw folder by my self like New-folder-raw and then put this raw folder in my res folder?

IF you have a res folder already – sure, go try !

I’ll tryed, but it’s doesn’t work. I really dont know what to do.

 public EyeDetectionClass(){
        String path = "MyProject\\app\\src\\main\\res\\raw\\haarcascade_eye.xml";
        eyeCascade = new CascadeClassifier();
        eyeCascade.load(path);
    } 

This is my block of code, of constructor for this class, where I try load cascade file.

you don’t listen, do you ?

In this sample I don’t see how I can put this is the /tmp or /sdcard. How I can do this? I doing my project on computer, not on phone, so I have all files on computer, and I don’t know how I can put this file in tmp folder. If I need that my app will be installed on many devices, I can’t copy this file in the tmp folder in each device, how can I do this automaticly? What information I need take from this sample? This sample have in opencv java folder wich I download from opencv.org, but I don’t know what I need to see in this code, what can help me find the soution.

that is exactly, what the sample code is doing, look again.

There are 2 methods here that cannot be recognized: getResources(), getDir() what should I do with them? The studio says that I need to declare them in the class, I do it, but they both return null, what should I do in this case? I already tried this example code, I understand what it does, but these methods don’t work…

I think that this sample don’t work for Android applications. I’m completely confused, I have no idea what to do.

https://guides.codepath.com/android/Understanding-App-Resources

apart from your current, android coding problems (where we can’t help, probably) –

i have no idea, what your app does in the end, but trying to find eyes using cascades without a previous face detection (and restricting eye-search to that region) will be terribly inaccurate.

keep an eye on FaceDetectorYN (OpenCV 4.7.0 Java documentation) , which returns face rects + 5 landmarks (eyes, nose, mouth)
(however, same problem with android, it needs external pretrained models / resources, …)