Giter Site home page Giter Site logo

neerajgulia / python-opencv-cuda Goto Github PK

View Code? Open in Web Editor NEW
112.0 5.0 20.0 14.4 MB

custom opencv_contrib module which exposes opencv cuda optical flow methods with python bindings

License: GNU General Public License v3.0

CMake 0.95% C++ 60.04% Python 39.01%
opencv-python opencv-cuda opencv-python-cuda python-cuda cuda-opencv3 opencv3 cuda

python-opencv-cuda's Introduction

python-opencv-cuda

Solution

  1. Create custom opencv_contrib module
  2. Write C++ code to wrap the OpenCV CUDA method
  3. Using OpenCV python bindings, expose your custom method
  4. Build opencv with opencv_contrib
  5. Run python code to test

Steps to create the build

Unzip the source:

  1. opencv source code: https://github.com/opencv/opencv/archive/3.4.2.zip
  2. opencv_contrib source code: https://github.com/opencv/opencv_contrib/archive/3.4.2.zip

Create custom module

  1. Copy the folder named "pythoncuda" (inside c++ folder) to: opencv_contrib/modules

Build opencv using following cmake command

  1. create build directory inside the opencv folder, cd to the build directory
  2. cmake (I used anaconda3 with environment named as: tensorflow_p36 (with python 3.6))
cmake \
-DCMAKE_BUILD_TYPE=RELEASE \
-DWITH_CUDA=ON \
-DCMAKE_INSTALL_PREFIX="/home/$USER/anaconda3/envs/tensorflow_p36" \
-DOPENCV_EXTRA_MODULES_PATH="../../opencv_contrib-3.4.2/modules" \
-DINSTALL_PYTHON_EXAMPLES=OFF \
-DINSTALL_C_EXAMPLES=OFF \
-DBUILD_SHARED_LIBS=OFF \
-DBUILD_DOCS=OFF \
-DBUILD_TESTS=OFF \
-DBUILD_EXAMPLES=OFF \
-DBUILD_PERF_TESTS=OFF \
-DBUILD_opencv_dnn=OFF \
-DTINYDNN_USE_NNPACK=OFF \
-DTINYDNN_USE_TBB=ON \
-DTINYDNN_USE_OMP=ON \
-DENABLE_FAST_MATH=ON \
-DWITH_OPENMP=ON \
-DWITH_TBB=ON \
-DWITH_JPEG=OFF \
-DWITH_IPP=OFF \
-DMKL_WITH_TBB=ON \
-DMKL_WITH_OPENMP=ON \
-DBUILD_opencv_python2=OFF \
-DPYTHON_EXECUTABLE="/home/$USER/anaconda3/envs/tensorflow_p36/bin/python" \
-DPYTHON_LIBRARY="/home/$USER/anaconda3/envs/tensorflow_p36/lib/python3.6" \
-DPYTHON3_LIBRARY="/home/$USER/anaconda3/envs/tensorflow_p36/lib/python3.6" \
-DPYTHON3_EXECUTABLE="/home/$USER/anaconda3/envs/tensorflow_p36/bin/python" \
-DPYTHON3_INCLUDE_DIR="/home/$USER/anaconda3/envs/tensorflow_p36/include/python3.6m" \
-DPYTHON3_INCLUDE_DIR2="/home/$USER/anaconda3/envs/tensorflow_p36/include" \
-DPYTHON3_NUMPY_INCLUDE_DIRS="/home/$USER/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/numpy/core/include" \
-DPYTHON3_INCLUDE_PATH="/home/$USER/anaconda3/envs/tensorflow_p36/include/python3.6m" \
-DPYTHON3_LIBRARIES="/home/$USER/anaconda3/envs/tensorflow_p36/lib/libpython3.6m.so" \
..
  1. make
  2. sudo make install
  3. sudo ldconfig

Test the code

  1. Activate conda environment
  2. Go to folder: python/ and execute the cpu-opt_flow.py and gpu-opt_flow.py python files
python cpu-opt_flow.py
python gpu-opt_flow.py 

Output at my end:

total time in optical flow CPU processing: 74.15 sec, for: 794 frames. FPS: 10.71

total time in optical flow GPU processing: 21.98 sec, for: 794 frames. FPS: 36.12

Harware configuration:

python-opencv-cuda's People

Contributors

malikashish27 avatar neerajgulia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

python-opencv-cuda's Issues

Compiled version?

Hello,
I am not proficient in C++ so was wondering if you have the final compiled version that I can use to install in my Python environment somehow?
I tried the steps but they didn't work for me.
Thank you

pyopencv_generated_include.h not found

Thanks for adding this tutorial.

I was able to cmake and make, following your instructions, however the process failed during
sudo make install with the error message:

/home/user/src/opencv-3.4.2/modules/python/src2/cv2.cpp:30:40: fatal error: pyopencv_generated_include.h: No such file or directory
compilation terminated.

This is from one fo your files. Any idea why that file was not generated?

cv2.cv2 has no attribute 'pythoncuda' ?

(DRenv) $USER@pop-os:~/Workspace/FYP/DR/python-opencv-cuda/python$ python cpu-opt_flow.py
Traceback (most recent call last):
File "cpu-opt_flow.py", line 4, in
cm.findOpticalFlow('video/vtest.avi', 'video/cpu_output.avi', False, False)
File "/home/muhdlaziem/Workspace/FYP/DR/python-opencv-cuda/python/common.py", line 63, in findOpticalFlow
flow = cv2.pythoncuda.cpuOpticalFlowFarneback(g_prev, g_next, None, 0.5, 3, 15, 3, 5, 1.2, 0)
AttributeError: module 'cv2.cv2' has no attribute 'pythoncuda'

(DRenv) $USER@pop-os:~/Workspace/FYP/DR/python-opencv-cuda/python$ python gpu-opt_flow.py
Traceback (most recent call last):
File "gpu-opt_flow.py", line 4, in
cm.findOpticalFlow('video/vtest.avi', 'video/cpu_output.avi', True, False)
File "/home/muhdlaziem/Workspace/FYP/DR/python-opencv-cuda/python/common.py", line 60, in findOpticalFlow
flow = cv2.pythoncuda.gpuOpticalFlowFarneback(g_prev, g_next, None, 0.5, 3, 15, 3, 5, 1.2, 0)
AttributeError: module 'cv2.cv2' has no attribute 'pythoncuda'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.