{"id":53728,"date":"2025-02-16T11:18:59","date_gmt":"2025-02-16T03:18:59","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53728\/"},"modified":"2025-02-16T11:18:59","modified_gmt":"2025-02-16T03:18:59","slug":"c%e8%bf%90%e8%a1%8cyolo%e8%bf%9b%e8%a1%8c%e7%9b%ae%e6%a0%87%e6%a3%80%e6%b5%8b","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53728\/","title":{"rendered":"C++\u8fd0\u884cYOLO\u8fdb\u884c\u76ee\u6807\u68c0\u6d4b"},"content":{"rendered":"<p>\u672c\u6587\u662f\u5173\u4e8e\u5982\u4f55\u5728<strong>CPU\u4e0a<\/strong>\u8fd0\u884cYOLOv5\u6a21\u578b\uff0c\u800c\u4e0d\u662fGPU\u3002\u8981\u5728GPU\u4e0a\u8fd0\u884c\u6a21\u578b\uff0c\u9700\u8981\u5b89\u88c5CUDA\u3001CUDNN\u7b49\uff0c\u8fd9\u53ef\u80fd\u4f1a\u8ba9\u4eba\u611f\u5230\u56f0\u60d1\u4e14\u8017\u65f6\u3002\u6211\u5c06\u5728\u672a\u6765\u5199\u53e6\u4e00\u7bc7\u6587\u7ae0\u4ecb\u7ecd\u5982\u4f55\u5728\u652f\u6301CUDA\u7684\u60c5\u51b5\u4e0b\u8fd0\u884cYOLO\u6a21\u578b\u3002<\/p>\n<p>\u76ee\u524d\uff0c\u4f60\u53ea\u9700\u8981\u5b89\u88c5OpenCV\u5e93\u3002\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5\u5b83\uff0c\u53ef\u4ee5\u4ece\u8fd9\u4e2a\u5b89\u88c5\u3002<\/p>\n<p>\u90a3\u4e48\uff0c\u8ba9\u6211\u4eec\u5f00\u59cb\u5427\u3002\u6211\u4f1a\u4e00\u6b65\u4e00\u6b65\u5730\u8bb2\u89e3\uff0c\u5b8c\u6574\u7684\u4ee3\u7801\u53ef\u4ee5\u5728\u9875\u9762\u5e95\u90e8\u67e5\u770b\u3002<\/p>\n<h2>1\u3001\u5bfc\u51faYOLOv5\u6a21\u578b\u4e3aONNX\u6a21\u578b<\/h2>\n<p>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u5939\u5e76\u547d\u540d\u4e3a\u4f60\u60f3\u8981\u7684\u540d\u79f0\u3002\u6253\u5f00\u7ec8\u7aef\u5e76\u5c06yolov5\u4ed3\u5e93\u514b\u9686\u5230\u8be5\u6587\u4ef6\u5939\u4e2d\u3002\u6211\u4eec\u5c06\u4f7f\u7528\u6b64\u4ed3\u5e93\u5bfc\u51fa\u6a21\u578b\u4e3aonnx\u683c\u5f0f\u3002<\/p>\n<pre><code>git clone https:\/\/github.com\/ultralytics\/yolov5\n<\/code><\/pre>\n<p>\u6211\u5c06\u4f7f\u7528yolov5s.pt\u6a21\u578b\uff0c\u4f46\u4f60\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684YOLOv5\u6a21\u578b\uff0c\u8fc7\u7a0b\u4e0d\u4f1a\u6539\u53d8\u3002\u4f60\u53ef\u4ee5\u4ece\u8fd9\u4e2a\u4e0b\u8f7d\u9884\u8bad\u7ec3\u6a21\u578b\uff0c\u6216\u8005\u4f7f\u7528\u4f60\u81ea\u5df1\u7684\u6a21\u578b\u3002\u5982\u679c\u4f60\u4e0d\u77e5\u9053\u5982\u4f55\u8bad\u7ec3\u81ea\u5b9a\u4e49YOLO\u6a21\u578b\uff0c\u4e0d\u7528\u62c5\u5fc3\uff0c\u6211\u5df2\u7ecf\u6709\u4e00\u7bc7\u6587\u7ae0\u4ecb\u7ecd\u4e86\u8fd9\u4e2a\u5185\u5bb9\uff0c\u4f60\u53ef\u4ee5\u67e5\u770b\u5b83\uff08\uff09<\/p>\n<p><strong>\u9884\u8bad\u7ec3YOLOv5\u6a21\u578b<\/strong> <\/p>\n<p>\u73b0\u5728\u8ba9\u6211\u4eec\u5c06\u6a21\u578b\u5bfc\u51fa\u4e3aonnx\u3002\u6709\u4e0d\u540c\u53c2\u6570\uff0c\u4f60\u53ef\u4ee5\u53c2\u8003\u4e0b\u9762\u7684\u56fe\u7247\u3002\u4f60\u53ef\u4ee5\u7f16\u8f91\u201c\u201d\u6587\u4ef6\uff08yolov5\/export.py\uff09\uff0c\u6216\u8005\u50cf\u6211\u5728\u8fd9\u91cc\u4e00\u6837\u624b\u52a8\u66f4\u6539\u53c2\u6570\u3002\u5bf9\u4e8e<strong>\u81ea\u5b9a\u4e49\u6a21\u578b<\/strong>\uff0c\u4f60\u9700\u8981\u5c06\u6743\u91cd\u66f4\u6539\u4e3a\u4f60\u7684\u81ea\u5b9a\u4e49\u6a21\u578b\u6743\u91cd\uff08your_model.pt\u6587\u4ef6\uff09\u3002<\/p>\n<pre><code>python yolov5\/export.py --weights yolov5s.pt --img 640 --include onnx --opset 12\n<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a\u4f60\u9700\u8981\u5c06<strong>opset\u8bbe\u7f6e\u4e3a12<\/strong>\uff0c\u5426\u5219\u53ef\u80fd\u4f1a\u62a5\u9519\u3002\u8fd9\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u5728GitHub\u4e0a\u67e5\u627e\u6b64\u9519\u8bef\u3002<\/p>\n<p>  parameers\/ export.py\u6587\u4ef6 <\/p>\n<h2>2\u3001\u521b\u5efaTXT\u6587\u4ef6\u5b58\u50a8YOLO\u6a21\u578b\u6807\u7b7e<\/h2>\n<p>\u8fd9\u4e00\u6b65\u5f88\u7b80\u5355\uff0c\u4f60\u53ea\u9700\u8981\u521b\u5efa\u4e00\u4e2atxt\u6587\u4ef6\u6765\u5b58\u50a8\u6807\u7b7e\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u9884\u8bad\u7ec3\u7684YOLO\u6a21\u578b\uff0c\u53ef\u4ee5\u4ece\u6b64\u76f4\u63a5\u4e0b\u8f7dtxt\u6587\u4ef6\u3002<br \/>\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6a21\u578b\uff0c\u5219\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684txt\u6587\u4ef6\u5e76\u5728\u5176\u4e2d\u5199\u5165\u4f60\u7684\u6807\u7b7e\u3002\u4f60\u53ef\u4ee5\u7ed9\u8fd9\u4e2a\u6587\u4ef6\u547d\u540d\u4efb\u4f55\u4f60\u60f3\u8981\u7684\u540d\u5b57\uff0c\u8fd9\u5e76\u4e0d\u91cd\u8981\u3002<\/p>\n<p>  coco-classes.txt <\/p>\n<h2>3\u3001\u521b\u5efaCMakeLists.txt\u6587\u4ef6<\/h2>\n<p>\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a<code>CMakeLists.txt<\/code>\u6587\u4ef6\u3002\u5f53\u4f60\u4f7f\u7528CMake\u7f16\u8bd1C++\u7a0b\u5e8f\u65f6\u9700\u8981\u8fd9\u4e2a\u6587\u4ef6\u3002\u5982\u679c\u4f60\u6309\u7167\u6211\u63d0\u4f9b\u7684\u94fe\u63a5\u5b89\u88c5\u4e86OpenCV\uff0c\u4f60\u5e94\u8be5\u5df2\u7ecf\u5b89\u88c5\u4e86CMake\u3002<\/p>\n<pre><code>cmake_minimum_required(VERSION 3.10)  \nproject(cpp-yolo-detection) # \u4f60\u7684\u6587\u4ef6\u5939\u540d\u79f0\n  \n# \u67e5\u627eOpenCV  \nset(OpenCV_DIR C:\/Libraries\/opencv\/build) # OpenCV\u8def\u5f84  \nfind_package(OpenCV REQUIRED)  \n  \nadd_executable(object-detection object-detection.cpp) # \u4f60\u7684\u6587\u4ef6\u540d  \n  \n# \u94fe\u63a5OpenCV\u5e93  \ntarget_link_libraries(object-detection ${OpenCV_LIBS})\n<\/code><\/pre>\n<h2>4\u3001\u4ee3\u7801<\/h2>\n<p>\u8fd9\u662f\u6700\u540e\u4e00\u6b65\u3002\u6211\u4f7f\u7528\u4e86\u8fd9\u4e2a\u4e2d\u7684\u4ee3\u7801\uff0c\u4f46\u4fee\u6539\u4e86\u4e00\u4e9b\u90e8\u5206\uff0c\u5e76\u6dfb\u52a0\u4e86\u6ce8\u91ca\u4ee5\u4fbf\u66f4\u597d\u5730\u7406\u89e3\u3002<\/p>\n<pre><code>#include &lt;fstream&gt;  \n#include &lt;opencv2\/opencv.hpp&gt;  \n  \n  \n\/\/ \u4ececoco-classes.txt\u6587\u4ef6\u52a0\u8f7d\u6807\u7b7e  \nstd::vector&lt;std::string&gt; load_class_list()  \n{  \n    std::vector&lt;std::string&gt; class_list;  \n    \/\/ \u66f4\u6539\u6b64txt\u6587\u4ef6\u4e3a\u4f60\u5305\u542b\u6807\u7b7e\u7684txt\u6587\u4ef6  \n    std::ifstream ifs(\"C:\/Users\/sirom\/Desktop\/cpp-ultralytics\/coco-classes.txt\");  \n    std::string line;  \n    while (getline(ifs, line))  \n    {  \n        class_list.push_back(line);  \n    }  \n    return class_list;  \n}  \n  \n\/\/ \u6a21\u578b   \nvoid load_net(cv::dnn::Net &amp;net)  \n{     \n    \/\/ \u66f4\u6539\u6b64\u8def\u5f84\u4e3a\u4f60\u6a21\u578b\u7684\u8def\u5f84   \n    auto result = cv::dnn::readNet(\"C:\/Users\/sirom\/Desktop\/cpp-ultralytics\/yolov5s.onnx\");  \n  \n    std::cout &lt;&lt; \"\u5728CPU\u4e0a\u8fd0\u884c\/n\";  \n    result.setPreferableBackend(cv::dnn::DNN_BACKEND_OPENCV);  \n    result.setPreferableTarget(cv::dnn::DNN_TARGET_CPU);  \n   \n    net = result;  \n}  \n  \nconst std::vector&lt;cv::Scalar&gt; colors = {cv::Scalar(255, 255, 0), cv::Scalar(0, 255, 0), cv::Scalar(0, 255, 255), cv::Scalar(255, 0, 0)};  \n  \n\/\/ \u4f60\u53ef\u4ee5\u66f4\u6539\u8fd9\u4e9b\u53c2\u6570\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u7ed3\u679c  \nconst float INPUT_WIDTH = 640.0;  \nconst float INPUT_HEIGHT = 640.0;  \nconst float SCORE_THRESHOLD = 0.5;  \nconst float NMS_THRESHOLD = 0.5;  \nconst float CONFIDENCE_THRESHOLD = 0.5;  \n  \nstruct Detection  \n{  \n    int class_id;  \n    float confidence;  \n    cv::Rect box;  \n};  \n  \n\/\/ yolov5\u683c\u5f0f  \ncv::Mat format_yolov5(const cv::Mat &amp;source) {  \n    int col = source.cols;  \n    int row = source.rows;  \n    int _max = MAX(col, row);  \n    cv::Mat result = cv::Mat::zeros(_max, _max, CV_8UC3);  \n    source.copyTo(result(cv::Rect(0, 0, col, row)));  \n    return result;  \n}  \n  \n\/\/ \u68c0\u6d4b\u51fd\u6570  \nvoid detect(cv::Mat &amp;image, cv::dnn::Net &amp;net, std::vector&lt;Detection&gt; &amp;output, const std::vector&lt;std::string&gt; &amp;className) {  \n    cv::Mat blob;  \n  \n    \/\/ \u5c06\u8f93\u5165\u56fe\u50cf\u683c\u5f0f\u5316\u4ee5\u6ee1\u8db3\u6a21\u578b\u8f93\u5165\u8981\u6c42  \n    auto input_image = format_yolov5(image);  \n      \n    \/\/ \u5c06\u56fe\u50cf\u8f6c\u6362\u4e3ablob\u5e76\u5c06\u5176\u4f5c\u4e3a\u7f51\u7edc\u7684\u8f93\u5165  \n    cv::dnn::blobFromImage(input_image, blob, 1.\/255., cv::Size(INPUT_WIDTH, INPUT_HEIGHT), cv::Scalar(), true, false);  \n    net.setInput(blob);  \n    std::vector&lt;cv::Mat&gt; outputs;  \n    net.forward(outputs, net.getUnconnectedOutLayersNames());  \n  \n    \/\/ \u7f29\u653e\u56e0\u5b50\u7528\u4e8e\u5c06\u8fb9\u754c\u6846\u6620\u5c04\u56de\u539f\u59cb\u56fe\u50cf\u5927\u5c0f  \n    float x_factor = input_image.cols \/ INPUT_WIDTH;  \n    float y_factor = input_image.rows \/ INPUT_HEIGHT;  \n      \n    float *data = (float *)outputs[0].data;  \n  \n    const int dimensions = 85;  \n    const int rows = 25200;  \n      \n    std::vector&lt;int&gt; class_ids; \/\/ \u5b58\u50a8\u68c0\u6d4b\u7684\u7c7b\u522bID  \n    std::vector&lt;float&gt; confidences; \/\/ \u5b58\u50a8\u68c0\u6d4b\u7684\u7f6e\u4fe1\u5ea6  \n    std::vector&lt;cv::Rect&gt; boxes;   \/\/ \u5b58\u50a8\u8fb9\u754c\u6846  \n  \n   \/\/ \u5faa\u73af\u5904\u7406\u6240\u6709\u884c\u4ee5\u5904\u7406\u9884\u6d4b  \n    for (int i = 0; i &lt; rows; ++i) {  \n  \n        \/\/ \u83b7\u53d6\u5f53\u524d\u68c0\u6d4b\u7684\u7f6e\u4fe1\u5ea6  \n        float confidence = data[4];  \n  \n        \/\/ \u53ea\u5904\u7406\u7f6e\u4fe1\u5ea6\u9ad8\u4e8e\u9608\u503c\u7684\u68c0\u6d4b  \n        if (confidence &gt;= CONFIDENCE_THRESHOLD) {  \n              \n            \/\/ \u83b7\u53d6\u7c7b\u522b\u5206\u6570\u5e76\u627e\u5230\u6700\u9ad8\u5206\u7684\u7c7b\u522b  \n            float * classes_scores = data + 5;  \n            cv::Mat scores(1, className.size(), CV_32FC1, classes_scores);  \n            cv::Point class_id;  \n            double max_class_score;  \n            minMaxLoc(scores, 0, &amp;max_class_score, 0, &amp;class_id);  \n  \n            \/\/ \u5982\u679c\u7c7b\u522b\u5206\u6570\u9ad8\u4e8e\u9608\u503c\uff0c\u5219\u5b58\u50a8\u68c0\u6d4b  \n            if (max_class_score &gt; SCORE_THRESHOLD) {  \n  \n                confidences.push_back(confidence);  \n                class_ids.push_back(class_id.x);  \n  \n                \/\/ \u8ba1\u7b97\u8fb9\u754c\u6846\u5750\u6807  \n                float x = data[0];  \n                float y = data[1];  \n                float w = data[2];  \n                float h = data[3];  \n                int left = int((x - 0.5 * w) * x_factor);  \n                int top = int((y - 0.5 * h) * y_factor);  \n                int width = int(w * x_factor);  \n                int height = int(h * y_factor);  \n                boxes.push_back(cv::Rect(left, top, width, height));  \n            }  \n        }  \n  \n        data += 85;  \n    }  \n  \n    \/\/ \u5e94\u7528\u975e\u6700\u5927\u6291\u5236  \n    std::vector&lt;int&gt; nms_result;  \n    cv::dnn::NMSBoxes(boxes, confidences, SCORE_THRESHOLD, NMS_THRESHOLD, nms_result);  \n  \n    \/\/ \u7ed8\u5236NMS\u8fc7\u6ee4\u540e\u7684\u8fb9\u754c\u6846\u5e76\u63a8\u9001\u7ed3\u679c  \n    for (int i = 0; i &lt; nms_result.size(); i++) {  \n        int idx = nms_result[i];  \n  \n        \/\/ \u53ea\u63a8\u9001\u8fc7\u6ee4\u540e\u7684\u68c0\u6d4b  \n        Detection result;  \n        result.class_id = class_ids[idx];  \n        result.confidence = confidences[idx];  \n        result.box = boxes[idx];  \n        output.push_back(result);  \n  \n        \/\/ \u7ed8\u5236\u6700\u7ec8\u7684NMS\u8fb9\u754c\u6846\u548c\u6807\u7b7e  \n        cv::rectangle(image, boxes[idx], cv::Scalar(0, 255, 0), 3);  \n        std::string label = className[class_ids[idx]];  \n        cv::putText(image, label, cv::Point(boxes[idx].x, boxes[idx].y - 5), cv::FONT_HERSHEY_SIMPLEX, 2, cv::Scalar(255, 255, 255), 2);  \n    }  \n}  \n  \n  \nint main(int argc, char **argv)  \n{     \n    \/\/ \u52a0\u8f7d\u7c7b\u522b\u5217\u8868   \n    std::vector&lt;std::string&gt; class_list = load_class_list();  \n  \n    \/\/ \u52a0\u8f7d\u8f93\u5165\u56fe\u50cf  \n    std::string image_path = cv::samples::findFile(\"C:\/Users\/sirom\/Desktop\/cpp-ultralytics\/test2.jpg\");  \n    cv::Mat frame = cv::imread(image_path, cv::IMREAD_COLOR);  \n  \n    \/\/ \u52a0\u8f7d\u6a21\u578b  \n    cv::dnn::Net net;  \n    load_net(net);  \n  \n    \/\/ \u5411\u91cf\u7528\u4e8e\u5b58\u50a8\u68c0\u6d4b\u7ed3\u679c  \n    std::vector&lt;Detection&gt; output;  \n    \/\/ \u5728\u8f93\u5165\u56fe\u50cf\u4e0a\u8fd0\u884c\u68c0\u6d4b  \n    detect(frame, net, output, class_list);  \n  \n    \/\/ \u5c06\u7ed3\u679c\u4fdd\u5b58\u5230\u6587\u4ef6  \n    cv::imwrite(\"C:\/Users\/sirom\/Desktop\/cpp-ultralytics\/result.jpg\", frame);  \n  \n    while (true)  \n    {         \n        \/\/ \u663e\u793a\u56fe\u50cf  \n        cv::imshow(\"image\",frame);  \n  \n        \/\/ \u5982\u679c\u6309\u4efb\u610f\u952e\u5219\u9000\u51fa\u5faa\u73af  \n        if (cv::waitKey(1) != -1)  \n        {  \n            std::cout &lt;&lt; \"\u7531\u7528\u6237\u5b8c\u6210\\n\";  \n            break;  \n        }  \n    }  \n  \n    std::cout &lt;&lt; \"\u5904\u7406\u5b8c\u6210\u3002\u56fe\u50cf\u5df2\u4fdd\u5b58 \/n\";  \n    return 0;  \n}\n<\/code><\/pre>\n<h2>5\u3001\u7f16\u8bd1\u5e76\u8fd0\u884c\u4ee3\u7801<\/h2>\n<pre><code>mkdir build  \ncd build   \ncmake ..  \ncmake --build .  \n.\\Debug\\object-detection.exe\n<\/code><\/pre>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u662f\u5173\u4e8e\u5982\u4f55\u5728CPU\u4e0a\u8fd0\u884cYOLOv5\u6a21\u578b\uff0c\u800c\u4e0d\u662fGPU\u3002\u8981\u5728GPU\u4e0a\u8fd0\u884c\u6a21\u578b\uff0c\u9700\u8981\u5b89\u88c5CUDA\u3001CUDNN\u7b49\uff0c\u8fd9\u53ef\u80fd\u4f1a\u8ba9\u4eba\u611f\u5230\u56f0\u60d1\u4e14\u8017\u65f6\u3002\u6211\u5c06\u5728\u672a\u6765\u5199\u53e6\u4e00\u7bc7\u6587\u7ae0\u4ecb\u7ecd\u5982\u4f55\u5728\u652f\u6301CUDA\u7684\u60c5\u51b5\u4e0b\u8fd0\u884cYOLO\u6a21\u578b\u3002 \u76ee\u524d\uff0c\u4f60\u53ea\u9700\u8981\u5b89\u88c5OpenCV\u5e93\u3002\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5\u5b83\uff0c\u53ef\u4ee5\u4ece\u8fd9\u4e2a\u5b89\u88c5\u3002 \u90a3\u4e48\uff0c\u8ba9\u6211\u4eec\u5f00\u59cb\u5427\u3002\u6211\u4f1a\u4e00\u6b65\u4e00\u6b65\u5730\u8bb2\u89e3\uff0c\u5b8c\u6574\u7684\u4ee3\u7801\u53ef\u4ee5\u5728\u9875\u9762\u5e95\u90e8\u67e5\u770b\u3002 1\u3001\u5bfc\u51faYOLOv5\u6a21\u578b\u4e3aONNX\u6a21\u578b \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u5939\u5e76\u547d\u540d\u4e3a\u4f60\u60f3\u8981\u7684\u540d\u79f0\u3002\u6253\u5f00\u7ec8\u7aef\u5e76\u5c06yolov5\u4ed3\u5e93\u514b\u9686\u5230\u8be5\u6587\u4ef6\u5939\u4e2d\u3002\u6211\u4eec\u5c06\u4f7f\u7528\u6b64\u4ed3\u5e93\u5bfc\u51fa\u6a21\u578b\u4e3aonnx\u683c\u5f0f\u3002 git clone https:\/\/github.com\/ultralytics\/yolov5 \u6211\u5c06\u4f7f\u7528yolov5s.pt\u6a21\u578b\uff0c\u4f46\u4f60\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684YOLOv5\u6a21\u578b\uff0c\u8fc7\u7a0b\u4e0d\u4f1a\u6539\u53d8\u3002\u4f60\u53ef\u4ee5\u4ece\u8fd9\u4e2a\u4e0b\u8f7d\u9884\u8bad\u7ec3\u6a21\u578b\uff0c\u6216\u8005\u4f7f\u7528\u4f60\u81ea\u5df1\u7684\u6a21\u578b\u3002\u5982\u679c\u4f60\u4e0d\u77e5\u9053\u5982\u4f55\u8bad\u7ec3\u81ea\u5b9a\u4e49YOLO\u6a21\u578b\uff0c\u4e0d\u7528\u62c5\u5fc3\uff0c\u6211\u5df2\u7ecf\u6709\u4e00\u7bc7\u6587\u7ae0\u4ecb\u7ecd\u4e86\u8fd9\u4e2a\u5185\u5bb9\uff0c\u4f60\u53ef\u4ee5\u67e5\u770b\u5b83\uff08\uff09 \u9884\u8bad\u7ec3YOLOv5\u6a21\u578b \u73b0\u5728\u8ba9\u6211\u4eec\u5c06\u6a21\u578b\u5bfc\u51fa\u4e3aonnx\u3002\u6709\u4e0d\u540c\u53c2\u6570\uff0c\u4f60\u53ef\u4ee5\u53c2\u8003\u4e0b\u9762\u7684\u56fe\u7247\u3002\u4f60\u53ef\u4ee5\u7f16\u8f91\u201c\u201d\u6587\u4ef6\uff08yolov5\/export.py\uff09\uff0c\u6216\u8005\u50cf\u6211\u5728\u8fd9\u91cc\u4e00\u6837\u624b\u52a8\u66f4\u6539\u53c2\u6570\u3002\u5bf9\u4e8e\u81ea\u5b9a\u4e49\u6a21\u578b\uff0c\u4f60\u9700\u8981\u5c06\u6743\u91cd\u66f4\u6539\u4e3a\u4f60\u7684\u81ea\u5b9a\u4e49\u6a21\u578b\u6743\u91cd\uff08your_model.pt\u6587\u4ef6\uff09\u3002 python yolov5\/export.py &#8211;weights yolov5s.pt &#8211;img 640 &#8211;include onnx &#8211;opset 12 \u6ce8\u610f\uff1a\u4f60\u9700\u8981\u5c06opset\u8bbe\u7f6e\u4e3a12\uff0c\u5426\u5219\u53ef\u80fd\u4f1a\u62a5\u9519\u3002\u8fd9\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u5728GitHub\u4e0a\u67e5\u627e\u6b64\u9519\u8bef\u3002 parameers\/ export.py\u6587\u4ef6 2\u3001\u521b\u5efaTXT\u6587\u4ef6\u5b58\u50a8YOLO\u6a21\u578b\u6807\u7b7e \u8fd9\u4e00\u6b65\u5f88\u7b80\u5355\uff0c\u4f60\u53ea\u9700\u8981\u521b\u5efa\u4e00\u4e2atxt\u6587\u4ef6\u6765\u5b58\u50a8\u6807\u7b7e\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u9884\u8bad\u7ec3\u7684YOLO\u6a21\u578b\uff0c\u53ef\u4ee5\u4ece\u6b64\u76f4\u63a5\u4e0b\u8f7dtxt\u6587\u4ef6\u3002\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6a21\u578b\uff0c\u5219\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684txt\u6587\u4ef6\u5e76\u5728\u5176\u4e2d\u5199\u5165\u4f60\u7684\u6807\u7b7e\u3002\u4f60\u53ef\u4ee5\u7ed9\u8fd9\u4e2a\u6587\u4ef6\u547d\u540d\u4efb\u4f55\u4f60\u60f3\u8981\u7684\u540d\u5b57\uff0c\u8fd9\u5e76\u4e0d\u91cd\u8981\u3002 coco-classes.txt 3\u3001\u521b\u5efaCMakeLists.txt\u6587\u4ef6 \u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2aCMakeLists.txt\u6587\u4ef6\u3002\u5f53\u4f60\u4f7f\u7528CMake\u7f16\u8bd1C++\u7a0b\u5e8f\u65f6\u9700\u8981\u8fd9\u4e2a\u6587\u4ef6\u3002\u5982\u679c\u4f60\u6309\u7167\u6211\u63d0\u4f9b\u7684\u94fe\u63a5\u5b89\u88c5\u4e86OpenCV\uff0c\u4f60\u5e94\u8be5\u5df2\u7ecf\u5b89\u88c5\u4e86CMake\u3002 cmake_minimum_required(VERSION 3.10) project(cpp-yolo-detection) # \u4f60\u7684\u6587\u4ef6\u5939\u540d\u79f0 # \u67e5\u627eOpenCV set(OpenCV_DIR C:\/Libraries\/opencv\/build) # OpenCV\u8def\u5f84 find_package(OpenCV REQUIRED) add_executable(object-detection object-detection.cpp) # \u4f60\u7684\u6587\u4ef6\u540d # \u94fe\u63a5OpenCV\u5e93 target_link_libraries(object-detection ${OpenCV_LIBS}) 4\u3001\u4ee3\u7801 \u8fd9\u662f\u6700\u540e\u4e00\u6b65\u3002\u6211\u4f7f\u7528\u4e86\u8fd9\u4e2a\u4e2d\u7684\u4ee3\u7801\uff0c\u4f46\u4fee\u6539\u4e86\u4e00\u4e9b\u90e8\u5206\uff0c\u5e76\u6dfb\u52a0\u4e86\u6ce8\u91ca\u4ee5\u4fbf\u66f4\u597d\u5730\u7406\u89e3\u3002 #include &lt;fstream&gt; #include [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-53728","post","type-post","status-publish","format-standard","hentry","category-ai"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53728","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=53728"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53728\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}