首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

linux C++ onnxruntime yolov8 视频检测Demo

  • 25-02-19 03:41
  • 2592
  • 11266
blog.csdn.net

linux C++ onnxruntime yolov8 视频检测Demo

目录

项目目录

效果

​编辑CMakeLists.txt

代码 

下载


项目目录

效果

./yolov8_demo --help

./yolov8_demo -c=2 -p=true

./yolov8_demo -c=1 -s=true

CMakeLists.txt

  1. # cmake needs this line
  2. cmake_minimum_required(VERSION 3.0)
  3. # Define project name
  4. project(yolov8_demo)
  5. # Release模式下的编译指令
  6. # SET(CMAKE_BUILD_TYPE "Release")
  7. # set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -s")
  8. # set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -std=c++17 -pthread -Wall -Wl")
  9. # Debug模式下的编译指令
  10. SET(CMAKE_BUILD_TYPE "Debug")
  11. set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_DEBUG}")
  12. set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -std=c++17 -pthread")
  13. set(OpenCV_LIBS opencv_videoio opencv_imgcodecs opencv_imgproc opencv_core opencv_dnn opencv_highgui)
  14. include_directories(
  15. /usr/local/include/opencv4
  16. ${PROJECT_SOURCE_DIR}/include
  17. ${PROJECT_SOURCE_DIR}/include/onnxruntime
  18. )
  19. link_directories(
  20. ${PROJECT_SOURCE_DIR}/lib/onnxruntime # 第三方动态库文件
  21. /usr/local/lib/
  22. )
  23. #递归指定源码的路径
  24. file(GLOB_RECURSE SRCS ${PROJECT_SOURCE_DIR}/src/*.cpp)
  25. # Declare the executable target built from your sources
  26. add_executable(yolov8_demo ${SRCS})
  27. # Link your application with OpenCV libraries
  28. target_link_libraries(yolov8_demo
  29. -lonnxruntime
  30. ${OpenCV_LIBS}
  31. )

代码 

main.cpp

#include <opencv2/core.hpp>

#include

#include

#include

#include

#include

#include

int VideoDet(int index, bool showDet, bool useGPU, bool printPerStepInfo)

{

    size_t threadId = static_cast(syscall(SYS_gettid));

    // std::cout << "index:" << index << " thread id: " << threadId << std::endl;

    cv::VideoCapture capture("./test/test_car_person_1080P.mp4");

    // 检查视频是否成功打开

    if (!capture.isOpened())

    {

        std::cout << "无法读取视频文件" << std::endl;

        return -1;

    }

    int frameCount = capture.get(cv::CAP_PROP_FRAME_COUNT); // 获取视频帧数

    double fps = capture.get(cv::CAP_PROP_FPS);             // 获取帧率

    int delay = int(1000 / fps);                            // 根据帧率计算帧间间隔时间

    // delay=1;

    std::string model_path = "./models/yolov8n.onnx";

    std::string lable_path = "./models/lable.txt";

    int GPUCount = 2;

    int device_id = 0;

    if (index >= GPUCount)

    {

        device_id = index % GPUCount;

    }

    else

    {

        device_id = index;

    }

    // device_id=0;

    YoloV8 yoloV8(model_path, lable_path, useGPU, device_id);

    yoloV8.index = index;

    yoloV8.threadId = threadId;

    yoloV8.videoFps = fps;

    yoloV8.frameCount = frameCount;

    // std::cout << "device_id:" << yoloV8.device_id << std::endl;

    // vector detectionResult;

    // Mat frame=cv::imread("../test/dog.jpg");

    // yoloV8.Detect(frame, detectionResult);

    // std::cout << "detectionResult size:" << detectionResult.size() << std::endl;

    string winname = "detectionResult-" + std::to_string(index);

    while (true)

    {

        double start = (double)cv::getTickCount();

        delay = int(1000 / fps);

        Mat frame;

        bool success = capture.read(frame); // 读取一帧数据

        // 检查是否成功读取帧

        if (!success)

        {

            std::cout << "index:" << index << ",读取完毕" << std::endl;

            yoloV8.PrintAvgCostTime();

            break;

        }

        vector detectionResult;

        yoloV8.Detect(frame, detectionResult);

        // std::cout <<"index:"<

        yoloV8.detectionResultSize = detectionResult.size();

        if (printPerStepInfo)

        {

            yoloV8.PrintCostTime();

            yoloV8.PrintAvgCostTime();

        }

        if (showDet)

        {

            yoloV8.Draw(frame, detectionResult);

            imshow(winname, frame);

            double costTime = ((double)getTickCount() - start) / getTickFrequency();

            delay = delay - costTime;

            if (delay <= 0)

            {

                delay = 1;

            }

            if (waitKey(delay) == 27) // 通过按下ESC键退出循环

            {

                break;

            }

        }

    }

    capture.release(); // 释放视频对象

    if (showDet)

    {

        cv::destroyWindow(winname);

    }

    return 0;

}

int main(int argc, char *const argv[])

{

    int threadCount = 1;

    bool showDet = false;

    bool useGPU = false;

    bool printPerStepInfo = true;

    const char *keys ="{h help                || print this message}"

        "{c threadCount         | 1     | run thread number}"

        "{s showDet             | false | show detection result}"

        "{g useGPU              | false | use GPU}"

        "{p printPerStepInfo    | false | print per Step Info}";

    cv::CommandLineParser parser(argc, argv, keys);

    if(parser.has("help"))

    {

        parser.about("YoloV8 demo v1.0.0");

        parser.printMessage();

        return 0;

    }

    threadCount=parser.get("threadCount");

    showDet=parser.get("showDet");

    useGPU=parser.get("useGPU");

    printPerStepInfo=parser.get("printPerStepInfo");

    std::cout << std::boolalpha;

    std::cout << "threadCount:" << threadCount << ",showDet:" << showDet<< ",useGPU:" << useGPU << ",printPerStepInfo:" << printPerStepInfo << std::endl;

    for (size_t i = 0; i < threadCount; i++)

    {

        std::thread thread(VideoDet, i, showDet, useGPU, printPerStepInfo);

        thread.detach();

    }

    while (true)

    {

        sleep(100);

    }

    return 0;

}

  1. #include <opencv2/core.hpp>
  2. #include <opencv2/highgui.hpp>
  3. #include <iostream>
  4. #include <YoloV8.hpp>
  5. #include <unistd.h>
  6. #include <sys/syscall.h>
  7. #include <thread>
  8. int VideoDet(int index, bool showDet, bool useGPU, bool printPerStepInfo)
  9. {
  10. size_t threadId = static_cast<size_t>(syscall(SYS_gettid));
  11. // std::cout << "index:" << index << " thread id: " << threadId << std::endl;
  12. cv::VideoCapture capture("./test/test_car_person_1080P.mp4");
  13. // 检查视频是否成功打开
  14. if (!capture.isOpened())
  15. {
  16. std::cout << "无法读取视频文件" << std::endl;
  17. return -1;
  18. }
  19. int frameCount = capture.get(cv::CAP_PROP_FRAME_COUNT); // 获取视频帧数
  20. double fps = capture.get(cv::CAP_PROP_FPS); // 获取帧率
  21. int delay = int(1000 / fps); // 根据帧率计算帧间间隔时间
  22. // delay=1;
  23. std::string model_path = "./models/yolov8n.onnx";
  24. std::string lable_path = "./models/lable.txt";
  25. int GPUCount = 2;
  26. int device_id = 0;
  27. if (index >= GPUCount)
  28. {
  29. device_id = index % GPUCount;
  30. }
  31. else
  32. {
  33. device_id = index;
  34. }
  35. // device_id=0;
  36. YoloV8 yoloV8(model_path, lable_path, useGPU, device_id);
  37. yoloV8.index = index;
  38. yoloV8.threadId = threadId;
  39. yoloV8.videoFps = fps;
  40. yoloV8.frameCount = frameCount;
  41. // std::cout << "device_id:" << yoloV8.device_id << std::endl;
  42. // vector<DetectionResult> detectionResult;
  43. // Mat frame=cv::imread("../test/dog.jpg");
  44. // yoloV8.Detect(frame, detectionResult);
  45. // std::cout << "detectionResult size:" << detectionResult.size() << std::endl;
  46. string winname = "detectionResult-" + std::to_string(index);
  47. while (true)
  48. {
  49. double start = (double)cv::getTickCount();
  50. delay = int(1000 / fps);
  51. Mat frame;
  52. bool success = capture.read(frame); // 读取一帧数据
  53. // 检查是否成功读取帧
  54. if (!success)
  55. {
  56. std::cout << "index:" << index << ",读取完毕" << std::endl;
  57. yoloV8.PrintAvgCostTime();
  58. break;
  59. }
  60. vector<DetectionResult> detectionResult;
  61. yoloV8.Detect(frame, detectionResult);
  62. // std::cout <<"index:"<<index<< " thread id: " << threadId << " detectionResult size: " << detectionResult.size() << std::endl;
  63. yoloV8.detectionResultSize = detectionResult.size();
  64. if (printPerStepInfo)
  65. {
  66. yoloV8.PrintCostTime();
  67. yoloV8.PrintAvgCostTime();
  68. }
  69. if (showDet)
  70. {
  71. yoloV8.Draw(frame, detectionResult);
  72. imshow(winname, frame);
  73. double costTime = ((double)getTickCount() - start) / getTickFrequency();
  74. delay = delay - costTime;
  75. if (delay <= 0)
  76. {
  77. delay = 1;
  78. }
  79. if (waitKey(delay) == 27) // 通过按下ESC键退出循环
  80. {
  81. break;
  82. }
  83. }
  84. }
  85. capture.release(); // 释放视频对象
  86. if (showDet)
  87. {
  88. cv::destroyWindow(winname);
  89. }
  90. return 0;
  91. }
  92. int main(int argc, char *const argv[])
  93. {
  94. int threadCount = 1;
  95. bool showDet = false;
  96. bool useGPU = false;
  97. bool printPerStepInfo = true;
  98. const char *keys ="{h help || print this message}"
  99. "{c threadCount | 1 | run thread number}"
  100. "{s showDet | false | show detection result}"
  101. "{g useGPU | false | use GPU}"
  102. "{p printPerStepInfo | false | print per Step Info}";
  103. cv::CommandLineParser parser(argc, argv, keys);
  104. if(parser.has("help"))
  105. {
  106. parser.about("YoloV8 demo v1.0.0");
  107. parser.printMessage();
  108. return 0;
  109. }
  110. threadCount=parser.get<int>("threadCount");
  111. showDet=parser.get<bool>("showDet");
  112. useGPU=parser.get<bool>("useGPU");
  113. printPerStepInfo=parser.get<bool>("printPerStepInfo");
  114. std::cout << std::boolalpha;
  115. std::cout << "threadCount:" << threadCount << ",showDet:" << showDet<< ",useGPU:" << useGPU << ",printPerStepInfo:" << printPerStepInfo << std::endl;
  116. for (size_t i = 0; i < threadCount; i++)
  117. {
  118. std::thread thread(VideoDet, i, showDet, useGPU, printPerStepInfo);
  119. thread.detach();
  120. }
  121. while (true)
  122. {
  123. sleep(100);
  124. }
  125. return 0;
  126. }

下载

源码下载

天天代码码天天
微信公众号
.NET 人工智能实践
注:本文转载自blog.csdn.net的天天代码码天天的文章"https://lw112190.blog.csdn.net/article/details/140647921"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top