Using OpenCV dnn module as dll, Net::forward() error

asked 2019-11-20 20:46:53 -0600

J.Han gravatar image

updated 2019-12-02 00:38:33 -0600

berak gravatar image

Hello

Question : OpenCV DNN module, cv::dnn::Net::forward causes error and that Net::forward() is in a DLL that i made before. Since it came from DLL, debug messages are not displayed on Console window.

  1. Is there any way to display debug messages came from DLL that contains OpenCV?

  2. Is there a Special way to call cv::dnn::Net::forward() from DLL?

Situation : I have made .weight file and .cfg file through Yolov3 of Darknet to run it in OpenCV DNN module.

Language is C++.

Here is my Code:

//Initialize Net, darkConfig = yolov3-tiny-obj.cfg darkModel = ...
Net net = readNetFromDarknet(darkConfig, darkModel);
net.setPreferableBackend(DNN_BACKEND_OPENCV);
net.setPreferableTarget(DNN_TARGET_CPU);

//Get Classes Names, ex Bird, Dog...
string classesFile = "obj.names";
ifstream ifs(classesFile.c_str());
string line;
while (getline(ifs, line))
{
    classes.push_back(line);
    cout << line << endl;
}

VideoCapture cap(0);
if (cap.isOpened() == false)
{
    cerr << "No cam" << endl;
    return -1;
}

Mat frame;
while (true)
{
    cap >> frame;
    if (frame.empty()) break;
    Mat blob;

    // Create a 4D blob from a frame.
    blobFromImage(frame, blob, 1 / 255.0, Size(288, 288), Scalar(0, 0, 0), true, false);

    //Sets the input to the network
    net.setInput(blob);

    // Runs the forward pass to get output of the output layers
    vector<Mat> outs;

    //BUT THIS LINE ALWAYS CAUSES ERROR
    net.forward(outs, getOutputsNames(net));

    // Remove the bounding boxes with low confidence
    postprocess(frame, outs);

    cv::imshow("Frame", frame);

    if (cv::waitKey(1) == 27)
        break;
}
return 0;

This Code works well when it is built by Visual Studio C++.

But When it is built as DLL and linked to another, the cv::dnn::Net::forward() causes error.

Other Net class' methods don't cause error Net::setInput() etc.

Even this error cannot be debugged like so :

image description

Conclusion:

  1. cv::dnn::Net::forward() works well in general C++ project.

  2. When that project is built as DLL and linked to another C++project, the only Net::forward() causes error.

How to solve this problem?

Thank you.

edit retag flag offensive close merge delete

Comments

can you check, if the same linker flags apply to your own dll, and opencv's ? like multithreaded dynamic runtime (/MD)

berak gravatar imageberak ( 2019-11-21 06:12:19 -0600 )edit