# Problem with cuda CascadeClassifier

**URL:** <https://forum.opencv.org/t/problem-with-cuda-cascadeclassifier/2115>\
**Category:** Uncategorized\
**Tags:** cascadeclassifier, cuda\
**Created:** [March 11, 2021, 6:36pm UTC](https://forum.opencv.org/t/problem-with-cuda-cascadeclassifier/2115 "2021-03-11T18:36:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![TatanC](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/tatanc/32/5591_2.png) [@TatanC](https://forum.opencv.org/u/TatanC)\
**Post date:** [March 11, 2021, 6:36pm UTC](https://forum.opencv.org/t/problem-with-cuda-cascadeclassifier/2115/1 "2021-03-11T18:36:17Z")

</div>

Hi, I have trained my own haar cascade classifier using opencv version 3.2 and after checking its performance I tried to use the cuda functions to perform some tests, however I encountered the following error:

```
terminate called after throwing an instance of 'cv::Exception'what(): OpenCV(4.4.0) /tmp/build_opencv/opencv_contrib/modules/cudalegacy/src/cuda/NCVHaarObjectDetection.cu:2079: error: (-215:Assertion failed) haar.ClassifierSize.height > 0 && haar.ClassifierSize.width > 0 in function 'loadFromXML'

```

I am currently using opencv version 4.4 and applying the detectMultiScale function as follows:

```
         cv::cuda::GpuMat gframe, obj;
         std::vector<cv::Rect> rect;
         std::string cascadePath = "./cascade.xml"
         Ptr<cuda::CascadeClassifier> cascade = cv::cuda::CascadeClassifier::create(cascadePath);

         cascade->setScaleFactor(1.8);
         cascade->setMinNeighbors(10);
         cascade->setMinObjectSize(cv::Size(Tmin,Tmin));
         cascade->setMinObjectSize(cv::Size(Tmax,Tmax));
         cascade->detectMultiScale(gframe, obj);
         cascade->convert(obj, rect);

```

I appreciate any information.

---

<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:** [March 11, 2021, 9:20pm UTC](https://forum.opencv.org/t/problem-with-cuda-cascadeclassifier/2115/2 "2021-03-11T21:20:56Z")

</div>

file can’t be found. most common cause.

if you use relative paths, pay attention to the “current working directory” of your process.

---

<div class="post-metadata">

**Author:** ![TatanC](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/tatanc/32/5591_2.png) [@TatanC](https://forum.opencv.org/u/TatanC)\
**Post date:** [March 11, 2021, 9:51pm UTC](https://forum.opencv.org/t/problem-with-cuda-cascadeclassifier/2115/3 "2021-03-11T21:51:31Z")

</div>

I have the .xml file in the same working directory and read it as follows:

```
     cv::cuda::GpuMat gframe, obj;
     std::vector<cv::Rect> rect;
     std::string cascadePath = "./cascade.xml"
     Ptr<cuda::CascadeClassifier> cascade = cv::cuda::CascadeClassifier::create(cascadePath);

```

It has worked for me on other occasions. Could it be generating the problem?

---

<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:** [March 12, 2021, 7:32am UTC](https://forum.opencv.org/t/problem-with-cuda-cascadeclassifier/2115/4 "2021-03-12T07:32:36Z")

</div>

does the code succeed if that file has some other, _known good_, content? (put a different file there)

note that “current working directory” is a property of the operating system process structure.

just use an absolute path. if that works, it was the CWD issue.

---

<div class="post-metadata">

**Author:** ![TatanC](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/tatanc/32/5591_2.png) [@TatanC](https://forum.opencv.org/u/TatanC)\
**Post date:** [March 12, 2021, 4:33pm UTC](https://forum.opencv.org/t/problem-with-cuda-cascadeclassifier/2115/5 "2021-03-12T16:33:55Z")

</div>

I tried with an absolute path and other .xml files and the result was the same.  
I have also tried the code without the cuda functions and the same .xml file and it works without problems.  
Is it possible that there is a formatting problem between the .xml files generated by opencv\_traincascade or cascade trainer GUI and what cuda::CascadeClasiffier is looking for?

Thank you very much for your answers.

---

<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:** [March 12, 2021, 8:45pm UTC](https://forum.opencv.org/t/problem-with-cuda-cascadeclassifier/2115/6 "2021-03-12T20:45:56Z")

</div>

> [@TatanC](#):
>
> I have also tried the code without the cuda functions and the same .xml file and it works without problems.

okay that’s good but now I’m out of ideas. your suspicion, the cuda version being somehow different, sounds plausible. I hope someone with more experience with the CUDA modules will see the issue.

---

<div class="post-metadata">

**Author:** ![TatanC](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/tatanc/32/5591_2.png) [@TatanC](https://forum.opencv.org/u/TatanC)\
**Post date:** [March 23, 2021, 12:06pm UTC](https://forum.opencv.org/t/problem-with-cuda-cascadeclassifier/2115/7 "2021-03-23T12:06:48Z")

</div>

I come back to say that I have solved the problem. It appears to be a compatibility problem. I used opencv version 3.4 and the opencv\_traincascade function with its -baseFormatSave argument to train with the old .xml format and it worked.

However, when comparing this classifier in its cpu version (with cv::detectMultiScale) versus gpu (with cv::cuda::detectMultiScale), the detection results are very different between them.
