12

I'm using an OpenCV binding library for Go and trying to asynchronously detect objects in 10 images but keep getting this panic. Detecting only 4 images never fails.

var wg sync.WaitGroup

for j := 0; j < 10; j++ {
    wg.Add(1)
    go func(i int) {
        image := opencv.LoadImage(strconv.Itoa(i) + ".jpg")
        defer image.Release()

        faces := cascade.DetectObjects(image)
        fmt.Println((len(faces) > 0))
        wg.Done()
    }(j)
}

wg.Wait()

I'm fairly new to OpenCV and Go and trying to figure where the problem lies. I'm guessing some resource is being exhausted but which one.

7
  • Does using runtime.LockOSThread() at the top of each goroutine fix it?
    – OneOfOne
    Commented Sep 10, 2014 at 21:52
  • No, that makes no difference.
    – bradley
    Commented Sep 10, 2014 at 21:55
  • Do you run out of memory by any chance? according to your panic it dies in C.cvHaarDetectObjects right after C.cvCreateMemStorage(0), so maybe it can't allocate enough memory. (you're getting segmentation fault")
    – OneOfOne
    Commented Sep 10, 2014 at 21:58
  • Think I have enough memory, I've got 1gb free and the total size of all images is 1.8mb.
    – bradley
    Commented Sep 10, 2014 at 22:07
  • 1
    Ok thanks. I just realised too that if I try to detect the same image (but 10 times in a row) all is fine.
    – bradley
    Commented Sep 10, 2014 at 22:25

3 Answers 3

12
+50

Each time you call DetectObjects the underlying implementation of OpenCV builds a tree of classifiers and stores them inside of cascade. You can see part of the handling of these chunks of memory at https://github.com/Itseez/opencv/blob/master/modules/objdetect/src/haar.cpp line 2002

Your original code only had one cascade as a global. Each new go routine call DetectObjects used the same root cascade. Each new image would have free'd the old memory and rebuilt a new tree and eventually they would stomp on each other's memory use and cause a dereference through 0, causing the panic.

Moving the allocation of the cascade inside the goroutine allocates a new one for each DetectObject call and they do not share any memory.

The fact that it never happened on 4 images, but failed on 5 images is the nature of computing. You got lucky with 4 images and never saw the problem. You always saw the problem on 5 images because exactly the same thing happened each time (regardless of concurrency).

Repeating the same image multiple times doesn't cause the cascade tree to be rebuilt. If the image didn't change why do the work ... an optimization in OpenCV to handle multiple images frames.

0
3

The problem seemed to be having the cascade as a global variable.

Once I moved
cascade := opencv.LoadHaarClassifierCascade("haarcascade_frontalface_alt.xml")
into the goroutine all was fine.

2

You are not handling for a nil image

image := opencv.LoadImage(strconv.Itoa(i) + ".jpg")
if image == nil {
    // handle error
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.