# Cascade detectMultiScale shows error while running

**URL:** <https://forum.opencv.org/t/cascade-detectmultiscale-shows-error-while-running/16096>\
**Category:** Uncategorized\
**Tags:** programming\
**Created:** [January 8, 2024, 8:51pm UTC](https://forum.opencv.org/t/cascade-detectmultiscale-shows-error-while-running/16096 "2024-01-08T20:51:40Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![zlogdan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/zlogdan/32/9387_2.png) [@zlogdan](https://forum.opencv.org/u/zlogdan)\
**Post date:** [January 8, 2024, 8:51pm UTC](https://forum.opencv.org/t/cascade-detectmultiscale-shows-error-while-running/16096/1 "2024-01-08T20:51:40Z")

</div>

Hi

I am trying to run the code below but it always fails as described. I am not sure what I am doing incorrectly there, but the code is very simple.

```auto
std::vector<Rect> detectFacesInImage(Mat& img)
{
    std::vector<Rect> faces;
    CascadeClassifier cascade;
    std::string f = "haarcascades//haarcascade_frontalface_default.xml";
    if (cascade.load(f) == true)
    {
        cascade.detectMultiScale(img, faces);
        return faces;
    }
    return faces;
}

```

At this point, the image is already converted to grayscale. While running

cascade.detectMultiScale(img, faces);

an error is thrown.

This used to work so I have a suspicion that it is due to

Windows 11  
or because of my graphic card  
or both

I used to run this without problems before using Windows 10 and a different hardware set.  
Is there anything I am missing here?

Thanks for the help  
Daniel

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [January 9, 2024, 8:52am UTC](https://forum.opencv.org/t/cascade-detectmultiscale-shows-error-while-running/16096/2 "2024-01-09T08:52:16Z")

</div>

> [@zlogdan](#):
>
> an error is thrown.

which is ? you probably want to tell us ! …

---

<div class="post-metadata">

**Author:** ![zlogdan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/zlogdan/32/9387_2.png) [@zlogdan](https://forum.opencv.org/u/zlogdan)\
**Post date:** [January 9, 2024, 1:03pm UTC](https://forum.opencv.org/t/cascade-detectmultiscale-shows-error-while-running/16096/4 "2024-01-09T13:03:10Z")

</div>

Hi  
I apologize, I forgot to add this information.

```auto
Exceção gerada em 0x00007FFA9543567C em diImage.exe: Exceção do Microsoft C++: cv::Exception no local de memória 0x000000C373DAD130.
"diImage.exe" (Win32): Carregado "C:\Windows\System32\opencl.dll". 
"diImage.exe" (Win32): Carregado "C:\Windows\System32\DXCore.dll". 
"diImage.exe" (Win32): Carregado "C:\Windows\System32\AppXDeploymentClient.dll". 
"diImage.exe" (Win32): Carregado "C:\Windows\System32\dxgi.dll". 
"diImage.exe" (Win32): Carregado "C:\Windows\System32\ResourcePolicyClient.dll". 
"diImage.exe" (Win32): Não carregado "C:\Windows\System32\ResourcePolicyClient.dll"
"diImage.exe" (Win32): Carregado "C:\Windows\System32\directxdatabasehelper.dll". 
"diImage.exe" (Win32): Não carregado "C:\Windows\System32\directxdatabasehelper.dll"
"diImage.exe" (Win32): Não carregado "C:\Windows\System32\dxgi.dll"
"diImage.exe" (Win32): Carregado "C:\Windows\System32\nvopencl.dll". 
"diImage.exe" (Win32): Carregado "C:\Windows\System32\nvapi64.dll". 
Exceção gerada em 0x0000000000000000 em diImage.exe: 0xC0000005: violação de acesso ao executar o local 0x0000000000000

```

It is an access violation exception.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 9, 2024, 6:19pm UTC](https://forum.opencv.org/t/cascade-detectmultiscale-shows-error-while-running/16096/5 "2024-01-09T18:19:25Z")

</div>

> [@zlogdan](#):
>
> `//`

that’s an invalid path.

learn about the “working directory”, and in which working dir your process actually executes. this is how relative paths are resolved.

also: load the cascade XML _once_ at the start of the program, not every time you want to detect.

---

<div class="post-metadata">

**Author:** ![zlogdan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/zlogdan/32/9387_2.png) [@zlogdan](https://forum.opencv.org/u/zlogdan)\
**Post date:** [January 9, 2024, 7:14pm UTC](https://forum.opencv.org/t/cascade-detectmultiscale-shows-error-while-running/16096/6 "2024-01-09T19:14:56Z")

</div>

Hi Crackwitz,

Yes, I have the working folder running under that relative path.

However, the standard C++ way of using paths is using a double “\”, but as you said, I made a mistake. I used the wrong escape sequence “//” ( You may forgive me because I work with both windows and Linux ), usually, compilers would write this warning.

```auto
>C:\Users\Administrador\Documents\GitHub\ImageProcessing\opcvwrapper.cpp(823,21): warning C4129: 'h': sequência de escape de caractere não reconhecida

```

Also:  
cascade.load(f) returned true before, meaning that it was properly loaded, which does not happen if I use a single “/”

Edit: I had also checked the existence of the path using c++17

```auto
#include <filesystem>
namespace fs = std::filesystem;
bool file_exists(const std::string& file)
{
	return fs::exists(file);
}

```

Edit 2:  
it does not work with a single /  
std::string f = “haarcascades\haarcascade\_frontalface\_default.xml”;

I even copied it to the working directory

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/8/8b5407fb370ebae1afab579853f409f97d18de47.png)

Same error.

regards  
Daniel

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 9, 2024, 7:47pm UTC](https://forum.opencv.org/t/cascade-detectmultiscale-shows-error-while-running/16096/7 "2024-01-09T19:47:52Z")

</div>

> [@zlogdan](#):
>
> ```auto
> Exceção gerada em 0x00007FFA9543567C em diImage.exe: Exceção do Microsoft C++: cv::Exception no local de memória 0x000000C373DAD130.
> 
> ```

I can’t read this language but I can see the `cv::Exception`.

catch the exception and print it. put a try-catch around the entire program.

---

<div class="post-metadata">

**Author:** ![zlogdan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/zlogdan/32/9387_2.png) [@zlogdan](https://forum.opencv.org/u/zlogdan)\
**Post date:** [January 9, 2024, 8:50pm UTC](https://forum.opencv.org/t/cascade-detectmultiscale-shows-error-while-running/16096/8 "2024-01-09T20:50:23Z")

</div>

that exception is thrown when I run this code:

```auto
        auto axes = CvPlot::plotImage(clone);
        cv::Mat mat = axes.render(clone.size().width, clone.size().height);
        CvPlot::show(title, axes);
        waitKey(0);

```

This is a graph library wrapper written for openCV , but this exception does not stop the program execution.

But thanks for your help. It looks like something at my hardware causes the error probably. Because I used to run this code on another computer.

Thanks  
Daniel

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 9, 2024, 9:07pm UTC](https://forum.opencv.org/t/cascade-detectmultiscale-shows-error-while-running/16096/9 "2024-01-09T21:07:32Z")

</div>

well what does the `cv::exception` _say_? it’s got a message text attached to it. you have not shared this piece of information yet.

---

<div class="post-metadata">

**Author:** ![zlogdan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/zlogdan/32/9387_2.png) [@zlogdan](https://forum.opencv.org/u/zlogdan)\
**Post date:** [January 9, 2024, 10:04pm UTC](https://forum.opencv.org/t/cascade-detectmultiscale-shows-error-while-running/16096/10 "2024-01-09T22:04:43Z")

</div>

That exception cannot be captured. At least I cannot use a try catch to grab it. However, it does not work if I remove the code from the library and use imshow instead. There is a try catch that already does it. I have appreciated your help in this case, but it does look like something on my graphic board.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 10, 2024, 2:22pm UTC](https://forum.opencv.org/t/cascade-detectmultiscale-shows-error-while-running/16096/11 "2024-01-10T14:22:16Z")

</div>

> [@zlogdan](#):
>
> That exception cannot be captured. At least I cannot use a try catch to grab it.

why? it’s a `cv::Exception`. it’s made to be caught.

---

<div class="post-metadata">

**Author:** ![zlogdan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/zlogdan/32/9387_2.png) [@zlogdan](https://forum.opencv.org/u/zlogdan)\
**Post date:** [January 10, 2024, 5:18pm UTC](https://forum.opencv.org/t/cascade-detectmultiscale-shows-error-while-running/16096/12 "2024-01-10T17:18:47Z")

</div>

The exception is thrown internally inside CvPlot::show(title, axes). I think the code in the library simply ignores the exception so that is why I cannot capture it. Visual Studio shows us in the debug mode.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 10, 2024, 7:18pm UTC](https://forum.opencv.org/t/cascade-detectmultiscale-shows-error-while-running/16096/13 "2024-01-10T19:18:08Z")

</div>

```auto
try {
    auto axes = CvPlot::plotImage(clone);
    cv::Mat mat = axes.render(clone.size().width, clone.size().height);
    CvPlot::show(title, axes);
    waitKey(0);
}
catch (cv::Exception& e)
{
    std::cerr << e.msg << std::endl;
}

```
