使用OpenCV、ONNXRuntime在c++平台部署YOLOV7实时目标检测的代码
时间: 2023-12-31 12:04:00 浏览: 116
好的,以下是一个简单的使用OpenCV和ONNXRuntime在C++平台部署YOLOv7实时目标检测的代码示例:
```c++
#include <iostream>
#include <chrono>
#include <opencv2/opencv.hpp>
#include <onnxruntime_cxx_api.h>
using namespace std;
using namespace cv;
using namespace std::chrono;
using namespace Ort;
int main() {
// 加载模型
Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "test");
Ort::SessionOptions session_options;
OrtSession* session;
Ort::AllocatorWithDefaultOptions allocator;
const char* model_path = "yolov7.onnx";
session = new Ort::Session(env, model_path, session_options);
// 加载图像
Mat frame = imread("test.jpg");
if (frame.empty()) {
cout << "读取图像失败!" << endl;
return -1;
}
// 预处理图像
Mat input_blob = blobFromImage(frame, 1 / 255.0, Size(416, 416), Scalar(0, 0, 0), true, false);
Tensor input_tensor = Tensor(
DataType::FLOAT,
{1, 3, input_blob.rows, input_blob.cols},
input_blob.data
);
// 运行模型
vector<const char*> input_names = {"input_1"};
vector<const char*> output_names = {"Identity_1", "Identity_2", "Identity_3"};
vector<Ort::Value> input_tensors = {input_tensor};
vector<Ort::Value> output_tensors = session->Run(Ort::RunOptions{}, input_names.data(), input_tensors.data(), input_names.size(), output_names.data(), output_names.size());
// 后处理输出结果
vector<Mat> detections;
for (int i = 0; i < output_tensors.size(); i++) {
float* output_data = output_tensors[i].GetTensorMutableData<float>();
vector<int64_t> output_shape = output_tensors[i].GetTensorTypeAndShapeInfo().GetShape();
Mat detection(output_shape[1], output_shape[0], CV_32F, output_data);
detections.push_back(detection);
}
float confidence_threshold = 0.5;
vector<int> class_ids;
vector<float> confidences;
vector<Rect> boxes;
for (int i = 0; i < detections.size(); i++) {
for (int j = 0; j < detections[i].rows; j++) {
float* data = detections[i].ptr<float>(j);
float confidence = data[4];
if (confidence > confidence_threshold) {
int class_id = j;
float left = data[0] * frame.cols;
float top = data[1] * frame.rows;
float right = data[2] * frame.cols;
float bottom = data[3] * frame.rows;
int width = right - left + 1;
int height = bottom - top + 1;
class_ids.push_back(class_id);
confidences.push_back(confidence);
boxes.push_back(Rect(left, top, width, height));
}
}
}
for (int i = 0; i < boxes.size(); i++) {
Scalar color(0, 255, 0);
rectangle(frame, boxes[i], color, 2);
string label = format("%.2f", confidences[i]);
int baseline;
Size label_size = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseline);
rectangle(frame, Point(boxes[i].x, boxes[i].y - label_size.height - baseline), Point(boxes[i].x + label_size.width, boxes[i].y), color, FILLED);
putText(frame, label, Point(boxes[i].x, boxes[i].y - baseline), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0));
}
// 显示结果
namedWindow("YOLOv7", WINDOW_NORMAL);
resizeWindow("YOLOv7", 800, 600);
imshow("YOLOv7", frame);
waitKey(0);
// 释放资源
delete session;
return 0;
}
```
这段代码首先使用ONNXRuntime加载YOLOv7模型,然后利用OpenCV读取图像并进行预处理,接着调用ONNXRuntime运行模型,最后根据模型输出结果进行后处理并显示检测结果。需要注意的是,这段代码只是一个简单的示例,实际部署时需要根据具体的场景进行调整。
阅读全文