在启用 OpenMP 的情况下编译 OpenCV,但应用程序仍在一个内核上运行

Compiled OpenCV with OpenMP enabled, but application still runs on one core

我在编译库时注意到我的 Python script using OpenCV is only using and maxing one of my cpu cores. I want to use the full potential of my cpu and not only 1/4th of it. As I researched, I found out that OpenCV is only using 1 core by default, and that to enable multi-threading, all I had to do was to enable OpenMP (WITH_OPENMP=ON)。所以这就是我所做的,但是当 运行 我的简单脚本时,我发现它仍然只使用一个内核,极大地限制了我的应用程序的性能。

我还需要做些什么(也许修改我的代码)才能使用我的 Raspberry Pi 的所有四个内核吗?

如果没有,可能是安装过程出了问题?我第一次安装 OpenCV, OpenMP wasn't enabled. Then after recompiling OpenCV with OpenMP, all I did was sudo make install and sudo ldconfig. I did not uninstall the previous OpenCV, guessing that installing the new OpenCV with OpenMP 会覆盖它。

感谢任何见解!!

干杯

编辑:

我的脚本的目标是从网络摄像头捕获帧并将它们同时写入 SD 卡。

import numpy as np
import cv2
import time

CV_CAP_PROP_FPS = 5 #https://github.com/Itseez/opencv/blob/e3ae36dcb3c1d523802f8642e5c3984db43637c4/modules/python/src2/defs

startTime = time.time()
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('output.div', fourcc, cap.get(CV_CAP_PROP_FPS), (640, 480))
nbOfFrames = 1

try:
    while(cap.isOpened()):
        ret, frame = cap.read()

        if ret == True:
            out.write(frame)
            print(time.time() - startTime)
            print(nbOfFrames)
            nbOfFrames += 1
except KeyboardInterrupt:
    cap.release()
    out.release()

请注意,这两点我可能都错了,因为我问题的评论部分中的@Jason 能够 运行 我的脚本在所有四个内核上(甚至没有编译 TBB 或 OpenMP,这很奇怪)。

所以我现在找到的使用所有四个内核的唯一解决方案是手动对我的代码进行线程化。如果我找到自动解决方案(即 OpenCV 内部实施的解决方案),我将更新此答案。

如果您有兴趣用 TBB 构建 OpenCV,您可以这样做:

$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_C_EXAMPLES=ON \
    -D BUILD_TBB=ON \
    -D WITH_TBB=ON \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
    -D BUILD_EXAMPLES=ON ..

使用 TBBOpenCV 3.0 编译时出现错误,但此 GitHub 票证 中有一个有效的解决方案很不错。享受吧!