Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use GPU with opencv-python

I'm trying to use opencv-python with GPU on windows 10.

I installed opencv-contrib-python using pip and it's v4.4.0.42, I also have Cuda on my computer and in path.

Anyway, here is a (simple) code that I'm trying to compile:

import cvlib as cv
from cvlib.object_detection import draw_bbox

bbox, label, conf = cv.detect_common_objects(img,confidence=0.5,model='yolov3-worker',enable_gpu=True)

output_image = draw_bbox(img, bbox, label, conf)

First, here is the line that tell me that tf is ok with cuda:

2020-08-26 5:51:55.718555: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudart64_101.dll

but when I try to use my GPU to analyse the image, here is what happen:

[ WARN:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-j8nxabm_\opencv\modules\dnn\src\dnn.cpp (1429) cv::dnn::dnn4_v20200609::Net::Impl::setUpNet DNN module was not built with CUDA backend; switching to CPU

Is there a way to solve this without install opencv using cmake? It's a mess on windows...

like image 882
Panda50 Avatar asked Aug 26 '20 16:08

Panda50


People also ask

Can OpenCV use GPU?

GPU module API interface is also kept similar with CPU interface where possible. So developers who are familiar with Opencv on CPU could start using GPU straightaway.

How do I enable GPU in Python?

Installation: First, make sure that Nvidia drivers are upto date also you can install cudatoolkit explicitly from here. then install Anaconda add anaconda to the environment while installing. After completion of all the installations run the following commands in the command prompt.


3 Answers

Things seem to have changed a little since this question was asked initially:

From https://github.com/opencv/opencv-python

Option 1 - Main modules package: pip install opencv-python

Option 2 - Full package (contains both main modules and contrib/extra modules): pip install opencv-contrib-python (check contrib/extra modules listing from OpenCV documentation) ==> https://docs.opencv.org/master/

Sadly, not all of the modules listed above seem to be available in the "Full package" eg. cudafilters. If anyone knows any better, I for one would be very grateful to learn more.

like image 61
RJJ Avatar answered Oct 20 '22 19:10

RJJ


The problem here is that version of opencv distributed with your system (Windows in this case) was not compiled with Cuda support. Therefore, you cannot use any cuda related function with this build.

If you want to have an opencv with cuda support, you will have to either compile it yourself (which may be tedious on windows) or find a prebuilt one somewhere. In case you want to go for the 1st solution, here is a link that may help you with the process: https://programming.vip/docs/compile-opencv-with-cuda-support-on-windows-10.html. Keep in mind that this will require you to install a bunch of SDK in the process.

like image 6
Harry333Cover Avatar answered Oct 20 '22 19:10

Harry333Cover


For those who can get the same issue. As Harry mentionned, it's not possible to use GPU with opencv from pip, you have to "manually" build it using Cmake (for windows).

It's a bit tricky but there are many tutorials which are here to help you. I spent two days trying to make cvlib works and that's why: one of the cudnn.dll curently available from Nvidia website is named:

Cudnn64_8.dll

and opencv (or tensorflow to be more precise) needs

Cudnn64_7.dll

in fact you just have to replace the 8 by the 7 ! ;) That was the only hard part and I believed it came from the cmake process.

Thanks again Harry.

like image 6
Panda50 Avatar answered Oct 20 '22 18:10

Panda50