当前位置:   article > 正文

linux C++ onnxruntime yolov8 视频检测Demo

linux C++ onnxruntime yolov8 视频检测Demo

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 <opencv2/highgui.hpp>

#include <iostream>

#include <YoloV8.hpp>

#include <unistd.h>

#include <sys/syscall.h>

#include <thread>

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

{

    size_t threadId = static_cast<size_t>(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> 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> detectionResult;

        yoloV8.Detect(frame, detectionResult);

        // std::cout <<"index:"<<index<< " thread id: " << threadId << " detectionResult size: " << detectionResult.size() << std::endl;

        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<int>("threadCount");

    showDet=parser.get<bool>("showDet");

    useGPU=parser.get<bool>("useGPU");

    printPerStepInfo=parser.get<bool>("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. }

下载

源码下载

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/873778
推荐阅读
相关标签
  

闽ICP备14008679号