linux C++ onnxruntime yolov8 视频检测Demo

news2024/9/21 14:50:21

linux C++ onnxruntime yolov8 视频检测Demo

目录

项目目录

效果

​编辑CMakeLists.txt

代码 

下载


项目目录

效果

./yolov8_demo --help

./yolov8_demo -c=2 -p=true

./yolov8_demo -c=1 -s=true

CMakeLists.txt

# cmake needs this line
cmake_minimum_required(VERSION 3.0)

# Define project name
project(yolov8_demo)

# Release模式下的编译指令
# SET(CMAKE_BUILD_TYPE "Release")
# set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -s")
# set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -std=c++17 -pthread -Wall -Wl")

# Debug模式下的编译指令
SET(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_DEBUG}")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -std=c++17 -pthread") 

set(OpenCV_LIBS opencv_videoio opencv_imgcodecs opencv_imgproc opencv_core opencv_dnn opencv_highgui)

include_directories(
    /usr/local/include/opencv4
    ${PROJECT_SOURCE_DIR}/include
    ${PROJECT_SOURCE_DIR}/include/onnxruntime
)

link_directories(
    ${PROJECT_SOURCE_DIR}/lib/onnxruntime   # 第三方动态库文件
    /usr/local/lib/
)

#递归指定源码的路径
file(GLOB_RECURSE SRCS ${PROJECT_SOURCE_DIR}/src/*.cpp)

# Declare the executable target built from your sources
add_executable(yolov8_demo ${SRCS})

# Link your application with OpenCV libraries
target_link_libraries(yolov8_demo 
-lonnxruntime
${OpenCV_LIBS}
)

代码 

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;

}

#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;
}

下载

源码下载

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1943651.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

力扣最热一百题——3.最长连续序列

目录 题目链接&#xff1a;128. 最长连续序列 - 力扣&#xff08;LeetCode&#xff09; 题目描述 示例 提示 解法一&#xff1a;排序双指针剪枝 思路 1. 获取数组长度并进行特判 2. 对数组进行排序 3. 初始化变量 4. 遍历数组并寻找最长连续子序列 5. 返回结果 总结…

Linux笔记-对.a静态库的进一步理解(2024-04-09)

过程 问&#xff1a; Linux中生成.a库时候&#xff0c;如果代码里面调用了一些只引用未定义的函数&#xff0c;gcc不报错&#xff0c;但能生成对应的.a文件&#xff0c;这是为什么&#xff1f;再写一个执行程序去调用.a库时&#xff0c;链接时就会报这个.a库未定义的引用&…

列举excel中调整行高列宽的五种方法

列举excel中调整行高列宽的五种方法 在Excel中调整行高列宽的方法有以下五种&#xff1a; 使用鼠标手动调整行高列宽&#xff1a;将鼠标悬停在行或列的边界上&#xff0c;光标会变成双向箭头&#xff0c;此时按住鼠标左键并拖动边界即可调整行高或列宽。 使用快捷键调整行高列…

node和npm安装;electron、 electron-builder安装

1、node和npm安装 参考&#xff1a; https://blog.csdn.net/sw150811426/article/details/137147783 下载&#xff1a; https://nodejs.org/dist/v20.15.1/ 安装&#xff1a; 点击下载msi直接运行安装 安装完直接cmd打开可以&#xff0c;默认安装就已经添加了环境变量&…

vue3通过html2canvas dom转图片复制到剪贴板和dom转图片并下载

代码实现 <template><div class"page"><div id"to-img-dom"><strong>我是图片标题</strong><p>我是内容&#xff0c;我是内容&#xff0c;我是内容&#xff0c;我是内容&#xff0c;我是内容&#xff0c;我是内容&am…

Jupyter notebook 快速入门

1、什么是jupyter notebook Jupyter Notebook是一个交互式笔记本环境&#xff0c;可以在其中同时编写和运行代码&#xff0c;以及进行数据分析和可视化。它支持多种编程语言&#xff08;如Python、R、Julia等&#xff09;&#xff0c;并提供了丰富的功能和工具供用户使用。Jup…

如何减少网站延迟?

什么是网络延迟&#xff1f; Web 延迟描述了网站响应用户请求所花费的时间。它是网络性能的一个重要因素&#xff0c;因为它决定了用户访问网站内容并与之交互的速度。当延迟很高时&#xff0c;网站会变得缓慢且反应迟钝&#xff0c;从而导致用户不满意。延迟可能由多种因素引…

如何用 WinDbg 调试Linux上的 .NET程序

一&#xff1a;背景 1. 讲故事 最新版本 1.2402.24001.0 的WinDbg真的让人很兴奋&#xff0c;可以将自己伪装成 GDB 来和远程的 GDBServer 打通来实现对 Linux 上 .NET程序进行调试&#xff0c;这样就可以继续使用熟悉的WinDbg 命令&#xff0c;在这个版本中我觉得 WinDbg 不…

数据结构C++——二叉树和树

文章目录 一、树二、二叉树三、二叉树的特性特性1. 包含n (n> 0 )个元素的二叉树边数为n-1特性 2: 若二叉树的高度为h,h≥0,则该二叉树最少有h个元素,最多有2^h-1 个元素特性3:包含n(n≥0)个元素的二叉树的高度最大为n,最小为ceiling(log2(n+1))特性4. 完全二叉树四、二…

基于微信小程序+SpringBoot+Vue的高校校园交友(带1w+文档)

基于微信小程序SpringBootVue的高校校园交友(带1w文档) 基于微信小程序SpringBootVue的高校校园交友(带1w文档) 在目前的情况下&#xff0c;可以引进一款高校校园交友微信小程序这样的现代化管理工具&#xff0c;这个工具就是解决上述问题的最好的解决方案。它不仅可以实时完成…

代码随想录 day 21 二叉树

第六章 二叉树part08 669. 修剪二叉搜索树 这道题目比较难&#xff0c;比 添加增加和删除节点难的多&#xff0c;建议先看视频理解。 题目链接/文章讲解&#xff1a; https://programmercarl.com/0669.%E4%BF%AE%E5%89%AA%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91.html…

行业、客户双认可!路凯智行精彩亮相新疆煤博会

7月18日&#xff0c;2024丝路矿业合作论坛、第14届中国新疆国际矿业与装备博览会、第19届中国新疆国际煤炭工业博览会在新疆国际会展中心举行&#xff0c;此次论坛和展会吸引了全国26个省区市和德国、美国、日本、挪威、芬兰、法国、巴基斯坦、俄罗斯、白俄罗斯、乌兹别克斯坦等…

企业如何应对大模型落地的四大挑战?

近年来&#xff0c;人工智能&#xff08;AI&#xff09;和机器学习&#xff08;ML&#xff09;技术取得了突飞猛进的发展&#xff0c;其中大模型&#xff08;如GPT-3、GPT-4、BERT等&#xff09;因其强大的数据处理和分析能力&#xff0c;受到了企业的高度重视。大模型在自然语…

使用nginx实现一个端口和ip访问多个vue前端

前言&#xff1a;由于安全组要求&#xff0c;前端页面只开放一个端口&#xff0c;但是项目有多个前端&#xff0c;此前一直使用的是一个前端使用单独一个端口进行访问&#xff0c;现在需要调整。 需要实现&#xff1a;这里以80端口为例&#xff0c;两个前端分别是&#xff1a;p…

[用AI日进斗金系列]用码上飞在企微接单开发一个项目管理系统!

今天是【日进斗金】系列的第二期文章。 先给不了解这个系列的朋友们介绍一下&#xff0c;在这个系列的文章中&#xff0c;我们将会在企微的工作台的“需求发布页面”中寻找有软件开发需求的用户 并通过自研的L4级自动化智能软件开发平台「码上飞CodeFlying」让AI生成应用以解…

可以免费合并pdf的软件 合并pdf文件的软件免费 合并pdf的软件免费

在数字化办公的今天&#xff0c;pdf格式因其稳定性和跨平台兼容性被广泛使用。然而&#xff0c;当我们需要将多个 pdf 文件合并为一个时&#xff0c;却往往感到力不从心。本文将为你介绍几款强大的pdf文件合并软件&#xff0c;让你轻松管理文档。 方法一、使用pdf转换器 步骤1…

Vue3 对比 Vue2

相关信息简介2020年9月18日&#xff0c;Vue.js发布3.0版本&#xff0c;代号&#xff1a;One Piece&#xff08;海贼王&#xff09; 2 年多开发, 100位贡献者, 2600次提交, 600次 PR、30个RFC Vue3 支持 vue2 的大多数特性 可以更好的支持 Typescript&#xff0c;提供了完整的…

亚马逊跟卖北美选品ERP操作注意事项,商标自动查询,可...

新手在美国站选品有哪几种方式呢&#xff1f;美国商标局复查未备案准确度100%。 今天来讲下美国品牌未备案准不准确。 点开采集任务&#xff0c;站点选择美国&#xff0c;有五种采集方式&#xff1a;关键词、店铺链接、类目asin&#xff0c;选择完之后点确定。 选择asin采集…

【Docker】Docker-consul容器服务自动发现与注册

目录 一.Consul概述 1.解决了什么问题 2.什么叫微服务或者注册与发现 3.consul的模式 4.相关命令 二.consul 部署 1.consul服务器部署 2.部署docker容器 3.Nginx负载均衡器 3.1.安装启动nginx 3.2.配置nginx负载均衡 3.3.创建配置consul complate模板文件 3.4.添加…

只需三步,即可使用 Kafka 托管服务快速部署微服务架构应用

微服务架构的应用程序的特点是将其组件组织得能够独立地进行开发、测试、部署和扩展。DigitalOcean App Platform&#xff08;应用平台&#xff09;的目标是通过允许用户在同一应用上添加多个组件&#xff0c;简化这一架构模型&#xff0c;使其更加平滑和易于管理。 一个简单的…