I get the output of yolo_net.forward() as [ 1 x 25200 x 6] . I am using Mat datatype to store the output from .forward(). Next, I need to access confidence values from 4th index position of 25,200 rows. I am not able to do that using .at function in c++. How do I do that?
Secondly, whenever I am trying to access the rows using . rows() function , I get output of my rows and columns as -1. I don’t know how to proceed.
Here is a snippet of my code:
` Mat img = imread(“/home/laddu/307.png”);
cout << "Image size: " << img.size() << endl;
cv::dnn::Net yolo_net = dnn::readNetFromONNX("/home/laddu/Downloads/best.onnx");
resize(img, img, Size(1000, 600));
Mat blob = dnn::blobFromImage(img, 1.0/255, Size(640, 640), Scalar(0, 0, 0), true, false);
net.setInput(blob);
Mat detections = net.forward()
;
vector<int> classes_ids;
vector<float> confidences;
vector<Rect> boxes;
int rows = detections.size[1];
int img_width = img.size().width;
int img_height = img.size().height;
float x_scale = (float)img_width/640;
float y_scale = (float)img_height/640;
for(int i = 0; i < rows; i++)
{
Mat row = detections.row(0).rowRange(i, i+1);
float confidence = row.at<float>(4);
if (confidence > 0.5)
{
Mat classes_score = row.colRange(5, row.cols);
Point max_loc;
double max_val;
minMaxLoc(classes_score, NULL, &max_val, NULL, &max_loc);
int ind = max_loc.x;
if (max_val > 0.5)
{
classes_ids.push_back(ind);
confidences.push_back(confidence);
float cx = row.at<float>(0);
float cy = row.at<float>(1);
float w = row.at<float>(2);
float h = row.at<float>(3);
int x1 = static_cast<int>((cx - w / 2) * x_scale);
int y1 = static_cast<int>((cy - h / 2) * y_scale);
int width = static_cast<int>(w * x_scale);
int height = static_cast<int>(h * y_scale);
boxes.push_back(Rect(x1, y1, width, height));
}
}
} I get an exception whenever i enter the first for loop and as soon as I start accessing the rows, exception is thrown.