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.
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
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