欢迎访问我的网站,希望内容对您有用,感兴趣的可以加入免费知识星球。

OpenCV使能CUDA加速

OpenCV 迷途小书童 4年前 (2019-12-17) 9671次浏览 0个评论

软硬件环境

  • ubuntu 18.04 64bit
  • NVIDIA GTX 1070Ti 8G
  • anaconda with python 3.6
  • opencv 3.4.3
  • cuda 9.0

视频看这里

安装显卡驱动和CUDA

ubuntu下安装的方法之前的博文中就有介绍,这里不再赘述,请参考 https://xugaoxiang.com/2019/12/13/ubuntu-cuda/

编译opencv

我这里的python环境anaconda3,如果你不是很熟悉的话,可以参考这篇 https://xugaoxiang.com/2019/12/08/anaconda/

mkdir opencv_cuda
cd opencv_cuda
git clone https://github.com/opencv/opencv.git
cd opencv
git checkout -b 3.4.3

cd ..
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout -b 3.4.3

cd ../opencv
mkdir build
cd build

# 根据自己的需要增减,如果你不是在anaconda中使用,CMAKE_INSTALL_PREFIX参数可以设置成/usr/local
cmake -D CMAKE_BUILD_TYPE=RELEASE -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules -D BUILD_EXAMPLES=ON -D BUILD_opencv_python2=OFF -D BUILD_opencv_python3=ON -D WITH_FFMPEG=ON -D WITH_LIBV4L=ON -D WITH_GTK=ON -D WITH_OPENGL=ON -D WITH_ZLIB=ON -D BUILD_PNG=ON -D BUILD_JPEG=ON -D BUILD_TIFF=ON -D BUILD_TIFF=ON -D WITH_CUDA=ON -D WITH_OPENCL=ON -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D WITH_LAPACK=OFF -DCMAKE_INSTALL_PREFIX=$(python -c "import sys; print(sys.prefix)") -DPYTHON3_EXECUTABLE=$(which python) -DPYTHON3_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") -DPYTHON3_PACKAGES_PATH=$(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") ..

make -j8
sudo make install
sudo ldconfig

cmake结束后注意看下CUDApython3相关的输出

--   NVIDIA CUDA:                   YES (ver 9.0, CUFFT CUBLAS NVCUVID)
--     NVIDIA GPU arch:             62
--     NVIDIA PTX archs:
-- 
--   OpenCL:                        YES (no extra features)
--     Include path:                /home/longjing/Work/opencv_cuda/opencv/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
-- 
--   Python 3:
--     Interpreter:                 /home/longjing/anaconda3/bin/python3 (ver 3.6.6)
--     Libraries:                   NO
--     numpy:                       /home/longjing/anaconda3/lib/python3.6/site-packages/numpy/core/include (ver 1.12.1)
--     packages path:               lib/python3.6/site-packages

我在make的过程中出现了如下错误

[ 24%] Generating perf_precomp.hpp.gch/opencv_perf_video_Release.gch
[ 24%] Built target pch_Generate_opencv_stitching
[ 24%] Generating test_precomp.hpp.gch/opencv_test_videostab_Release.gch
[ 24%] Built target pch_Generate_opencv_test_stitching
[ 24%] Built target pch_Generate_opencv_video
[ 24%] Generating perf_precomp.hpp.gch/opencv_perf_superres_Release.gch
[ 24%] Generating precomp.hpp.gch/opencv_superres_Release.gch
[ 24%] Built target pch_Generate_opencv_test_video
[ 24%] Generating test_precomp.hpp.gch/opencv_test_superres_Release.gch
[ 24%] Built target pch_Generate_opencv_perf_stitching
[ 24%] Built target pch_Generate_opencv_perf_video
[ 24%] Built target pch_Generate_opencv_videostab
[ 24%] Building NVCC (Device) object modules/core/CMakeFiles/cuda_compile_1.dir/src/cuda/cuda_compile_1_generated_gpu_mat.cu.o
/home/longjing/Work/opencv_cuda/opencv/modules/core/src/cuda/gpu_mat.cu:47:2: error: #error "opencv_cudev is required"
 #error "opencv_cudev is required"
  ^
CMake Error at cuda_compile_1_generated_gpu_mat.cu.o.Release.cmake:219 (message):
  Error generating
  /home/longjing/Work/opencv_cuda/opencv/build/modules/core/CMakeFiles/cuda_compile_1.dir/src/cuda/./cuda_compile_1_generated_gpu_mat.cu.o

modules/core/CMakeFiles/opencv_core.dir/build.make:63: recipe for target 'modules/core/CMakeFiles/cuda_compile_1.dir/src/cuda/cuda_compile_1_generated_gpu_mat.cu.o' failed
make[2]: *** [modules/core/CMakeFiles/cuda_compile_1.dir/src/cuda/cuda_compile_1_generated_gpu_mat.cu.o] Error 1
CMakeFiles/Makefile2:1664: recipe for target 'modules/core/CMakeFiles/opencv_core.dir/all' failed
make[1]: *** [modules/core/CMakeFiles/opencv_core.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
[ 24%] Built target pch_Generate_opencv_superres
[ 24%] Built target pch_Generate_opencv_perf_superres
[ 24%] Built target pch_Generate_opencv_test_videostab
[ 24%] Built target pch_Generate_opencv_test_superres
Makefile:162: recipe for target 'all' failed
make: *** [all] Error 2

看日志应该是找不到外部的module, 这里有个很坑爹的地方,大家一定要注意,参数OPENCV_EXTRA_MODULES_PATH后面跟的路径千万不能有空格,这点也可以通过cmake的汇总信息中查看,对应字段是Extra modules。建议大家,不管是添加还是删除某种特性时,cmake后都去仔细看看这些信息,确认无误后再去make

-- HDF5: Using hdf5 compiler wrapper to determine C configuration
-- Module opencv_ovis disabled because OGRE3D was not found
-- No preference for use of exported gflags CMake configuration set, and no hints for include/library directories provided. Defaulting to preferring an installed/exported gflags CMake configuration if available.
-- Failed to find installed gflags CMake configuration, searching for gflags build directories exported with CMake.
-- Failed to find gflags - Failed to find an installed/exported CMake configuration for gflags, will perform search for installed gflags components.
-- Failed to find gflags - Could not find gflags include directory, set GFLAGS_INCLUDE_DIR to directory containing gflags/gflags.h
-- Failed to find glog - Could not find glog include directory, set GLOG_INCLUDE_DIR to directory containing glog/logging.h
-- Module opencv_sfm disabled because the following dependencies are not found: Eigen Glog/Gflags
-- Checking for module 'tesseract'
--   No package 'tesseract' found
-- Tesseract:   NO
-- HDF5: Using hdf5 compiler wrapper to determine C configuration
-- Registering hook 'INIT_MODULE_SOURCES_opencv_dnn': /home/longjing/xgx/opencv_cuda/opencv/modules/dnn/cmake/hooks/INIT_MODULE_SOURCES_opencv_dnn.cmake
CMake Error at modules/dnn/CMakeLists.txt:97 (message):
  CUDA backend for DNN module requires CC 5.3 or higher.  Please remove
  unsupported architectures from CUDA_ARCH_BIN option.

-- Configuring incomplete, errors occurred!
See also "/home/longjing/xgx/opencv_cuda/opencv/build/CMakeFiles/CMakeOutput.log".
See also "/home/longjing/xgx/opencv_cuda/opencv/build/CMakeFiles/CMakeError.log".

这个错误去修改build/CMakeCache.txt文件中的CUDA_ARCH_BIN,将低的版本号删除即可。

[ 37%] Building NVCC (Device) object modules/cudafilters/CMakeFiles/cuda_compile_1.dir/src/cuda/cuda_compile_1_generated_median_filter.cu.o
[ 37%] Linking CXX executable ../../bin/example_phase_unwrapping_unwrap
../../lib/libopencv_imgcodecs.so.4.0.0: undefined reference to `TIFFReadDirectory@LIBTIFF_4.0'
../../lib/libopencv_imgcodecs.so.4.0.0: undefined reference to `TIFFWriteEncodedStrip@LIBTIFF_4.0'
../../lib/libopencv_imgcodecs.so.4.0.0: undefined reference to `TIFFIsTiled@LIBTIFF_4.0'
../../lib/libopencv_imgcodecs.so.4.0.0: undefined reference to `TIFFOpen@LIBTIFF_4.0'
../../lib/libopencv_imgcodecs.so.4.0.0: undefined reference to `TIFFReadEncodedStrip@LIBTIFF_4.0'
../../lib/libopencv_imgcodecs.so.4.0.0: undefined reference to `TIFFSetField@LIBTIFF_4.0'
../../lib/libopencv_imgcodecs.so.4.0.0: undefined reference to `TIFFWriteScanline@LIBTIFF_4.0'
../../lib/libopencv_imgcodecs.so.4.0.0: undefined reference to `_TIFFfree@LIBTIFF_4.0'
../../lib/libopencv_imgcodecs.so.4.0.0: undefined reference to `TIFFGetField@LIBTIFF_4.0'
../../lib/libopencv_imgcodecs.so.4.0.0: undefined reference to `TIFFScanlineSize@LIBTIFF_4.0'
../../lib/libopencv_imgcodecs.so.4.0.0: undefined reference to `TIFFNumberOfStrips@LIBTIFF_4.0'
../../lib/libopencv_imgcodecs.so.4.0.0: undefined reference to `TIFFWriteDirectory@LIBTIFF_4.0'
../../lib/libopencv_imgcodecs.so.4.0.0: undefined reference to `TIFFSetWarningHandler@LIBTIFF_4.0'
../../lib/libopencv_imgcodecs.so.4.0.0: undefined reference to `_TIFFmalloc@LIBTIFF_4.0'
../../lib/libopencv_imgcodecs.so.4.0.0: undefined reference to `TIFFSetErrorHandler@LIBTIFF_4.0'
../../lib/libopencv_imgcodecs.so.4.0.0: undefined reference to `TIFFReadEncodedTile@LIBTIFF_4.0'
../../lib/libopencv_imgcodecs.so.4.0.0: undefined reference to `TIFFReadRGBATile@LIBTIFF_4.0'
../../lib/libopencv_imgcodecs.so.4.0.0: undefined reference to `TIFFReadScanline@LIBTIFF_4.0'
../../lib/libopencv_imgcodecs.so.4.0.0: undefined reference to `TIFFClose@LIBTIFF_4.0'
../../lib/libopencv_imgcodecs.so.4.0.0: undefined reference to `TIFFRGBAImageOK@LIBTIFF_4.0'
../../lib/libopencv_imgcodecs.so.4.0.0: undefined reference to `TIFFClientOpen@LIBTIFF_4.0'
../../lib/libopencv_imgcodecs.so.4.0.0: undefined reference to `TIFFReadRGBAStrip@LIBTIFF_4.0'
collect2: error: ld returned 1 exit status
modules/phase_unwrapping/CMakeFiles/example_phase_unwrapping_unwrap.dir/build.make:101: recipe for target 'bin/example_phase_unwrapping_unwrap' failed
make[2]: *** [bin/example_phase_unwrapping_unwrap] Error 1
CMakeFiles/Makefile2:5054: recipe for target 'modules/phase_unwrapping/CMakeFiles/example_phase_unwrapping_unwrap.dir/all' failed
make[1]: *** [modules/phase_unwrapping/CMakeFiles/example_phase_unwrapping_unwrap.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....

conda安装libtiff即可

conda install -c anaconda libtiff

由于网络的原因,在编译过程中有些包可能会下载不成功,比如ippicv_2019_lnx_intel64_general_20180723.tgz,遇到这种情况可以这样去解决,首先在源码目录

grep "ippicv_2019_lnx_intel64_general_20180723.tgz" . -r -n

命令输出的信息

(base) longjing@ai:~/xgx/opencv_cuda/opencv$ grep "ippicv_2019_lnx_intel64_general_20180723.tgz" . -r -n
./3rdparty/ippicv/ippicv.cmake:21:      set(OPENCV_ICV_NAME "ippicv_2019_lnx_intel64_general_20180723.tgz")
./build/CMakeDownloadLog.txt:2:#do_unpack "ippicv_2019_lnx_intel64_general_20180723.tgz" "c0bd78adb4156bbf552c1dfe90599607" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/32e315a5b106a7b89dbed51c28f8120a48b368b4/ippicv/ippicv_2019_lnx_intel64_general_20180723.tgz" "/home/longjing/xgx/opencv_cuda/opencv/build/3rdparty/ippicv"
./build/CMakeDownloadLog.txt:3:#cmake_download "/home/longjing/xgx/opencv_cuda/opencv/.cache/ippicv/c0bd78adb4156bbf552c1dfe90599607-ippicv_2019_lnx_intel64_general_20180723.tgz" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/32e315a5b106a7b89dbed51c28f8120a48b368b4/ippicv/ippicv_2019_lnx_intel64_general_20180723.tgz"
./build/download_with_wget.sh:1:mkdir -p $(dirname /home/longjing/xgx/opencv_cuda/opencv/.cache/ippicv/c0bd78adb4156bbf552c1dfe90599607-ippicv_2019_lnx_intel64_general_20180723.tgz) && wget -O "/home/longjing/xgx/opencv_cuda/opencv/.cache/ippicv/c0bd78adb4156bbf552c1dfe90599607-ippicv_2019_lnx_intel64_general_20180723.tgz" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/32e315a5b106a7b89dbed51c28f8120a48b368b4/ippicv/ippicv_2019_lnx_intel64_general_20180723.tgz"
./build/download_with_curl.sh:1:curl --create-dirs --output "/home/longjing/xgx/opencv_cuda/opencv/.cache/ippicv/c0bd78adb4156bbf552c1dfe90599607-ippicv_2019_lnx_intel64_general_20180723.tgz" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/32e315a5b106a7b89dbed51c28f8120a48b368b4/ippicv/ippicv_2019_lnx_intel64_general_20180723.tgz"

从输出信息中可以看到脚本是从地址https://raw.githubusercontent.com/opencv/opencv_3rdparty/32e315a5b106a7b89dbed51c28f8120a48b368b4/ippicv/ippicv_2019_lnx_intel64_general_20180723.tgz去下载的,我们可以手动去wget,然后自己把文件拷贝到对应的目录中去,比如这里的目录/home/longjing/xgx/opencv_cuda/opencv/.cache/ippicv/c0bd78adb4156bbf552c1dfe90599607-ippicv_2019_lnx_intel64_general_20180723.tgz

验证

通过导入cv2模块,查看方法cv2.getBuildInformation()的输出信息

longjing@FR:~/Work/opencv_cuda/opencv/build$ ipython
Python 3.6.6 | packaged by conda-forge | (default, Oct 12 2018, 14:08:43) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.0.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import cv2
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-c8ec22b3e787> in <module>()
----> 1 import cv2

ImportError: /usr/lib/x86_64-linux-gnu/libatk-1.0.so.0: undefined symbol: g_log_structured_standard

解决方法:

省事起见,我是直接更新anaconda了,防止类似的错误一再出现

conda update --all

我的环境中还出现了一些so找不到的情况,比如libhdf5,通过conda install *安装就可以解决了。

Python 3.6.6 | packaged by conda-forge | (default, Oct 12 2018, 14:08:43) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.0.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import cv2                                                                                                                                                                                                 

In [2]: cv2.getBuildInformation()                                                                                                                                                                                  
Out[2]: '\nGeneral configuration for OpenCV 4.0.0-pre =====================================\n  Version control:               4.0.0-beta-28-gdbed39a93\n\n  Extra modules:\n    Location (extra):            /home/longjing/Work/opencv_cuda/opencv_contrib/modules\n    Version control (extra):     4.0.0-beta-4-gfd63e177\n\n  Platform:\n    Timestamp:                   2018-10-26T02:12:24Z\n    Host:                        Linux 4.15.0-33-generic x86_64\n    CMake:                       3.10.2\n    CMake generator:             Unix Makefiles\n    CMake build tool:            /usr/bin/make\n    Configuration:               RELEASE\n\n  CPU/HW features:\n    Baseline:                    SSE SSE2 SSE3\n      requested:                 SSE3\n    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX\n      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX\n      SSE4_1 (5 files):          + SSSE3 SSE4_1\n      SSE4_2 (2 files):          + SSSE3 SSE4_1 POPCNT SSE4_2\n      FP16 (1 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX\n      AVX (6 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX\n      AVX2 (11 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2\n      AVX512_SKX (1 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_SKX\n\n  C/C++:\n    Built as dynamic libs?:      YES\n    C++ Compiler:                /usr/local/cuda-9.0/bin/c++  (ver 5.5.0)\n    C++ flags (Release):         -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffast-math -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG\n    C++ flags (Debug):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffast-math -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG\n    C Compiler:                  /usr/local/cuda-9.0/bin/cc\n    C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffast-math -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG\n    C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffast-math -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG\n    Linker flags (Release):      \n    Linker flags (Debug):        \n    ccache:                      NO\n    Precompiled headers:         YES\n    Extra dependencies:          m pthread cudart_static -lpthread dl rt nppc nppial nppicc nppicom nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cufft -L/usr/local/cuda-9.0/lib64 -L/usr/lib/x86_64-linux-gnu\n    3rdparty dependencies:\n\n  OpenCV modules:\n    To be built:                 aruco bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dpm face features2d flann freetype fuzzy gapi hdf hfs highgui img_hash imgcodecs imgproc java_bindings_generator line_descriptor ml objdetect optflow phase_unwrapping photo plot python3 python_bindings_generator reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab xfeatures2d ximgproc xobjdetect xphoto\n    Disabled:                    python2 world\n    Disabled by dependency:      -\n    Unavailable:                 cnn_3dobj cvv java js matlab ovis sfm viz\n    Applications:                tests perf_tests examples apps\n    Documentation:               NO\n    Non-free algorithms:         NO\n\n  GUI: \n    GTK+:                        YES (ver 3.22.30)\n      GThread :                  YES (ver 2.56.1)\n      GtkGlExt:                  NO\n    OpenGL support:              NO\n    VTK support:                 NO\n\n  Media I/O: \n    ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)\n    JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver 80)\n    WEBP:                        /usr/lib/x86_64-linux-gnu/libwebp.so (ver encoder: 0x020e)\n    PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.34)\n    TIFF:                        build (ver 42 - 4.0.9)\n    JPEG 2000:                   /home/longjing/anaconda3/lib/libjasper.so (ver 2.0.14)\n    OpenEXR:                     /usr/lib/x86_64-linux-gnu/libImath.so /usr/lib/x86_64-linux-gnu/libIlmImf.so /usr/lib/x86_64-linux-gnu/libIex.so /usr/lib/x86_64-linux-gnu/libHalf.so /usr/lib/x86_64-linux-gnu/libIlmThread.so (ver 2.2.0)\n    HDR:                         YES\n    SUNRASTER:                   YES\n    PXM:                         YES\n    PFM:                         YES\n\n  Video I/O:\n    DC1394:                      YES (ver 2.2.5)\n    FFMPEG:                      YES\n      avcodec:                   YES (ver 58.18.100)\n      avformat:                  YES (ver 58.12.100)\n      avutil:                    YES (ver 56.14.100)\n      swscale:                   YES (ver 5.1.100)\n      avresample:                YES (ver 3.7.0)\n    GStreamer:                   \n      base:                      YES (ver 1.14.1)\n      video:                     YES (ver 1.14.1)\n      app:                       YES (ver 1.14.1)\n      riff:                      YES (ver 1.14.1)\n      pbutils:                   YES (ver 1.14.1)\n    libv4l/libv4l2:              1.14.2 / 1.14.2\n    v4l/v4l2:                    linux/videodev.h linux/videodev2.h\n\n  Parallel framework:            pthreads\n\n  Trace:                         YES (with Intel ITT)\n\n  Other third-party libraries:\n    Intel IPP:                   2017.0.3 [2017.0.3]\n           at:                   /home/longjing/Work/opencv_cuda/opencv/build/3rdparty/ippicv/ippicv_lnx\n    Intel IPP IW:                sources (2017.0.3)\n              at:                /home/longjing/Work/opencv_cuda/opencv/build/3rdparty/ippicv/ippiw_lnx\n    Eigen:                       NO\n    Custom HAL:                  NO\n    Protobuf:                    build (3.5.1)\n\n  NVIDIA CUDA:                   YES (ver 9.0, CUFFT CUBLAS NVCUVID FAST_MATH)\n    NVIDIA GPU arch:             30 35 37 50 52 60 61 70\n    NVIDIA PTX archs:\n\n  OpenCL:                        YES (no extra features)\n    Include path:                /home/longjing/Work/opencv_cuda/opencv/3rdparty/include/opencl/1.2\n    Link libraries:              Dynamic load\n\n  Python 3:\n    Interpreter:                 /home/longjing/anaconda3/bin/python (ver 3.6.6)\n    Libraries:                   /home/longjing/anaconda3/lib/libpython3.6m.so (ver 3.6.6)\n    numpy:                       /home/longjing/anaconda3/lib/python3.6/site-packages/numpy/core/include (ver 1.15.3)\n    packages path:               /home/longjing/anaconda3/lib/python3.6/site-packages\n\n  Python (for build):            /usr/bin/python2.7\n    Pylint:                      /home/longjing/anaconda3/bin/pylint (ver: 3.6.6, checks: 162)\n\n  Java:                          \n    ant:                         NO\n    JNI:                         /usr/lib/jvm/java-8-openjdk-amd64/include /usr/lib/jvm/java-8-openjdk-amd64/include/linux /usr/lib/jvm/java-8-openjdk-amd64/include\n    Java wrappers:               NO\n    Java tests:                  NO\n\n  Install to:                    /home/longjing/anaconda3\n-----------------------------------------------------------------\n\n'   

从上面的输出信息可以看到python中的cv2模块已经使能了CUDA

在使用opencv进行视频处理的时候,碰到了问题

(Deep learning object detection in OpenCV:8725): GStreamer-CRITICAL **: 15:47:19.541: gst_element_get_state: assertion 'GST_IS_ELEMENT (element)' failed
Unable to stop the stream: Inappropriate ioctl for device

(Deep learning object detection in OpenCV:8725): GStreamer-CRITICAL **: 15:47:19.541: gst_element_make_from_uri: assertion 'gst_uri_is_valid (uri)' faile

这个一般都是由于ffmpeg引起的,如果是ubuntu环境就直接sudo apt install ffmpeg libv4l-dev一般就可以解决了,如果使用的是anaconda,就执行下面命令

conda install -c conda-forge ffmpeg

然后确认下ffmpeg的路径

longjing@FR:~$ which ffmpeg 
/home/longjing/anaconda3/bin/ffmpeg

最后进入到opencv目录,重新cmake,看看是不是已经把ffmpeg的特性加进去了

--   Video I/O:
--     DC1394:                      YES (ver 2.2.5)
--     FFMPEG:                      YES
--       avcodec:                   YES (ver 58.18.100)
--       avformat:                  YES (ver 58.12.100)
--       avutil:                    YES (ver 56.14.100)
--       swscale:                   YES (ver 5.1.100)
--       avresample:                YES (ver 3.7.0)
--     GStreamer:                   
--       base:                      YES (ver 1.14.1)
--       video:                     YES (ver 1.14.1)
--       app:                       YES (ver 1.14.1)
--       riff:                      YES (ver 1.14.1)
--       pbutils:                   YES (ver 1.14.1)
--     libv4l/libv4l2:              1.14.2 / 1.14.2
--     v4l/v4l2:                    linux/videodev.h linux/videodev2.h

如何加速

这里以在OpenCV中使用YOLOv3模型进行物体检测为例,具体可以看Darknet基本使用


# yolo v3的配置及weights文件
modelConfiguration = "yolov3-voc.cfg"
modelWeights = "voc.weights"

# opencv读取外部模型
net = cv2.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)

参考资料

喜欢 (1)

您必须 登录 才能发表评论!