OpenCV 3.2 for python with CUDA in Windows
Asked Answered
S

1

0

I build opencv3.2 with cmake and Visual Studio 12 (2013) with CUDA support and python3.5. All the libraries were build successfully and I can run opencv commands with python.

Here is the output from the command print(cv2.getBuildInformation())

General configuration for OpenCV 3.2.0 =====================================
  Version control:               unknown

  Extra modules:
    Location (extra):            D:/OpenCV/opencv_contrib/modules
    Version control (extra):     unknown

  Platform:
    Timestamp:                   2017-06-09T14:07:41Z
    Host:                        Windows 10.0.14393 AMD64
    CMake:                       3.9.0-rc2
    CMake generator:             Visual Studio 12 2013 Win64
    CMake build tool:            C:/Program Files (x86)/MSBuild/12.0/bin/MSBuild.exe
    MSVC:                        1800

  C/C++:
    Built as dynamic libs?:      YES
    C++ Compiler:                D:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/x86_amd64/cl.exe  (ver 18.0.21005.1)
    C++ flags (Release):         /DWIN32 /D_WINDOWS /W4 /GR /EHa  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /arch:AVX2 /Oi  /wd4127 /wd4251 /wd4324 /wd4275 /wd4589 /MP4  /MD /O2 /Ob2 /DNDEBUG  /Zi
    C++ flags (Debug):           /DWIN32 /D_WINDOWS /W4 /GR /EHa  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /arch:AVX2 /Oi  /wd4127 /wd4251 /wd4324 /wd4275 /wd4589 /MP4  /MDd /Zi /Ob0 /Od /RTC1
    C Compiler:                  D:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/x86_amd64/cl.exe
    C flags (Release):           /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /arch:AVX2 /Oi    /MP4  /MD /O2 /Ob2 /DNDEBUG  /Zi
    C flags (Debug):             /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /arch:AVX2 /Oi    /MP4  /MDd /Zi /Ob0 /Od /RTC1
    Linker flags (Release):      /machine:x64  /INCREMENTAL:NO  /debug
    Linker flags (Debug):        /machine:x64  /debug /INCREMENTAL
    ccache:                      NO
    Precompiled headers:         YES
    Extra dependencies:          comctl32 gdi32 ole32 setupapi ws2_32 vfw32 C:/Users/Ramesh/Anaconda3/Library/lib/hdf5.lib cudart nppc nppi npps cublas cufft -LIBPATH:C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v8.0/lib/x64
    3rdparty dependencies:       zlib libjpeg libwebp libpng libtiff libjasper IlmImf libprotobuf

  OpenCV modules:
    To be built:                 cudev core cudaarithm flann hdf imgproc ml reg surface_matching video cudabgsegm cudafilters cudaimgproc cudawarping dnn fuzzy imgcodecs photo shape videoio cudacodec highgui objdetect plot ts xobjdetect xphoto bgsegm bioinspired dpm face features2d line_descriptor saliency text calib3d ccalib cudafeatures2d cudalegacy cudaobjdetect cudaoptflow cudastereo datasets rgbd stereo superres tracking videostab xfeatures2d ximgproc aruco optflow phase_unwrapping stitching structured_light python3
    Disabled:                    world contrib_world
    Disabled by dependency:      -
    Unavailable:                 java python2 viz cnn_3dobj cvv freetype matlab sfm

  Windows RT support:            NO

  GUI:
    QT:                          NO
    Win32 UI:                    YES
    OpenGL support:              NO
    VTK support:                 NO

  Media I/O:
    ZLib:                        build (ver 1.2.8)
    JPEG:                        build (ver 90)
    WEBP:                        build (ver 0.3.1)
    PNG:                         build (ver 1.6.24)
    TIFF:                        build (ver 42 - 4.0.2)
    JPEG 2000:                   build (ver 1.900.1)
    OpenEXR:                     build (ver 1.7.1)
    GDAL:                        NO
    GDCM:                        NO

  Video I/O:
    Video for Windows:           YES
    DC1394 1.x:                  NO
    DC1394 2.x:                  NO
    FFMPEG:                      YES (prebuilt binaries)
      avcodec:                   YES (ver 57.64.101)
      avformat:                  YES (ver 57.56.100)
      avutil:                    YES (ver 55.34.100)
      swscale:                   YES (ver 4.2.100)
      avresample:                YES (ver 3.1.0)
    GStreamer:                   NO
    OpenNI:                      NO
    OpenNI PrimeSensor Modules:  NO
    OpenNI2:                     NO
    PvAPI:                       NO
    GigEVisionSDK:               NO
    DirectShow:                  YES
    Media Foundation:            NO
    XIMEA:                       NO
    Intel PerC:                  NO

  Parallel framework:            Concurrency

  Other third-party libraries:
    Use IPP:                     9.0.1 [9.0.1]
         at:                     D:/OpenCV/build2/3rdparty/ippicv/ippicv_win
    Use IPP Async:               NO
    Use Lapack:                  NO
    Use Eigen:                   YES (ver 3.3.3)
    Use Cuda:                    YES (ver 8.0)
    Use OpenCL:                  YES
    Use OpenVX:                  NO
    Use custom HAL:              NO

  NVIDIA CUDA
    Use CUFFT:                   YES
    Use CUBLAS:                  YES
    USE NVCUVID:                 NO
    NVIDIA GPU arch:             20 30 35 37 50 52 60 61
    NVIDIA PTX archs:
    Use fast math:               YES

  OpenCL:                        <Dynamic loading of OpenCL library>
    Include path:                D:/OpenCV/sources/3rdparty/include/opencl/1.2
    Use AMDFFT:                  NO
    Use AMDBLAS:                 NO

  Python 2:
    Interpreter:                 NO

  Python 3:
    Interpreter:                 C:/Users/Ramesh/Anaconda3/python.exe (ver 3.5.3)
    Libraries:                   C:/Users/Ramesh/Anaconda3/libs/python35.lib (ver 3.5.3)
    numpy:                       C:/Users/Ramesh/Anaconda3/lib/site-packages/numpy/core/include (ver 1.13.0)
    packages path:               C:/Users/Ramesh/Anaconda3/Lib/site-packages

  Python (for build):            C:/Users/Ramesh/Anaconda3/python.exe

  Java:
    ant:                         NO
    JNI:                         C:/Program Files/Java/jdk1.8.0_112/include C:/Program Files/Java/jdk1.8.0_112/include/win32 C:/Program Files/Java/jdk1.8.0_112/include
    Java wrappers:               NO
    Java tests:                  NO

  Matlab:                        Matlab not found or implicitly disabled

  Documentation:
    Doxygen:                     D:/Program Files/doxygen/bin/doxygen.exe (ver 1.8.13)

  Tests and samples:
    Tests:                       NO
    Performance tests:           NO
    C/C++ Examples:              NO

  Install path:                  D:/OpenCV/build2/install

  cvconfig.h is in:              D:/OpenCV/build2
-----------------------------------------------------------------



But I can't use functions in cv2.cuda package. When I run help(cv2.cuda)

Help on module cv2.cuda in cv2:

NAME
    cv2.cuda

DATA
    SURF_CUDA_ANGLE_ROW = 5
    SURF_CUDA_HESSIAN_ROW = 6
    SURF_CUDA_LAPLACIAN_ROW = 2
    SURF_CUDA_OCTAVE_ROW = 3
    SURF_CUDA_ROWS_COUNT = 7
    SURF_CUDA_SIZE_ROW = 4
    SURF_CUDA_X_ROW = 0
    SURF_CUDA_Y_ROW = 1

FILE
    (built-in)

The result says that there are no functions in the module cv2.cuda


My Questions are,

  1. Is there a way to compile opencv3.2 with CUDA support for python3.5..? As stated in this question, it was not possible in opencv 2.4.7
  2. If there is no way, can I bind c++ functions with python..? I went through opencv doc and I couldn't understand how to extend new modules to python. When I google it, I get some answers about writing wrappers (all answers are older than 4 years) but I couldn't understand how to bind data structures between 2 languages like c++ Mat with numpy.ndarray.

Any help will be appreciated!!

Shelton answered 10/6, 2017 at 12:10 Comment(0)
S
1

I was able to find a way to get cuda support for OpenCV python by following the tutorial in here (I did not test that). As far as I know GPUMat is not available. But UMat is available for python which can be used to access OpenCL.
As described in this answer, UMat is

A unified abstraction cv::UMat that enables the same APIs to be implemented using CPU or OpenCL code, without a requirement to call OpenCL accelerated version explicitly. These functions use an OpenCL-enabled GPU if exists in the system, and automatically switch to CPU operation otherwise.

As described in OpenCL section here, you just have to change normal matrices (in python np.ndarrays) to UMats (in python cv2.UMat) to get the OpenCL support.

But what I do when I need cuda support is, I write that part as a function in c++ with cuda support. I make that function to take a cv::Mat image as the argument and return a cv::Mat as result.

Then write a python wrapper for that function as described here.
That is a bit old code. But it worked for me on python 3.5 and python 2.7 with OpenCV 3.4

Shelton answered 21/6, 2018 at 12:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.