Jop Knoppers
Jop Knoppers

Reputation: 714

Cuda CascadeClassifier detectMultiScale output is unreadable

I am trying to do cascade classification using CUDA with openCV4.4 however when I run the detectMultiScale function it gives me a segmentation fault. What am I doing wrong?

There is limited documentation from openCV for CUDA in python, making it difficult to find the right procedure to do cascade classification using CUDA.

My system:

The code I came up with:

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

classifier_cuda and cuda_frame are respectively recognized as <cuda_GpuMat 0x7fffa9446d10> <cuda_CascadeClassifier 0x7fffa9446cf0>

This was resolved by changing the code to:

classifier_cuda = cv2.cuda.CascadeClassifier_create('model.xml')
while True:
    success, frame = vidcap.read()
    cuFrame = cv2.cuda_GpuMat(frame)
    output2 = cv2.cuda_GpuMat()
    output = classifier_cuda.detectMultiScale(cuFrame, output2)
    
    # And then its unclear what to use to get the detections
    # I Tried:
    final = classifier_cuda.convert(output)
    # And:
    final = classifier_cuda.convert(output2)
    # And:
    final = output.download()
    # And:
    final = output2.download()

The problem now is that the result is all ways empty. So, how should I extract the data from my detectmultiscale? I need to have a list of boundingboxes [x,y,w,h].

Upvotes: 0

Views: 1114

Answers (1)

kmetz
kmetz

Reputation: 26

output.download() actually works for me, but will be None if nothing was detected.

I think your issue is that you need to convert frames to COLOR_BGR2GRAY.

This should work:

source = cv2.VideoCapture('video_file.mp4')
classifier = cv2.cuda.CascadeClassifier_create('model.xml')

while True:
    success, frame = source.read()

    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gpu_frame = cv2.cuda_GpuMat(gray_frame)
    gpu_result = classifier.detectMultiScale(gpu_frame) # cuda_GpuMat
    result = gpu_result.download() # UMat

    if result is not None:
        for item in result[0]:
            (x, y, w, h) = item
            print(x, y, w, h)

(OpenCV 4.4)

Upvotes: 1

Related Questions