Object Detection on a Webcam with Yolo

I set up Yolov3 like on this page: Darknet Yolo and everything works but while tried to run Real-Time Detection on a Webcam with
./darknet detector demo cfg/coco.data cfg/yolov3.cfg yolov3.weights

it doesn’t work.

Your error messages would be helpful to get hint.

./darknet detector demo cfg/coco.data cfg/yolov3.cfg yolov3.weights
after run this code, the error is: Demo needs OpenCV for webcam images.

there is only this. nothing else.

@savunmasad,

Are u using a CSI camera or an USB webcam?
If you have a CSI camera, follow this command:

USB, trying ZED camera

Hi,

Please remember to enable the openCV flag to launch the demo application.
For example. if you are using this GitHub, please apply the following two change:

Enable darnet for Jetson

From 437eab747cc076c67d9299a5df586290741b1ec6 Mon Sep 17 00:00:00 2001
From: Your Name <you@example.com>
Date: Wed, 6 May 2020 11:44:41 +0800
Subject: [PATCH] bug 200614143

---
 Makefile             | 16 ++++++-------
 src/image_opencv.cpp | 56 ++++++++++++++++----------------------------
 2 files changed, 27 insertions(+), 45 deletions(-)

diff --git a/Makefile b/Makefile
index 63e15e6..9a7471d 100644
--- a/Makefile
+++ b/Makefile
@@ -1,13 +1,11 @@
-GPU=0
-CUDNN=0
-OPENCV=0
+GPU=1
+CUDNN=1
+OPENCV=1
 OPENMP=0
 DEBUG=0
 
-ARCH= -gencode arch=compute_30,code=sm_30 \
-      -gencode arch=compute_35,code=sm_35 \
-      -gencode arch=compute_50,code=[sm_50,compute_50] \
-      -gencode arch=compute_52,code=[sm_52,compute_52]
+ARCH= -gencode arch=compute_62,code=sm_62 \
+      -gencode arch=compute_62,code=[sm_62,compute_62]
 #      -gencode arch=compute_20,code=[sm_20,sm_21] \ This one is deprecated?
 
 # This is what I use, uncomment if you know your arch and want to specify
@@ -42,8 +40,8 @@ CFLAGS+=$(OPTS)
 ifeq ($(OPENCV), 1) 
 COMMON+= -DOPENCV
 CFLAGS+= -DOPENCV
-LDFLAGS+= `pkg-config --libs opencv` -lstdc++
-COMMON+= `pkg-config --cflags opencv` 
+LDFLAGS+= `pkg-config --libs opencv4` -lstdc++
+COMMON+= `pkg-config --cflags opencv4` 
 endif
 
 ifeq ($(GPU), 1) 
diff --git a/src/image_opencv.cpp b/src/image_opencv.cpp
index 7511280..c11805a 100644
--- a/src/image_opencv.cpp
+++ b/src/image_opencv.cpp
@@ -9,30 +9,34 @@ using namespace cv;
 
 extern "C" {
 
-IplImage *image_to_ipl(image im)
+Mat image_to_mat(image im)
 {
+    assert(im.c == 3 || im.c == 1);
     int x,y,c;
-    IplImage *disp = cvCreateImage(cvSize(im.w,im.h), IPL_DEPTH_8U, im.c);
-    int step = disp->widthStep;
+    image copy = copy_image(im);
+    constrain_image(copy);
+    if(im.c == 3) rgbgr_image(copy);
+    Mat m(im.h, im.w, CV_MAKETYPE(CV_8U, im.c));
     for(y = 0; y < im.h; ++y){
         for(x = 0; x < im.w; ++x){
             for(c= 0; c < im.c; ++c){
-                float val = im.data[c*im.h*im.w + y*im.w + x];
-                disp->imageData[y*step + x*im.c + c] = (unsigned char)(val*255);
+                float val = copy.data[c*im.h*im.w + y*im.w + x];
+                m.data[y*im.w*im.c + x*im.c + c] = (unsigned char)(val*255);
             }
         }
     }
-    return disp;
+    free_image(copy);
+    return m;
 }
 
-image ipl_to_image(IplImage* src)
+image mat_to_image(Mat m)
 {
-    int h = src->height;
-    int w = src->width;
-    int c = src->nChannels;
+    int h = m.rows;
+    int w = m.cols;
+    int c = m.channels();
     image im = make_image(w, h, c);
-    unsigned char *data = (unsigned char *)src->imageData;
-    int step = src->widthStep;
+    unsigned char *data = (unsigned char*)m.data;
+    int step = m.step;
     int i, j, k;
 
     for(i = 0; i < h; ++i){
@@ -42,26 +46,6 @@ image ipl_to_image(IplImage* src)
             }
         }
     }
-    return im;
-}
-
-Mat image_to_mat(image im)
-{
-    image copy = copy_image(im);
-    constrain_image(copy);
-    if(im.c == 3) rgbgr_image(copy);
-
-    IplImage *ipl = image_to_ipl(copy);
-    Mat m = cvarrToMat(ipl, true);
-    cvReleaseImage(&ipl);
-    free_image(copy);
-    return m;
-}
-
-image mat_to_image(Mat m)
-{
-    IplImage ipl = m;
-    image im = ipl_to_image(&ipl);
     rgbgr_image(im);
     return im;
 }
@@ -72,9 +56,9 @@ void *open_video_stream(const char *f, int c, int w, int h, int fps)
     if(f) cap = new VideoCapture(f);
     else cap = new VideoCapture(c);
     if(!cap->isOpened()) return 0;
-    if(w) cap->set(CV_CAP_PROP_FRAME_WIDTH, w);
-    if(h) cap->set(CV_CAP_PROP_FRAME_HEIGHT, w);
-    if(fps) cap->set(CV_CAP_PROP_FPS, w);
+    if(w) cap->set(CAP_PROP_FRAME_WIDTH, w);
+    if(h) cap->set(CAP_PROP_FRAME_HEIGHT, w);
+    if(fps) cap->set(CAP_PROP_FPS, w);
     return (void *) cap;
 }
 
@@ -123,7 +107,7 @@ void make_window(char *name, int w, int h, int fullscreen)
 {
     namedWindow(name, WINDOW_NORMAL); 
     if (fullscreen) {
-        setWindowProperty(name, CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
+        setWindowProperty(name, WND_PROP_FULLSCREEN, WINDOW_FULLSCREEN);
     } else {
         resizeWindow(name, w, h);
         if(strcmp(name, "Demo") == 0) moveWindow(name, 0, 0);
-- 
2.17.1

Fix the compatible issue for cuDNN v8.

From 816ec5889d7f43a3486021868096ec5576b9eb09 Mon Sep 17 00:00:00 2001
From: Your Name <you@example.com>
Date: Fri, 10 Jul 2020 12:15:07 +0800
Subject: [PATCH] fix for cudnn_v8 (limited memory to default darknet setting)

---
 src/convolutional_layer.c | 74 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 74 insertions(+)

diff --git a/src/convolutional_layer.c b/src/convolutional_layer.c
index 1fb58b0..f950c01 100644
--- a/src/convolutional_layer.c
+++ b/src/convolutional_layer.c
@@ -8,6 +8,9 @@
 #include <stdio.h>
 #include <time.h>
 
+#define PRINT_CUDNN_ALGO 0
+#define MEMORY_LIMIT 2000000000
+
 #ifdef AI2
 #include "xnor_layer.h"
 #endif
@@ -145,6 +148,76 @@ void cudnn_convolutional_setup(layer *l)
     }
     #endif
 
+    #if CUDNN_MAJOR >= 8
+    int returnedAlgoCount;
+    cudnnConvolutionFwdAlgoPerf_t       fw_results[2 * CUDNN_CONVOLUTION_FWD_ALGO_COUNT];
+    cudnnConvolutionBwdDataAlgoPerf_t   bd_results[2 * CUDNN_CONVOLUTION_BWD_DATA_ALGO_COUNT];
+    cudnnConvolutionBwdFilterAlgoPerf_t bf_results[2 * CUDNN_CONVOLUTION_BWD_FILTER_ALGO_COUNT];
+
+    cudnnFindConvolutionForwardAlgorithm(cudnn_handle(),
+            l->srcTensorDesc,
+            l->weightDesc,
+            l->convDesc,
+            l->dstTensorDesc,
+            CUDNN_CONVOLUTION_FWD_ALGO_COUNT,
+            &returnedAlgoCount,
+	    fw_results);
+    for(int algoIndex = 0; algoIndex < returnedAlgoCount; ++algoIndex){
+        #if PRINT_CUDNN_ALGO > 0
+        printf("^^^^ %s for Algo %d: %f time requiring %llu memory\n",
+               cudnnGetErrorString(fw_results[algoIndex].status),
+               fw_results[algoIndex].algo, fw_results[algoIndex].time,
+               (unsigned long long)fw_results[algoIndex].memory);
+        #endif
+        if( fw_results[algoIndex].memory < MEMORY_LIMIT ){
+            l->fw_algo = fw_results[algoIndex].algo;
+            break;
+	}
+    }
+
+    cudnnFindConvolutionBackwardDataAlgorithm(cudnn_handle(),
+            l->weightDesc,
+            l->ddstTensorDesc,
+            l->convDesc,
+            l->dsrcTensorDesc,
+            CUDNN_CONVOLUTION_BWD_DATA_ALGO_COUNT,
+            &returnedAlgoCount,
+            bd_results);
+    for(int algoIndex = 0; algoIndex < returnedAlgoCount; ++algoIndex){
+        #if PRINT_CUDNN_ALGO > 0
+        printf("^^^^ %s for Algo %d: %f time requiring %llu memory\n",
+               cudnnGetErrorString(bd_results[algoIndex].status),
+               bd_results[algoIndex].algo, bd_results[algoIndex].time,
+               (unsigned long long)bd_results[algoIndex].memory);
+        #endif
+        if( bd_results[algoIndex].memory < MEMORY_LIMIT ){
+            l->bd_algo = bd_results[algoIndex].algo;
+            break;
+        }
+    }
+
+    cudnnFindConvolutionBackwardFilterAlgorithm(cudnn_handle(),
+            l->srcTensorDesc,
+            l->ddstTensorDesc,
+            l->convDesc,
+            l->dweightDesc,
+            CUDNN_CONVOLUTION_BWD_FILTER_ALGO_COUNT,
+            &returnedAlgoCount,
+            bf_results);
+    for(int algoIndex = 0; algoIndex < returnedAlgoCount; ++algoIndex){
+        #if PRINT_CUDNN_ALGO > 0
+        printf("^^^^ %s for Algo %d: %f time requiring %llu memory\n",
+               cudnnGetErrorString(bf_results[algoIndex].status),
+               bf_results[algoIndex].algo, bf_results[algoIndex].time,
+               (unsigned long long)bf_results[algoIndex].memory);
+        #endif
+        if( bf_results[algoIndex].memory < MEMORY_LIMIT ){
+            l->bf_algo = bf_results[algoIndex].algo;
+            break;
+        }
+    }
+
+    #else
     cudnnGetConvolutionForwardAlgorithm(cudnn_handle(),
             l->srcTensorDesc,
             l->weightDesc,
@@ -169,6 +242,7 @@ void cudnn_convolutional_setup(layer *l)
             CUDNN_CONVOLUTION_BWD_FILTER_SPECIFY_WORKSPACE_LIMIT,
             2000000000,
             &l->bf_algo);
+    #endif
 }
 #endif
 #endif
-- 
2.17.1

Thanks.

1 Like

Screenshot from 2020-10-07 10-31-10

i tried it from Darknet Yolo
and i activated GPU, CUDNN, OPENCV and then make beforehand
but it shows me

sadsavunma@sadsavunma-desktop:/media/sadsavunma/SD/darknet$ make
gcc -Iinclude/ -Isrc/ -DOPENCV `pkg-config --cflags opencv`  -DGPU -I/usr/local/cuda/include/ -DCUDNN  -Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DOPENCV -DGPU -DCUDNN -c ./src/gemm.c -o obj/gemm.o
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
./src/gemm.c: In function ‘time_gpu’:
./src/gemm.c:232:9: warning: ‘cudaThreadSynchronize’ is deprecated [-Wdeprecated-declarations]
         cudaThreadSynchronize();
         ^~~~~~~~~~~~~~~~~~~~~
In file included from /usr/local/cuda/include/cuda_runtime.h:96:0,
                 from include/darknet.h:11,
                 from ./src/utils.h:5,
                 from ./src/gemm.c:2:
/usr/local/cuda/include/cuda_runtime_api.h:957:57: note: declared here
 extern __CUDA_DEPRECATED __host__ cudaError_t CUDARTAPI cudaThreadSynchronize(void);
                                                         ^~~~~~~~~~~~~~~~~~~~~
gcc -Iinclude/ -Isrc/ -DOPENCV `pkg-config --cflags opencv`  -DGPU -I/usr/local/cuda/include/ -DCUDNN  -Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DOPENCV -DGPU -DCUDNN -c ./src/utils.c -o obj/utils.o
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
gcc -Iinclude/ -Isrc/ -DOPENCV `pkg-config --cflags opencv`  -DGPU -I/usr/local/cuda/include/ -DCUDNN  -Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DOPENCV -DGPU -DCUDNN -c ./src/cuda.c -o obj/cuda.o
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
gcc -Iinclude/ -Isrc/ -DOPENCV `pkg-config --cflags opencv`  -DGPU -I/usr/local/cuda/include/ -DCUDNN  -Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DOPENCV -DGPU -DCUDNN -c ./src/deconvolutional_layer.c -o obj/deconvolutional_layer.o
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
gcc -Iinclude/ -Isrc/ -DOPENCV `pkg-config --cflags opencv`  -DGPU -I/usr/local/cuda/include/ -DCUDNN  -Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DOPENCV -DGPU -DCUDNN -c ./src/convolutional_layer.c -o obj/convolutional_layer.o
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
./src/convolutional_layer.c: In function ‘cudnn_convolutional_setup’:
./src/convolutional_layer.c:148:5: warning: implicit declaration of function ‘cudnnGetConvolutionForwardAlgorithm’; did you mean ‘cudnnGetConvolutionForwardAlgorithm_v7’? [-Wimplicit-function-declaration]
     cudnnGetConvolutionForwardAlgorithm(cudnn_handle(),
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     cudnnGetConvolutionForwardAlgorithm_v7
./src/convolutional_layer.c:153:13: error: ‘CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT’ undeclared (first use in this function); did you mean ‘CUDNN_CONVOLUTION_FWD_ALGO_DIRECT’?
             CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT,
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
             CUDNN_CONVOLUTION_FWD_ALGO_DIRECT
compilation terminated due to -Wfatal-errors.
Makefile:89: recipe for target 'obj/convolutional_layer.o' failed
make: *** [obj/convolutional_layer.o] Error 1
sadsavunma@sadsavunma-desktop:/media/sadsavunma/SD/darknet$ 

it doesn’t work.

Hi,

Based on the log, please use the opencv4 rather than opencv.
The folder for opencv v4.x.x has changed for distinguishing.

... -DOPENCV `pkg-config --cflags opencv4` ...

Thanks.

like this ?
+GPU=1
+CUDNN=1
+OPENCV4=1
i don’t understand i’m the new at jetson

Hi,

Please update here into the following:

 ifeq ($(OPENCV), 1) 
 COMMON+= -DOPENCV
 CFLAGS+= -DOPENCV
 LDFLAGS+= `pkg-config --libs opencv4` -lstdc++
 COMMON+= `pkg-config --cflags opencv4` 
 endif

The complete detail is shared above.
It is worthy to take a look and update the darknet source with the change.

Thanks.

thank you