0

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:

  • Quadro P620
  • Debian 6.3.0-18
  • Python 3.5.3
  • OpenCV 4.5.0 build with CUDA=ON, CUDNN=ON

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].

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Jop Knoppers
  • 676
  • 1
  • 10
  • 22
  • Try giving the full path to the `CascadeClassifier` like `C:\User\..\..\opencv\data\haarcascades\cascade_file.xml` – Ahmet Sep 01 '20 at 15:40
  • try giving absolute pathes to both, video and cascade files. – Micka Sep 01 '20 at 16:03
  • Thanks @Ahmet and Micka but I already tried that didn't work unfortunately – Jop Knoppers Sep 01 '20 at 17:50
  • Did you get `cascade_file.xml` from the internet? I have the same problem when I use the `çxml` from the internet. It seems `.xml` only from the `/opencv/data/haarcascades` directory works with the `detectMultiScale`. – Ahmet Sep 01 '20 at 17:58
  • @Ahmet no it is a custom cascade, tried both the new and old layout – Jop Knoppers Sep 01 '20 at 18:00

1 Answers1

1

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)

kmetz
  • 26
  • 3