Ask Your Question

OpenCV + Darknet - Error when initializing darknet

asked 2017-12-14 11:52:16 -0600

lorenzo gravatar image

Hi! I'm an university student and for my thesis work I have to perform object detection using YOLO.

I read the related paper and I completed all of the command line examples located at

Now I have to do the same using OpenCV. I'm using Xubuntu 16.04 LTS x64, OpenCV 3.3.1 and Qt Creator 4.5.0.

For the moment I'm stuck at these few lines because I can't succeed to initialize the network.

#include <opencv2/core.hpp>
#include <opencv2/dnn.hpp>
#include <opencv2/dnn/shape_utils.hpp>
#include <iostream>

using namespace cv;
using namespace cv::dnn;
using namespace std;

int main()
    // The path to the .cfg file with text description of the network architecture.
    String modelConfiguration = "/home/lorenzo/Scrivania/yolo-9000/darknet/cfg/yolo-9000.cfg";
    // The path to the .weights file with learned network.
    String modelBinary = "/home/lorenzo/Scrivania/yolo-9000/yolo9000-weights/yolo-9000.weights";

    //! [Initialize network]
    //Reads a network model stored in Darknet model files.
    dnn::Net net = readNetFromDarknet(modelConfiguration, modelBinary);
    //! [Initialize network]

    if (net.empty())
        cerr << "Can't load network by using the following files: " << endl;
        cerr << "cfg-file:     " << modelConfiguration << endl;
        cerr << "weights-file: " << modelBinary << endl;

    return 0;

It returns me the following error.

OpenCV Error: Parsing error (Failed to parse NetParameter file: /home/lorenzo/Scrivania/yolo-9000/darknet/cfg/yolo-9000.cfg) in ReadNetParamsFromCfgFileOrDie,
                      file /home/lorenzo/Scrivania/opencv-3.3.1/modules/dnn/src/darknet/darknet_io.cpp, line 612
terminate called after throwing an instance of 'cv::Exception'
what():  /home/lorenzo/Scrivania/opencv-3.3.1/modules/dnn/src/darknet/darknet_io.cpp:612:
            error: (-212) Failed to parse NetParameter file: /home/lorenzo/Scrivania/yolo-9000/darknet/cfg/yolo-9000.cfg in function ReadNetParamsFromCfgFileOrDie

I opened the file at "/home/lorenzo/Scrivania/opencv-3.3.1/modules/dnn/src/darknet/darknet_io.cpp" and I found the following code at lines 609:614.

void ReadNetParamsFromCfgFileOrDie(const char *cfgFile, darknet::NetParameter *net)
            if (!darknet::ReadDarknetFromCfgFile(cfgFile, net)) {
                CV_Error(cv::Error::StsParseError, "Failed to parse NetParameter file: " + std::string(cfgFile));

The output of cv::getBuildInformation() is the following.

General configuration for OpenCV 3.3.1 =====================================
  Version control:               unknown

    Timestamp:                   2017-12-13T21:08:24Z
    Host:                        Linux 4.10.0-42-generic x86_64
    CMake:                       3.5.1
    CMake generator:             Unix Makefiles
    CMake build tool:            /usr/bin/make
    Configuration:               Release

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3
      requested:                 SSE3
    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2
      SSE4_1 (3 files):          + SSSE3 SSE4_1
      SSE4_2 (1 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
      FP16 (2 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (5 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (8 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2

    Built as dynamic libs?:      YES
    C++ Compiler:                /usr/bin/c++  (ver 5.4.0)
    C++ flags (Release):         -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections  -msse -msse2 -msse3 ...
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-12-14 13:30:19 -0600

Eduardo gravatar image

updated 2018-11-08 12:04:09 -0600

As of 2017/12/14, YOLOv2 has been integrated into OpenCV but not YOLO9000, see the corresponding pull request.

See the sample code (here for OpenCV 3.4.3) if you want to do object detection with the supported networks (YOLO, MobileNet-SSD, Faster R-CNN, ...). Change the tag or the branch on Github to get the sample file according to your OpenCV version.

edit flag offensive delete link more



Very fast and complete answer. Thank you so much!!

lorenzo gravatar imagelorenzo ( 2017-12-14 13:37:49 -0600 )edit

@Eduardo the link finds nothing, could you fix it?

voo_doo gravatar imagevoo_doo ( 2018-11-08 01:32:34 -0600 )edit

@voo_doo I have updated the link.

Eduardo gravatar imageEduardo ( 2018-11-08 12:04:32 -0600 )edit

Question Tools

1 follower


Asked: 2017-12-14 11:52:16 -0600

Seen: 8,378 times

Last updated: Nov 08 '18