opencv yolo11 onnx c++代码

1.export onnx, opset我设置的是12,其它值或许也可以

2.opencv使用的4.10【其他版本或许也可以,4.7.0版本是不行的】

3.代码核心参考yolo之前版本的实现即可。

 

model.setInput(blob);
model.forward(outputs, outnames);

const int dimensions = 84;
const int rows = 8400;
float x_factor = input_image.cols / INPUT_WIDTH;
float y_factor = input_image.rows / INPUT_HEIGHT;

outputs[0] = outputs[0].reshape(1, dimensions);
cv::transpose(outputs[0], outputs[0]);//转置

float* data = (float*)outputs[0].data;

 

std::vector<int> class_ids;
std::vector<float> confidences;
std::vector<cv::Rect> boxes;

for (int i = 0; i < rows; ++i) {

 

float* classes_scores = data + 4;
cv::Mat scores(1, dimensions - 4, CV_32FC1, classes_scores);
cv::Point class_id;
double max_class_score;
minMaxLoc(scores, 0, &max_class_score, 0, &class_id);
if (max_class_score > SCORE_THRESHOLD) {

confidences.push_back(max_class_score);

class_ids.push_back(class_id.x);

float x = data[0];
float y = data[1];
float w = data[2];
float h = data[3];
int left = int((x - 0.5 * w) * x_factor);
int top = int((y - 0.5 * h) * y_factor);
int width = int(w * x_factor);
int height = int(h * y_factor);
boxes.push_back(cv::Rect(left, top, width, height));

}

data += dimensions;

}

posted @ 2024-10-17 10:06  彭城老朽  阅读(90)  评论(0编辑  收藏  举报