**OpenCV’s ‘Deep Neural Network’ (dnn) **module is a convenient tool for computer vision, it is very easy to apply some techniques such as Yolo and OpenPose. However, the major drawback of OpenCV was the lack of GPU support, resulting in slow inference. Luckily since OpenCV 4.2, NVIDIA GPU/CUDA is supported.
It still required some works to use GPU, you can check Pyimagesearch’s article here, they demonstrate how to set up a Ubuntu machine.
If you do not have a machine with GPU like me, you can consider using Google Colab, which is a free service with powerful NVIDIA GPU. It is also a lot easier to set up, most of the requirements are already satisfied. In this article, I will share how I set up the Colab environment for OpenCV’s dnn
with GPU in just a few lines of code. You can also check here, I made slight changes based on the answer.
The code to assign the dnn
to GPU is simple:
import cv2
net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile)
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
However, if you run this cell directly on Colab, you will see this error:
#computer-vision #programming #deep-learning #data-science #opencv