Use Opencv with GPU with just 2 lines of code (2024)

Table of Contents
Lines to add Related Posts

If you are working on deep learning or real-time video processing project using Opencv (like Object Detection, Social Distance detection), you will face lags in the output video (less frame rate per second), you can fix this lag using GPU if your system has NVIDIA GPU (NVIDIA Graphics card). OpenCV library can be used for both CPU and GPU.

In my last post, I have shared how to install OpenCV GPU for windows. In this post, I will show you how you can use OpenCV with GPU to optimize your real-time video processing project up to 1000 times faster with just 2 lines of code.

Must Read:

Lines to add

These are the two lines of code you need to add after OpenCV’s “dnn” module (where you are reading the pre-trained deep learning or machine learning model).

net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)

Now if you recall Object detection with the YOLO algorithm in my previous article. There was a huge lag in the real-time output video. Now let’s add those two lines of code and let’s see the changes.

CPU processing code (before):

net = cv2.dnn.readNet(yolo_weight, yolo_config)
Use Opencv with GPU with just 2 lines of code (1)

GPU processing code (after):

net = cv2.dnn.readNet(yolo_weight, yolo_config)net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)

As you can see before adding those two lines of code the frame rate was: 3. And there was a huge lag in the real-time output video. But after adding those two lines of code frame rate (FPS): 11 and there is no lag in real-time video analysis output.

If you have any question or suggestion regarding this topic see you in comment section. I will try my best to answer.

Use Opencv with GPU with just 2 lines of code (3)

Hi there, I’m Anindya Naskar, Data Science Engineer. I created this website to show you what I believe is the best possible way to get your start in the field of Data Science.

Use Opencv with GPU with just 2 lines of code (2024)
Top Articles
Latest Posts
Article information

Author: Tish Haag

Last Updated:

Views: 6470

Rating: 4.7 / 5 (67 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Tish Haag

Birthday: 1999-11-18

Address: 30256 Tara Expressway, Kutchburgh, VT 92892-0078

Phone: +4215847628708

Job: Internal Consulting Engineer

Hobby: Roller skating, Roller skating, Kayaking, Flying, Graffiti, Ghost hunting, scrapbook

Introduction: My name is Tish Haag, I am a excited, delightful, curious, beautiful, agreeable, enchanting, fancy person who loves writing and wants to share my knowledge and understanding with you.