Home > Software engineering >  OPENCV: Fail to use dnn module in c
OPENCV: Fail to use dnn module in c

Time:03-11

I'm using dnn module in opencv. Previously, it work well in python. But when I turn to C version.readNetFromDarknet function report error that I don't know how to fix. Error is below:

Unhandled exception at 0x00007FFF37BE4F69 in untitled.exe: Microsoft C   exception: std::out_of_range at memory location 0x000000BF2A53F090

My full code is :

#include <QCoreApplication>
#include <opencv2/opencv.hpp>
#include <string>

using namespace cv;
using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    string classesFile, modelConfig, modelWeights;

    classesFile = "D:/work/rs/model/guoxing_mac9_3535_20220224.names";
    modelConfig = "D:/work/rs/model/guoxing_mac9_3535_20220224.cfg";
    modelWeights = "D:/work/rs/model/guoxing_mac9_3535_20220224_last.weights";
    dnn::Net m_model;           
    vector<string> m_classes; 
    ifstream ifs(classesFile.c_str());
    string line;
    while (getline(ifs, line))
        m_classes.push_back(line);
    
    try
    {
        cout << "trying" << endl;
        m_model = dnn::readNetFromDarknet(modelConfig, modelWeights);
    }
    catch (Exception& e)
    {
        cout << e.msg << endl;
    }

    m_model.setPreferableBackend(dnn::DNN_BACKEND_OPENCV);
    m_model.setPreferableTarget(dnn::DNN_TARGET_OPENCL);
    
    return a.exec();
}

Test in Opencv3.4.2 and VS2017. Thanks for all reply!

CodePudding user response:

I cannot post a comment because my reputation is too low, but what I think is your problem is:

while (getline(ifs,line))

as getline does not return a bool, so it won't become false, thus the while-loop does not break. Why do you want a while loop with getline?

CodePudding user response:

After update Opencv from 3.4.2 to 3.4.16, it solved. low version dnn module is not work well for some network architecture.

  • Related