微信公众号:OpenCV开发者联盟
关注获取更多计算机视觉与深度学习知识
同步模式推理流程
while(true) {
// capture frame
// populate CURRENT InferRequest
// Infer CURRENT InferRequest
//this call is synchronous
// display CURRENT result
}
// 创建IE插件, 查询支持硬件设备
ov::Core core;
std::string model_onnx = "D:/python/yolov5-7.0/yolov5s.onnx";
auto model = core.read_model(model_onnx);
ov::CompiledModel cmodel = core.compile_model(model, "CPU");
// create infer request
auto request = cmodel.create_infer_request();
cv::Mat frame;
while (true) {
bool ret = cap.read(frame);
if (frame.empty()) {
break;
}
image_detect(frame, request);
char c = cv::waitKey(1);
if (c == 27) { // ESC
break;
}
}
// 前处理
// 开启同步
request.infer();
// 后处理
异步模式推理流程
推理的流程如下:
while(true) {
// capture frame
// populate NEXT InferRequest
// start NEXT InferRequest
// this call is async and returns immediately
// wait for the CURRENT InferRequest
// display CURRENT result
// swap CURRENT and NEXT InferRequests
}
1// 创建IE插件, 查询支持硬件设备
2ov::Core core;
3std::string model_onnx = "D:/python/yolov5-7.0/yolov5s.onnx";
4auto model = core.read_model(model_onnx);
5ov::CompiledModel cmodel = core.compile_model(model, "AUTO");
6
7// create infer request
8auto request = cmodel.create_infer_request();
9auto next_request = cmodel.create_infer_request();
10std::exception_ptr exception_var;
11request.set_callback([&](std::exception_ptr ex) {
12 if (ex) {
13 exception_var = ex;
14 return;
15 }
16 det_boxes.clear();
17 det_ids.clear();
18 ov::Tensor output = request.get_output_tensor();
19 const float* prob = (float*)output.data();
20 const ov::Shape outputDims = output.get_shape();
21 size_t numRows = outputDims[1];
22 size_t numCols = outputDims[2];
23
24 // 后处理, 1x25200x85
25 std::vector boxes;
26 std::vector<int> classIds;
27 std::vector<float> confidences;
28 cv::Mat det_output(numRows, numCols, CV_32F, (float*)prob);
29 for (int i = 0; i < det_output.rows; i++) {
30 float confidence = det_output.at<float>(i, 4);
31 if (confidence < 0.45) {
32 continue;
33 }
34 cv::Mat classes_scores = det_output.row(i).colRange(5, numCols);
35 cv::Point classIdPoint;
36 double score;
37 minMaxLoc(classes_scores, 0, &score, 0, &classIdPoint);
38
39 // 置信度 0~1之间
40 if (score > 0.25)
41 {
42 float cx = det_output.at<float>(i, 0);
43 float cy = det_output.at<float>(i, 1);
44 float ow = det_output.at<float>(i, 2);
45 float oh = det_output.at<float>(i, 3);
46 int x = static_cast<int>((cx - 0.5 * ow) * x_factor);
47 int y = static_cast<int>((cy - 0.5 * oh) * y_factor);
48 int width = static_cast<int>(ow * x_factor);
49 int height = static_cast<int>(oh * y_factor);
50 cv::Rect box;
51 box.x = x;
52 box.y = y;
53 box.width = width;
54 box.height = height;
55
56 boxes.push_back(box);
57 classIds.push_back(classIdPoint.x);
58 confidences.push_back(score);
59 }
60 }
61
62 // NMS
63 std::vector<int> indexes;
64 cv::dnn::NMSBoxes(boxes, confidences, 0.25, 0.45, indexes);
65 for (size_t i = 0; i < indexes.size(); i++) {
66 int index = indexes[i];
67 det_ids.emplace_back(classIds[index]);
68 det_boxes.emplace_back(boxes[index]);
69 }
70});
1cv::Mat frame, next_frame;
2// do first frame
3cap.read(frame);
4async_image_detect(frame, request);
5std::chrono::milliseconds tout{ 50 };
6int cnt = 0;
7while (true) {
8 bool ret = cap.read(next_frame);
9 if (next_frame.empty()) {
10 break;
11 }
12
13 int64 start = cv::getTickCount();
14 // 继续异步
15 if (cnt % 2 == 0) {
16 async_image_detect(next_frame, next_request);
17 request.wait_for(tout);
18 }
19 if (cnt % 2 == 1) {
20 async_image_detect(next_frame, request);
21 next_request.wait_for(tout);
22 }
23 for (size_t t = 0; t < det_boxes.size(); t++) {
24 int idx = det_ids[t];
25 cv::rectangle(frame, det_boxes[t], colors_table[idx % 6], 2, 8, 0);
26 putText(frame, classNames[idx].c_str(), det_boxes[t].tl(), cv::FONT_HERSHEY_PLAIN, 1.0, cv::Scalar(255, 0, 0), 1, 8);
27 }
28
29 // 计算FPS render it
30 float t = (cv::getTickCount() - start) / static_cast<float>(cv::getTickFrequency());
31 putText(frame, cv::format("FPS: %.2f", 1.0 / t), cv::Point(20, 40), cv::FONT_HERSHEY_PLAIN, 2.0, cv::Scalar(255, 0, 0), 2, 8);
32 cv::imshow("OpenVINO2023 - YOLOv5 7.0 异步推理", frame);
33 char c = cv::waitKey(1);
34 if (c == 27) { // ESC
35 break;
36 }
37 next_frame.copyTo(frame);
38 cnt++;
39}
40cv::waitKey(0);
41cv::destroyAllWindows();
42return 0;
preprocess(frame)
// 开启异步
request.start_async();
推荐阅读
YOLOv8修改+注意力模块 训练与部署
普通PC上CPU运行YOLOv5推理 400+FPS
Ubuntu系统下编译OpenCV4.8源码记录
Pytoorch轻松学 – RetinaNet自定义对象检测
基于OpenCV实现精准线线间距测量
扫码查看深度学习系统化学习路线图