赞
踩
1.yolov10推理代码
- #include <iostream>
- #include <string>
- #include <vector>
- #include <openvino/openvino.hpp>
- #include <opencv2/opencv.hpp>
- std::vector<cv::Scalar> colors = { cv::Scalar(0, 0, 255) , cv::Scalar(0, 255, 0) , cv::Scalar(255, 0, 0) ,
- cv::Scalar(255, 100, 50) , cv::Scalar(50, 100, 255) , cv::Scalar(255, 50, 100) };
- const std::vector<std::string> class_names = {
- "person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light",
- "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow",
- "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
- "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard",
- "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple",
- "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch",
- "potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone",
- "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear",
- "hair drier", "toothbrush" };
- using namespace cv;
- using namespace dnn;
-
- Mat letterbox(const cv::Mat& source)
- {
- int col = source.cols;
- int row = source.rows;
- int _max = MAX(col, row);
- Mat result = Mat::zeros(_max, _max, CV_8UC3);
- source.copyTo(result(Rect(0, 0, col, row)));
- return result;
- }
- int main(int argc, char* argv[])
- {
- int64 start = cv:: getTickCount();
- ov::Core core;
- auto compiled_model = core.compile_model("/home/master/yolov10/yolov10x.xml");
- ov::InferRequest infer_request = compiled_model.create_infer_request();
- Mat img = cv::imread("/home/master/yolov10/12.jpg");
- Mat letterbox_img = letterbox(img);
- float scale = letterbox_img.size[0] / 640.0;
- Mat blob = blobFromImage(letterbox_img, 1.0 / 255.0, Size(640, 640), Scalar(), true);
- auto input_port = compiled_model.input();
- ov::Tensor input_tensor(input_port.get_element_type(), input_port.get_shape(), blob.ptr(0));
- infer_request.set_input_tensor(input_tensor);
- infer_request.infer();
- auto output = infer_request.get_output_tensor(0);
- auto output_shape = output.get_shape();
- std::cout << "The shape of output tensor:" << output_shape << std::endl;
- int rows = output_shape[2];
- int dimensions = output_shape[1];
- float* data = output.data<float>();
- Mat output_buffer(output_shape[1], output_shape[2], CV_32F, data);
- float score_threshold = 0.15;
- std::vector<int> class_ids;
- std::vector<float> class_scores;
- std::vector<Rect> boxes;
- for (int i = 0; i < output_buffer.rows; i++) {
- float confidence = output_buffer.at<float>(i, 4);
- if (confidence > score_threshold) {
- int xmin = output_buffer.at<float>(i, 0) * scale;
- int ymin = output_buffer.at<float>(i, 1) * scale;
- int xmax = output_buffer.at<float>(i, 2) * scale;
- int ymax = output_buffer.at<float>(i, 3) * scale;
- int width = xmax - xmin;
- int height = ymax - ymin;
- int ID = (int)output_buffer.at<float>(i, 5);
- boxes.push_back(Rect(xmin, ymin, width, height));
- class_scores.push_back(confidence);
- class_ids.push_back(ID);
- }
- }
- for (size_t i = 0; i < boxes.size(); i++)
- {
- rectangle(img, boxes[i], colors[class_ids[i] % 6], 2, 8);
- std::string label = class_names[class_ids[i]] + ":" + std::to_string(class_scores[i]).substr(0, 4);
- Size textSize = cv::getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, 0);
- Rect textBox(boxes[i].tl().x, boxes[i].tl().y - 15, textSize.width, textSize.height+5);
- cv::rectangle(img, textBox, colors[class_ids[i] % 6], FILLED);
- putText(img, label, Point(boxes[i].tl().x, boxes[i].tl().y - 5), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 255, 255));
- }
- int64 end = cv::getTickCount();
- double frequency = cv::getTickFrequency();
- double duration = (end - start) / frequency;
- std::cout << "Execution time: " << duration << " seconds" << std::endl;
- namedWindow("result", WINDOW_AUTOSIZE);
- cv::imwrite("../resultv10.jpg", img);
- imshow("result", img);
- waitKey(0);
- destroyAllWindows();
- return 0;
- }
2.CMakeLists
- cmake_minimum_required(VERSION 3.0.0)
- project(yolo5 VERSION 0.1.0 LANGUAGES C CXX)
-
- include(CTest)
- enable_testing()
-
- set(INC_DIR /opt/intel/openvino/runtime/include)
- set(LINK_DIR /opt/intel/openvino/runtime/lib/intel64)
- set(CMAKE_CXX_STANDARD 17)
- include_directories(${INC_DIR})
- link_directories(${LINK_DIR})
- link_libraries(libopenvino.so)
-
- add_executable(yolo10 yolo.cpp)
-
- target_link_libraries(yolo10 libopenvino.so)
-
- set(CPACK_PROJECT_NAME ${PROJECT_NAME})
- SET(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
-
- include(CPack)
- find_package(OpenCV)
- include_directories(${OpenCV_INCLUDE_DIRS})
- target_link_libraries(yolo10 ${OpenCV_LIBS})
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。