3

I am attempting to use the Haarclassifier from opencv cuda, for this I found the object cv.cuda_CascadeClassifier. However, assigning cv.cuda_CascadeClassifier() to a variable spit the following error:

this object has no ''load'' attribute. I could successfully verify it by printing their dir() print(dir(cv.cuda_CascadeClassifier)).

Is there any other way to call this object or did anyone effectively exploite the cascadeclassifier with opencv cuda?

thx

talonmies
  • 70,661
  • 34
  • 192
  • 269
yuri
  • 99
  • 8

3 Answers3

2

The lack of documentation for the python API really is a pain. Speaking about the version 4.5 of OpenCV, you have to call the create method when reading a xml cascade file or it'll yield segmentation fault when trying to detect. In my experience you'll also need to convert to gray scale or it will yield (-215:Assertion failed) src.type() == CV_8UC1.

Here's my working code on OpenCV 4.5.1:

import cv2

img = cv2.imread('./img_sample.jpg')
cascade = cv2.cuda_CascadeClassifier.create('./cascade_file.xml')
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cuFrame = cv2.cuda_GpuMat(gray_img)
result = cascade.detectMultiScale(cuFrame).download() # download() gets the result as UMat

if result is not None:
    print(result[0])

I hope this answers your question about the cuda version usage.

Pratibha
  • 1,730
  • 7
  • 27
  • 46
Breno Alef
  • 21
  • 3
0

This is most likely due to the use of a version of openCV between 4.0.0 and 4.3.0, In those versions cuda_CascadeClassifier was disabled. In 4.4.0 This functionallity was brought back. (https://github.com/opencv/opencv_contrib/pull/2554/files)

Even though this seems to work fine in C++ it gives me a segmentation fault using the python wrapper using the following code:

classifier_cuda = cv2.cuda_CascadeClassifier('cascades_file.xml')
while True:
    success, frame = vidcap.read()
    cuFrame = cv2.cuda_GpuMat(frame)
    result = classifier_cuda.detectMultiScale(cuFrame)
    print (result) 

Any solutions?

Jop Knoppers
  • 676
  • 1
  • 10
  • 22
0

As already Breno Alef wrote, the problem of OpenCV for Python is the lack of documentation and also of some code examples, which makes difficult to understand how to write correctly Python code to use OpenCV.

Looking at the documentation of the cuda_CascadeClassifier or using the Python built-in function help() you can see that the cuda_CascadeClassifier is a subclass of cv.Algorithm, which contains the load() method, but the problem is the way cuda_CascadeClassifier works is a bit different from the Cascade class declared in opencv2/objdetect.hpp.

Simone
  • 146
  • 2
  • 13