Ask Your Question

Does opencv_dnn use gpu?

asked 2019-10-18 03:41:43 -0500

redhwan gravatar image

updated 2019-10-20 21:21:06 -0500

I tried to run the code in Jetson GPU but I can't?

I just obtained 5 fps, this means that use CPU not GPU

this is my full code

from import VideoStream
from import FPS
import numpy as np
import argparse
import imutils
import time
import cv2
CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat",
           "bottle", "bus", "car", "cat", "chair", "cow", "diningtable",
           "dog", "horse", "motorbike", "person", "pottedplant", "sheep",
           "sofa", "train", "tvmonitor"]
COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe('prototxt.txt', 'caffemodel')  # deep neural network ( dnn )
print("[INFO] starting video stream...")
vs = cv2.VideoCapture(0)
vs = cv2.VideoCapture(0)
fps = FPS().start()

while True:

    ret, frame =  # add ret,

    frame = imutils.resize(frame, width=400)

    (h, w) = frame.shape[:2]
    blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)),
                                 0.007843, (300, 300), 127.5)

    detections = net.forward()
    big_area = 0
    big_center = 320
    detected = 0
    # loop over the detections
    for i in np.arange(0, detections.shape[2]):

        confidence = detections[0, 0, i, 2]
        object_type = int(detections[0, 0, i, 1])

        if object_type == 15 and confidence > 0.2:  # it was if confidence > args["confidence"]:

            box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
            (startX, startY, endX, endY) = box.astype("int")

            label = "{}: {:.2f}%".format('person', confidence * 100)

            cv2.rectangle(frame, (startX, startY), (endX, endY), [0, 0, 255], 2)
            y = startY - 15 if startY - 15 > 15 else startY + 15
            cv2.putText(frame, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, [0, 0, 255], 2)

            rect_area = (endX - startX) * (endY - startY)

            detected = 1
            if rect_area > big_area:  # big_area and big_center are used so that the TurtleBot always tracks the closest person
                big_area = rect_area

    cv2.imshow("Frame", frame)
    key = cv2.waitKey(1) & 0xFF

    if key == ord("q"):


    # stop the timer and display FPS information


any ideas or suggestions?

thank you in advance

edit retag flag offensive close merge delete


holger gravatar imageholger ( 2019-10-19 00:39:56 -0500 )edit

thanks, can I do it but it is bad, I saw that the CPU is faster than GPU with MobileNet SSD, right?

redhwan gravatar imageredhwan ( 2019-10-19 04:11:00 -0500 )edit

Note that the benchmarks were carried out on a desktop/mobile CPU. They might not be applicable to embedded boards. Jetson can probably perform better with the CUDA backend than the on-board CPU. CUDA support for the DNN module was merged into master a few days ago. You have to use net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA) and net.setPreferableTarget(cv2.dnn. DNN_TARGET_CUDA) or cv2.dnn.DNN_TARGET_CUDA_FP16 if your device supports it.

Yashas gravatar imageYashas ( 2019-10-26 22:50:30 -0500 )edit

@Yashas, thank you so much for your help, I was frustrated, but when I read your response, change my feeling, for more details about my system, you can see here, what is wrong???

redhwan gravatar imageredhwan ( 2019-10-26 23:55:11 -0500 )edit

@redhwan your question was posted before the CUDA support was merged into master. I preassume that your OpenCV build doesn't have the CUDA backend. To use the CUDA backend, you need to build the master branch from OpenCV GitHub repository.

Yashas gravatar imageYashas ( 2019-10-27 07:01:32 -0500 )edit

@Yashas, I have one question about building with CUDA based on your gist. The two first points WITH_CUDA and WITH_CUDNN are Cmake parameters I guess. Then there is The CUDA backend is enabled by setting the following option:OPENCV_DNN_CUDA Where in the build process do we specify that option?

Erik Langskjegg gravatar imageErik Langskjegg ( 2019-10-27 10:41:39 -0500 )edit

@Erik LangskjeggOPENCV_DNN_CUDA is also a CMake option just like the other two.

Yashas gravatar imageYashas ( 2019-10-27 12:49:59 -0500 )edit

@Yashas, I am confused about some things. So, I have some questions? first one: you mean that master is 4.x releases and branch is 3.4.x releases??? I got it here, right? Second one: do the steps of building a master differ from the branch? I followed this blog post hereon my PC with "3.3.1 version" and here on JetsonTX2. Third one: can I run the OpenCV dnn with CUDA Version: 9 or not? you mentioned cuDNN (min: 7.5.0) here if not, I need to use everything similar your system on my PC.

redhwan gravatar imageredhwan ( 2019-10-27 20:26:43 -0500 )edit

Sorry, @Yashas, I have other questions, you wrote above "you need to build the master branch from OpenCV GitHub repository", you mean these here and here or not ?? another issue I noticed here in your tables that MobileNet SSD is very bad with GPU, Inference Engine CPU was the best one?? can you give me some tips about how to use it? my PC: Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz and NVIDIA GTX 1050 Ti

redhwan gravatar imageredhwan ( 2019-10-27 20:46:16 -0500 )edit

@redhwan The CUDA backend hasn't been released yet. It's available on the master branch (where stuff for the next release are kept). You need to download (or clone) the OpenCV repository and build it. You can find more information about installing Inference Engine here.

Yashas gravatar imageYashas ( 2019-10-27 23:34:30 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-10-26 22:54:41 -0500

Yashas gravatar image



You can also use cv2.dnn.DNN_TARGET_CUDA_FP16 if your device supports it.

edit flag offensive delete link more


sorry @Yashas, how to use these lines and modify my code above,

blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 0.007843, (300, 300), 127.5)
blob = cv2.dnn.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
blob =cv2.dnn.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
detections = net.forward()

these are a modifitions, right??

redhwan gravatar imageredhwan ( 2019-10-27 00:12:09 -0500 )edit

The CUDA support is currently available in the master branch only. Assuming that you have built the master branch, you have to set the backend and target for the net object. You have to just call net.setPreferableBackend(backend_id) and net.setPreferableTarget(target_id). You can find a list of backends and targets here.

Yashas gravatar imageYashas ( 2019-10-27 00:49:34 -0500 )edit

Question Tools

1 follower


Asked: 2019-10-18 03:41:43 -0500

Seen: 2,888 times

Last updated: Oct 26 '19