Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cv2.cuda_CascadeClassifier in python

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

like image 274
yuri Avatar asked Jan 20 '26 04:01

yuri


1 Answers

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.

like image 184
Breno Alef Avatar answered Jan 21 '26 18:01

Breno Alef



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!