# How to detect objects with Cpp and DNN, CUDA

**URL:** https://forum.opencv.org/t/how-to-detect-objects-with-cpp-and-dnn-cuda/3516
**Category:** C++
**Tags:** dnn, cuda, videoio
**Created:** [May 25, 2021, 9:49am UTC](https://forum.opencv.org/t/how-to-detect-objects-with-cpp-and-dnn-cuda/3516 "2021-05-25T09:49:26Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Legion\_PL](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/legion_pl/32/761_2.png) [@Legion\_PL](https://forum.opencv.org/u/Legion_PL)
#### Post date: [May 25, 2021, 9:49am UTC](https://forum.opencv.org/t/how-to-detect-objects-with-cpp-and-dnn-cuda/3516/1 "2021-05-25T09:49:26Z")

</div>

**What I want to do:**

I would like to write a program that is **detecting objects in real-time** (There will be more features in the future, so I really hope to write smth that I could modify and add to). I’m just starting with Computer Vision, I’m C++ developer and have some experience with OpenCV - that’s why I would prefer to use OpenCV and that language for that. I aim at performance and speed with stable video input and I read that DNN models run the best in that regard. I looked around the internet and tried to install everything (CUDA, cuDNN, OpenCV, Yolo), I successfully run some examples from yolo but I have no idea how to implement that to my cpp code. I looked around for some step-by-step guide, but still, without success - link errors in OpenCV, VisualStudio doesn’t see yolo, code examples don’t run, my IDE doesn’t see OpenCV define, builds are extra slow, and even occasional bluescreens! I’m sure I did something wrong but doesn’t know what, because to start anything I needed to reach different sources that ware not necessarily compatible 😕

As my first milestone, I want to just run some code examples that detect faces on my webcam. I hope that after achieving that I would have an easier time with anything more.

**Environment:**

I’m working on **Windows + Visual Studio**

**What I’m looking for here:**

So I’m here to ask, for some help with research. Maybe someone knows what step should I take or what phases should I look for to successfully do that or at last help me achieve my first milestone?

---

<div class="post-metadata">

### Author: ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)
#### Post date: [May 25, 2021, 2:19pm UTC](https://forum.opencv.org/t/how-to-detect-objects-with-cpp-and-dnn-cuda/3516/2 "2021-05-25T14:19:05Z")

</div>

one problem at a time, please.

which opencv version ? what did you install ? did you try to build it locally ?

opencv’s dnn can read yolo networks, so your 1st attempt might be [to get the sample code](https://github.com/opencv/opencv/blob/master/samples/dnn/object_detection.cpp) running with a yolo network

(no you don’t need to build darknet for this at all)

once you have that, you could try to rebuild the opencv libs with contrib modules / cuda

---

<div class="post-metadata">

### Author: ![Legion\_PL](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/legion_pl/32/761_2.png) [@Legion\_PL](https://forum.opencv.org/u/Legion_PL)
#### Post date: [May 25, 2021, 3:12pm UTC](https://forum.opencv.org/t/how-to-detect-objects-with-cpp-and-dnn-cuda/3516/3 "2021-05-25T15:12:38Z")

</div>

@berak

I use OpenCV 4.5.2, it’s seemed that I built something wrong. After some tweaks I finally succeeded (at last I think so cause all examples I tried somehow run).

I also finally succeded in running Opencv dnn example - object\_detection, using:

```
$ example_dnn_object_detection --config=yolov3.cfg --model=yolov3.weights --width=416 --height=416 --scale=0.00392 --target=1

```

It indeed recognizes objects, but it’s working terribly slow - around 1,5 FPS, how can I improve that?

I already built it with CUDA.  
I tried to run modules/cudaobjectdetect.sln but while building I get

```
gl_core_3_1.obj : error LNK2019: unresolved external symbol __imp_wglGetProcAddress referenced in function "void *__ cdecl IntGetProcAddress(char const *)" (?IntGetProcAddress@@YAPEAXPEBD@Z)
1>opengl.obj : error LNK2019: unresolved external symbol __imp_wglGetCurrentContext referenced in function "class cv::ocl::Context &__ cdecl cv::ogl::ocl::initializeContextFromGL(void)" (?initializeContextFromGL@ocl@ogl@cv@@YAAEAVContext@13@XZ)
1>opengl.obj : error LNK2019: unresolved external symbol __imp_wglGetCurrentDC referenced in function "class cv::ocl::Context &__ cdecl cv::ogl::ocl::initializeContextFromGL(void)" (?initializeContextFromGL@ocl@ogl@cv@@YAAEAVContext@13@XZ)
1>C:\Program Files\OpenCV\build\bin\Release\opencv_core452.dll : fatal error LNK1120: 3 unresolved externals
1>Done building project "opencv_core.vcxproj" -- FAILED.

```

How can I fix it?  
I built with OpenGL as well.

---

<div class="post-metadata">

### Author: ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)
#### Post date: [May 26, 2021, 12:00pm UTC](https://forum.opencv.org/t/how-to-detect-objects-with-cpp-and-dnn-cuda/3516/4 "2021-05-26T12:00:17Z")

</div>

> [@Legion\_PL](#):
>
> around 1,5 FPS

Exact hardware specs please.

OpenCV can use various backends and devices. Check that it executes DNN on the GPU, not the CPU

---

<div class="post-metadata">

### Author: ![Legion\_PL](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/legion_pl/32/761_2.png) [@Legion\_PL](https://forum.opencv.org/u/Legion_PL)
#### Post date: [May 26, 2021, 1:11pm UTC](https://forum.opencv.org/t/how-to-detect-objects-with-cpp-and-dnn-cuda/3516/5 "2021-05-26T13:11:42Z")

</div>

@crackwitz

Here :  
CPU - i5-4460 3.20GHz  
GPU - Nvidia GeForce GTX 970  
RAM - 8GB  
Windows 10 Pro 64x

DNN example use like 35% GPU and CPU, on diffrent --targets it use CPU only or do not start at all

---

<div class="post-metadata">

### Author: ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)
#### Post date: [May 26, 2021, 1:44pm UTC](https://forum.opencv.org/t/how-to-detect-objects-with-cpp-and-dnn-cuda/3516/6 "2021-05-26T13:44:49Z")

</div>

there are also [faster and smaller yolo networks](https://github.com/dog-qiuqiu/MobileNet-Yolo)

---

<div class="post-metadata">

### Author: ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)
#### Post date: [May 26, 2021, 5:13pm UTC](https://forum.opencv.org/t/how-to-detect-objects-with-cpp-and-dnn-cuda/3516/7 "2021-05-26T17:13:24Z")

</div>

[a GTX 970 has ~4 Tflop/s of conventional FP32](https://www.techpowerup.com/gpu-specs/geforce-gtx-970.c2620), which is [about half of what an RTX 2070 can do](https://www.techpowerup.com/gpu-specs/geforce-rtx-2070.c3252), but that ignores _tensor cores_. the 20 series has tensor cores, the 9 series does not. tensor cores are **the** performance factor that accelerate convolutional layers by at least an order of magnitude.

do follow that link on mobile/tiny variants of these networks.

make sure to pick the CUDA backend, not the generic OpenCL one.

someone named Yashas Samaga implemented the CUDA backend for the dnn module. he can also be seen battling with Adrian Rosebrock over proper benchmarking methodology on Adrian’s notorious code blog. I won’t link to that but suffice it to say, I chose my words carefully.
