I get this error and I dont understand very well why. I create my own model with yolov5 and I export it as ONNX symplified. Also I am using opencv 4.5.4 and c++14 in visual studio 2022.
Here i leave mi code:
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include
using namespace cv;
using namespace dnn;
using namespace std;
int main() {
String modelFile = "C://Capturas//best.onnx";
Net net = readNet(modelFile);
String imageFile = "C://Capturas//data//images//train//positivo121.jpg";
Mat frame = imread(imageFile);
if (frame.empty()) {
cerr << "No se pudo cargar la imagen." << endl;
return -1;
}
Size targetSize(640, 640);
resize(frame, frame, targetSize);
Mat blob = blobFromImage(frame, 1, Size(640, 640), true, false);
net.setInput(blob);
try {
Mat detection = net.forward();
Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());
for (int i = 0; i < detectionMat.rows; i++) {
float confidence = detectionMat.at<float>(i, 2);
if (confidence > 0.5) {
int x1 = static_cast<int>(detectionMat.at<float>(i, 3) * frame.cols);
int y1 = static_cast<int>(detectionMat.at<float>(i, 4) * frame.rows);
int x2 = static_cast<int>(detectionMat.at<float>(i, 5) * frame.cols);
int y2 = static_cast<int>(detectionMat.at<float>(i, 6) * frame.rows);
rectangle(frame, Point(x1, y1), Point(x2, y2), Scalar(0, 255, 0), 2);
}
}
}
catch (Exception ex) {
cout << "HUBO UN ERROR GORDO: " << ex.what() << endl;
}
imshow("Objetos detectados", frame);
waitKey(0);
destroyAllWindows();
return 0;
}