Error: (-215:Assertion failed) i < dims() in function 'cv::MatSize::operator []'

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;

}

… index out of bounds …

indeed

what IS detection.size ?

what about class probabilities ? NMS ? yolo boxes being [cx,cy,w,h] ?

you’ll have to rewrite this anyway …

@laurent.berger , where is the image from ?

I downloaded onnx model link in ultralytics/yolov5: YOLOv5 :rocket: in PyTorch > ONNX > CoreML > TFLite
then I use netron get input and output size

1 Like

and, does opencv’s dnn really output a 3d float16 result ?

whoa, that would be new to me, and we’d have to check a lot of resp. samples now …

No

import numpy as np
import cv2 as cv

yolov5 = cv.dnn.readNet(r"C:\Users\laurent\Downloads\yolov5s6.onnx")

img = cv.imread(cv.samples.findFile('baboon.jpg'))

paramYolov5 = cv.dnn.Image2BlobParams()
paramYolov5.datalayout = cv.dnn.DNN_LAYOUT_NCHW
paramYolov5.ddepth = cv.CV_32F
paramYolov5.mean = (0, 0, 0)
paramYolov5.scalefactor = (1 / 255, 1 / 255, 1 / 255)
paramYolov5.size = (640, 640)
paramYolov5.swapRB = True

blob = cv.dnn.blobFromImageWithParams(img, paramYolov5)
yolov5.setInput(blob)

output = yolov5.forward()

print("output.shape : ", output.shape)
print("output.dtype : ", output.dtype)

Results

output.shape :  (1, 25500, 85)
output.dtype :  float32

PS No idea what’s scalefactor and mean real values

1 Like