19/08/2020

4 Steps to Install Darknet with Cuda and Opencv for realtime object detection

By snorlaxprime

I have been pulling my hair for the last couple of days trying to install Darknet with Cuda and Opencv. Installing Darknet on it’s own is easy, you only need to pull the github repository and compile it.

Step 1. Install Darknet

Thanks to Joseph Redmond you can download Darknet from the following location:

git clone https://github.com/pjreddie/darknet
cd darknet
make

Once it compiled successfully you will need to download the pre-trained weight file (it is 237 MB) using the following command

wget https://pjreddie.com/media/files/yolov3.weights

Then test it by running the detector using the following command

./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg

It should execute successfully and detect the picture of a dog as per below:

The detection output will be in a file called predictions.png

Your detection should take about 6-12 seconds using CPU, you can experiments with a few different files in the data directory, e.g.: horses.jpg, giraffe.jpg, person.jpg, eagle.jpg

So speed up the detection you will need to use the GPU, this will reduce the time down to fraction of a second, so that’s when Cuda came in, so let’s proceed to the next step to install Cuda.

Step 2. Install Cuda

The first pain came from installing Cuda. You will have to get the right version of Cuda from NVidia website, this depends on the graphics card that you have. I found that the version that works for me is version 10.2, so I went and download “cuda_10.2.89_mac.dmg”

Once installed when trying to compile darknet using the GPU=1 in the Makefile. If you encounter the first error come that you can’t find nvcc. To solve this you can modify the Makefile using

vi Makefile

edit the following line

NVCC=/usr/local/cuda/bin/nvcc

Then it will complain that the following library is not loaded when you try to run the image prediction.

Library not loaded: @rpath/libcudart.10.2.dylib

I solved it by running the following in the command prompt

export CUDA_HOME=/usr/local/cuda
export DYLD_LIBRARY_PATH=/usr/local/cuda/lib:/usr/local/cuda/extras/CUPTI/lib
export LD_LIBRARY_PATH=$DYLD_LIBRARY_PATH
export PATH=$DYLD_LIBRARY_PATH:$PATH

Then I get hit with the following error:

CUDA Error: out of memory

To solve the above, I reconfigure the cfg/yolov3.cfg as following

batch=32
subdivision=32
width=416
height=416

Once this is done successfully, you should see significant improvement in the speed of processing the image. Now we can proceed to the next step to install OpenCV

Step 3. Install OpenCV

The next pain is to install opencv. Now I am downloading the latest OpenCV using the following command

cd $home
mkdir OpenCV
cd OpenCV
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

Now that you have downloaded opencv, we will try to separate the build in temporary directory called build_opencv so execute the following command

mkdir build_opencv
cd build_opencv

now configure the opencv by executing cmake, as per the following command:

cmake -DCMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=ON -DBUILD_EXAMPLES=ON -D CMAKE_INSTALL_PREFIX=/usr/local ../opencv 

Then you can build using the following command

make -j7

If all goes well it should build successfully and you can execute the following command to install opencv

sudo make install

Once it installed successfully you can test that pkg-config is configured properly using the following command

pkg-config --cflags opencv4

-I/usr/local/include/opencv4

Try to compile darknet again, if you encounter the error where it still can’t find opencv.pc, you might want to check that you have the opencv4.pc in the following location

ls /usr/local/lib/pkgconfig/opencv4.pc

if you do then just copy opencv4 to opencv by using the following command

sudo cp /usr/local/lib/pkgconfig/opencv4.pc /usr/local/lib/pkgconfig/opencv.pc

Now try to compile darknet again but first you will need to change the option opencv=1 in the Makefile. When you try to run the make command you might get the following error if you are using opencv version 4.

error: #error "OpenCV 4.x+ requires enabled C++11 support"

To solve this you will need to edit the Makefile by adding the -std=c++11 in the following line:

CPP=g++ -std=c++11

Step 4. Test everything

If all goes well you should be able to get realtime detection using the webcam. To try that you can run the following command

./darknet detector demo cfg/coco.data cfg/yolov3.cfg yolov3.weights

This will open your webcam and start detecting objects in realtime as shown in the following video.

You can also get it to run detection on a video by using the following command

./darknet detector demo cfg/coco.data cfg/yolov3.cfg yolov3.weights <video file>

I hope you like this post which shows that AI will soon take over the world (just kidding) but we are not far off. Please drop me a comment if you like this post or subscribe if you haven’t already.