cascadeClassifier.detectMultiScale get error: Insufficient memory in OutOfMemoryError

Hello everyone I have the following code:

public boolean isContainsFace(String path){
        Mat matImage = Imgcodecs.imread(path);

        MatOfRect faceDetections = new MatOfRect();
        cascadeClassifier.detectMultiScale(matImage, faceDetections);

        return !faceDetections.empty();
    }

I use it to check if there are faces in the photos. And that’s when I tried a larger-than-usual photo, I got the following error:

E/cv::error(): OpenCV(4.6.0-dev) Error: Insufficient memory (Failed to allocate 1281229312 bytes) in OutOfMemoryError, file E:/OpenCV/opencv/modules/core/src/alloc.cpp, line 73
E/org.opencv.objdetect: objdetect::detectMultiScale_15() caught cv::Exception: OpenCV(4.6.0-dev) E:/OpenCV/opencv/modules/core/src/alloc.cpp:73: error: (-4:Insufficient memory) Failed to allocate 1281229312 bytes in function 'OutOfMemoryError'
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.findyourselfinthephoto, PID: 25403
    CvException [org.opencv.core.CvException: cv::Exception: OpenCV(4.6.0-dev) E:/OpenCV/opencv/modules/core/src/alloc.cpp:73: error: (-4:Insufficient memory) Failed to allocate 1281229312 bytes in function 'OutOfMemoryError'
    ]
        at org.opencv.objdetect.CascadeClassifier.detectMultiScale_5(Native Method)
        at org.opencv.objdetect.CascadeClassifier.detectMultiScale(CascadeClassifier.java:195)
        at com.example.findyourselfinthephoto.Helpers.PhotoHelper.isContainsFace(PhotoHelper.java:41)
        at com.example.findyourselfinthephoto.Fragments.Home.onRequestHandlePathOz(Home.java:164)
        at br.com.onimur.handlepathoz.utils.HandlePathOzUtils$getRealPath$1.invokeSuspend(HandlePathOzUtils.kt:107)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.internal.ScopeCoroutine.afterResume(Scopes.kt:33)
        at kotlinx.coroutines.AbstractCoroutine.resumeWith(AbstractCoroutine.kt:102)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:46)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
    	Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@e6b34af, Dispatchers.Main]

What should i do to fix that?

Don’t use larger-than-usual photos… Resize photos if the original file is too large.

that’s just 1.3 GB for that one allocation. how much RAM do you have? what settings do you give your java VM?

crosspost: