Can CascadeClassifier.detectMultiScale be run in several threads at once?

81 views
Skip to first unread message

Benjamin Xiao

unread,
Aug 22, 2020, 3:39:25 PM8/22/20
to javacv
Hello again, fellow JavaCV users!

So I am using CascadeClassifier to do object detection in a video feed. Currently I am doing upper body, full body, and face detection in 3 separate CascadeClassifier instances. I am trying to run each detection in a separate CPU thread. However, that ends up being slower than if I run them sequentially. It almost seems like there's some lock behind calls to CascadeClassifier.detectMultiScale that's only allowing one call to run at a time.

Here is how I am testing this:
// Create classifier jobs
var classifierJobs = getClassifiers().stream()
        .map((classifier) -> {
            Callable<List<ClassifierResult>> classifierJob = () -> {
                var detectedObjs = new RectVector();
                classifier.detectMultiScale(grayMat,
                    detectedObjs,
                    scaleFactor,
                    minNeighbors,
                    CASCADE_SCALE_IMAGE,
                    new Size(minSize, minSize),
                    new Size()
                );
                return detectedObjs;
            };
            return classifierJob;
        })
        .collect(Collectors.toList());

// Execute jobs with ExecutorService instance
ExecutorService classifierPool = Executors.newCachedThreadPool();
classifierPool.invokeAll(classifierJobs)

As you can see, I am creating a Callable for each CascadeClassifier instance. Inside, there's a call to detectMultiScale. I feed all of these Callables to a cached thread pool via invokeAll.

This code runs but it is slow. If I instead define classifierPool as newSingleThreadExecutor() then the code runs much faster, albeit in a sequential fashion on a single thread.

So what's the deal here? Am I running into threading overhead? I thought using a thread pool would decrease some of the overhead particularly in terms of thread creation and destruction.

Or is there some kind of mutex lock behind the scenes that I am not aware of that's causing my multithreaded code to slow down?

Any help appreciated. Thanks in advance!

Ben

Samuel Audet

unread,
Aug 22, 2020, 6:11:17 PM8/22/20
to jav...@googlegroups.com, Benjamin Xiao
OpenCV already uses all threads for each call. If you'd like to disable multithreading in OpenCV, you can try to call setNumThreads(1).

Samuel

Benjamin Xiao

unread,
Aug 22, 2020, 11:29:27 PM8/22/20
to javacv
Thanks Samuel! Did not know that detectMultiScale was already multithreaded. That probably explains why I am running into the issues I am experiencing. I guess I'll implement a job queue and feed these classifier instances one by one.

Ben

Reply all
Reply to author
Forward
0 new messages