readNetFromDarkNet Throwing Exception

Hello this is my first time posting, if I am missing anything please ask.

I am trying to run yolov3 object detection algorithm in opencv on a cpu in C++ Visual Studio 2019.

Here is my C++ Code:

#include <iostream>
#include <fstream>

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/dnn.hpp>

using namespace std;

int main()
{
    cv::VideoCapture webcam(0);
    cv::Mat frame;
    webcam.read(frame);

    vector<string> classNames;
    ifstream cocoNamesFile;
    cocoNamesFile.open("C:/darknet/cfg/coco.names");
    string line;

    try
    {
        if (cocoNamesFile.fail()) throw 1;
        while (getline(cocoNamesFile, line)) classNames.push_back(line);
        for (int i = 0; i < classNames.size(); i++) cout << classNames[i] << endl;
        cout << "Number of different classes: " << classNames.size() << endl;
        cocoNamesFile.close();

        if (frame.empty()) throw 2;

        string modelConfiguration = "C:/Users/Blake/Documents/College Courses/Senior Design Project (400D)/YOLO/yolov3-320.cfg";
        string modelWeights = "C:/Users/Blake/Documents/College Courses/Senior Design Project (400D)/YOLO/yolov3-320.weights";

        ifstream model;
        model.open(modelConfiguration);
        if (model.fail()) throw 3;
        model.close();
        model.open(modelWeights);
        if (model.fail()) throw 4;
        model.close();

        cv::dnn::Net net = cv::dnn::readNetFromDarknet(modelConfiguration, modelWeights);
        net.setPreferableBackend(cv::dnn::DNN_BACKEND_OPENCV);
        net.setPreferableTarget(cv::dnn::DNN_TARGET_CPU);

        do
        {

            cv::imshow("Webcam Live Feed", frame);
            cv::waitKey(1);
            webcam.read(frame);
        } while (!frame.empty());
    }

    catch (int i)
    {
        if (i == 1) cout << "Cannot Open Coco Names File\n";
        else if (i == 2) cout << "Cannot Read Web Cam\n";
        else if (i == 3) cout << "Cannot Open Model Configuration File\n";
        else if (i == 4) cout << "Cannot Open Model Weights File\n";

        return EXIT_FAILURE;
    }

    return 0;
}

I know the code is not complete and is not optimized, I have been working through a youtube video that uses python and I am trying to mimic in C++ and catch errors when they occur.

At runtime, an exception is thrown at cv::dnn::readNetFromDarkNet(modelConfiguration, modelWeights); I am not sure what to do because my ifstream is able to open the files just fine but it throws an error inside the function.

I have downloaded these files from the official Yolo website.

Any assistance would be appreciated, thank you. I am hoping to get this code running for a senior electrical engineering design automous vehicle project.

and what exception is thrown?

Please see the following attachment of the exception thrown in the Visual Studio IDE.

please check, if you downloaded the yolo data correctly
(compare sizes, peek into it with some text editor)

Hello,

The .cfg file looks right. I am not sure if the .weights file is right as it is just a list of data and a text editor displays it as ASCII characters.

I have downloaded the files from the official YOLO website: https://pjreddie.com/darknet/yolo/

I have used the yolov3-320 version. Is there a type of version that is only supported by opencv?

If you could please assist me in attaining the right files that would be appreciated.

Thank you.

i wonder why there is only 1 cfg for 3 model sizes (defaults to 608x608)
can you try the yolov3-608 version (which seems to work for anyone else) ?
or, try to change the sizes in the cfg to 320x320 ?

I ran the 608 version but it still throws the same exception.

I noticed that opencv gives me some information about the error that occurs:

OpenCV(4.5.3) Error: The function/feature is not implemented (Transpose the weights (except for convolutional) is not implemented) in cv::dnn::darknet::ReadDarknetFromWeightsStream, file C:\build\master_winpack-build-win64-vc15\opencv\modules\dnn\src\darknet\darknet_io.cpp, line 931

Does this help at all?

1 Like

Could I possibly be missing an opencv file? The location they are referring to does not exist on my machine.

Hi., try to place the cfg and weights files near the exe., and pass the direct pass “” to the readfromNet function instead of passing the string variables

Try to download another YOLO model or try ONNX or TensorFlow formats

please explain, more detailled (links ?)

the location they are referring to does not exist on my machine.

that’s the location, the prebuild libs were built, no, not your machine.

Hi Alex,

Here is where I placed the cfg and weight files:

And here is the C++ VS code I am running:

int main()
{
    cv::VideoCapture webcam(0);
    cv::Mat frame;
    webcam.read(frame);

    vector<string> classNames;
    ifstream cocoNamesFile;
    cocoNamesFile.open("C:/darknet/cfg/coco.names");
    string line;

    try
    {
        if (cocoNamesFile.fail()) throw 1;
        while (getline(cocoNamesFile, line)) classNames.push_back(line);
        for (int i = 0; i < classNames.size(); i++) cout << classNames[i] << endl;
        cout << "Number of different classes: " << classNames.size() << endl;
        cocoNamesFile.close();

        if (frame.empty()) throw 2;

        string modelConfiguration = "C:/Users/Blake/source/repos/YOLO/yolov3-608.cfg";
        string modelWeights = "C:/Users/Blake/source/repos/YOLO/yolov3-608.weights";

        ifstream model;
        model.open(modelConfiguration);
        if (model.fail()) throw 3;
        model.close();
        model.open(modelWeights);
        if (model.fail()) throw 4;
        model.close();

        cv::dnn::Net net = cv::dnn::readNetFromDarknet("C:/Users/Blake/source/repos/YOLO/yolov3-608.cfg", "C:/Users/Blake/source/repos/YOLO/yolov3-608.weights");
        net.setPreferableBackend(cv::dnn::DNN_BACKEND_OPENCV);
        net.setPreferableTarget(cv::dnn::DNN_TARGET_CPU);

        do
        {

            cv::imshow("Webcam Live Feed", frame);
            cv::waitKey(1);
            webcam.read(frame);
        } while (!frame.empty());
    }

    catch (int i)
    {
        if (i == 1) cout << "Cannot Open Coco Names File\n";
        else if (i == 2) cout << "Cannot Read Web Cam\n";
        else if (i == 3) cout << "Cannot Open Model Configuration File\n";
        else if (i == 4) cout << "Cannot Open Model Weights File\n";

        return EXIT_FAILURE;
    }

    return 0;
}

I am still getting an exception thrown at the same spot

That error was printed to the console. Is it possible this version of opencv does not support yolov3? Possibly some sort of bug??

Any suggestions on what to do next to run yolov3?

I am really hoping to run this software on a pi for an autonomous vehicle project for school.