7

I used to use OpenCV on Raspberry for different projects (face recognition, sync cam, stitcher etc...). I bought a raspberry Pi 2 for the advantages given by the new hardware config.

However, I could not use the 4 cores for processing for example the stitching of images. When I perform let's say the stitching of 3 pictures, in X the load meter give 25% of load (1 core). When I launch two scripts of stitching with 2 pairs of 2 pictures, it is still 25% load on the CPU.

Is this possible to use OpenCV and operate the instructions on each core or does it need a modification of OpenCV core functions? Could it be possible to use, for example with Python, the libraries that enable multiprocessing on several raspberry but on the different cores of on Raspberry Pi 2?

  • If you are referring to completely separate processes, which it sounds like you are, you should eventually be able to run enough of them to max out all four cores unless there is some significant I/O bottleneck they all share. I don't know enough about openCV to say what that could be -- does it make use of the GPU for this? You might check with some other tool (e.g. htop) just to confirm that the system is really not making the most of things. – goldilocks Mar 18 '15 at 16:02
3

You should check out the multiprocessing library. If you have parts of you program that you can run in parallel, like processing multiple images and detecting a faces, you can simply define a process Pool. By default a pool will start as many processes simultaneously as you have processors. You can also define the pool size and fix it to 4.

e.g:

from multiprocessing import Pool
from glob import glob

def detect_face(file_name):
    ...

if __name__ == '__main__':
    with Pool() as p:
       p.map(detect_face, glob('*.jpg'))

You should also checkout sherlock, a collection of example on how to combine multiprocessing and opencv.

Limitations:

The algorithms in OpenCV itself are not easily executed in multiple processes. As OpenCV is written in C++, their execution depends highly on their implementation.

3

If you compile the OpenCV library with the OpenMP option active in the cmake file (it is off by default), many algorithms will use multiple cores automatically, without you needing to change anything in your source code. In my case I managed to achieve an up to 4.5x increase in performance. Yes, the Raspberry Pi 2 only has 4 cores, but if you do heavy number-crunching, a good division of the tasks can significantly reduce waiting times. For shorter algorithms don't expect much more than a 3x speedup.

Take care, with compiling the OpenMP libraries there is a bug in the libpng package. If the make halts with an error because libpng cannot find zlib, a quick and dirty solution can be that you just copy the source of zlib into the libpng source folder (or turn libpng off, if you don't need it).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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