Use GPU with opencv-python

Panda50 picture Panda50 · Aug 26, 2020 · Viewed 13.1k times · Source

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...

Answer

Harry333Cover picture Harry333Cover · Aug 26, 2020

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.