c++项目中使用YOLOv4模型简单案例

2024-06-12 08:18

本文主要是介绍c++项目中使用YOLOv4模型简单案例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

主要是使用yolo_v2_class.hpp文件

1、hpp文件
#ifndef DEMO_HPP
#define DEMO_HPP
#ifndef OPENCV
#define OPENCV
#endif
#include<yolo_v2_class.hpp>
#include<darknet.h>
using namespace cv;
using namespace std;
class yoloDetector
{
public:yoloDetector(string cfgfile,string weightfile,float thresh = 0.2, bool use_mean = false,int gpu_id = 0);vector<map<string,float>> detect(Mat imag);~yoloDetector();private:vector<map<string,float>> bbox_to_vector(vector<bbox_t> booxs);vector<bbox_t> detectionObjetc;float prob;float thresh;bool use_mean;Detector* dector;};
yoloDetector::yoloDetector(string cfgfile,string weightfile,float thresh, bool use_mean,int gpu_id ):prob(prob),thresh(thresh),use_mean(use_mean)
{dector = new Detector(cfgfile,weightfile,gpu_id);
}vector<map<string,float>> yoloDetector::detect(Mat imag){vector<bbox_t> boxs= dector->detect(imag,thresh);if(boxs.size()>0){return bbox_to_vector(boxs);}}vector<map<string,float>> yoloDetector::bbox_to_vector(vector<bbox_t> booxs){vector<map<string,float>> im;for (vector<bbox_t>::iterator iter = booxs.begin(); iter != booxs.end(); iter++){if((*iter).prob < prob)continue;map<string, float> ma;ma["x"]=(*iter).x;ma["y"]=(*iter).y;ma["w"]=(*iter).w;ma["h"]=(*iter).h;ma["prob"]=(*iter).prob;ma["obj_id"]=(*iter).obj_id;ma["frames_counter"]=(*iter).frames_counter;ma["x_3d"]=(*iter).x_3d;ma["y_3d"]=(*iter).y_3d;ma["z_3d"]=(*iter).z_3d;im.push_back(ma);}return im;}yoloDetector::~yoloDetector(){delete(dector);}#endif // DEMO_H
2、main文件
#include<demo.hpp>
#include<iostream>Mat huatu(Mat img,vector<map<string,float>> boox,string classname[]){for(vector<map<string,float>>::iterator iter=boox.begin();iter != boox.end(); iter++){float x = (*iter).at("x");float y = (*iter).at("y");float w = (*iter).at("w");float h = (*iter).at("h");float prob = (*iter).at("prob");float obj_id = (*iter).at("obj_id");if(prob>0.8){Rect rect(x, y,w, h);cv::rectangle(img,rect, cv::Scalar(255, 0, 0), 1,LINE_8,0);string pr = to_string(prob);string labe=classname[(int)obj_id] + ' ' + pr;cv::putText(img,labe ,cv::Point(x, y - 13),cv::FONT_HERSHEY_SIMPLEX,0.5,cv::Scalar(0, 255, 0),2);}}//cv::imshow("MyWindow", img);//waitKey(0);return img;}int main(int argc, char const *argv[]){if(argc<5){cout<<"input cfg/weight/(igm/video/cam)/path "<<endl;// ../yolov4Dect ../cfg/yolov4-head-test.cfg ../backup/yolov4-head_best.weights  cam o}string  classname[]={"head"};string cfgfile = argv[1];string weightfile = argv[2];string type=argv[3];string path=argv[4];yoloDetector yd(cfgfile,weightfile);vector<map<string,float>> boox;if(type=="img"){Mat img = cv::imread(path);Mat img2;if(img.data==nullptr){cout<<"path:"<<path<<"error"<<endl;return -1;}else{boox = yd.detect(img);if(boox.size()>0){img2 = huatu(img,boox,classname);}imshow("demo",img2);waitKey(0);}}else if(type=="video"){VideoCapture capture;capture.open(path);double rate = capture.get(CV_CAP_PROP_FPS);int deply = cvRound(1000.000/rate);if(!capture.isOpened()){cout<<"no opend"<<endl;return -1;}while(1){Mat fram;capture>>fram;//resize(fram,img,Size(640.0,480.0));//imshow("demo",fram);if(fram.data!=nullptr){boox = yd.detect(fram);}cout<<boox.size()<<endl;Mat img;if(boox.size()>0){//Mat img2=fram.clone();img=huatu(fram,boox,classname);imshow("demo",img);}waitKey(deply);}}else if(type=="cam"){VideoCapture capture(0);double rate = capture.get(CV_CAP_PROP_FPS);int deply = cvRound(1000.000/rate);if(!capture.isOpened()){cout<<"no opend"<<endl;return -1;}while(1){Mat fram;capture>>fram;//resize(fram,img,Size(640.0,480.0));//imshow("demo",fram);if(fram.data!=nullptr){boox = yd.detect(fram);}cout<<boox.size()<<endl;Mat img;if(boox.size()>0){//Mat img2=fram.clone();img=huatu(fram,boox,classname);imshow("demo",img);}waitKey(deply);}}return 0;}
3、CMakeLists
cmake_minimum_required(VERSION 2.8.3)
project(yolov4Dect)
set(CMAKE_CXX_STANDARD 11)find_package(OpenCV)set(SOURCE_FILES demo3.cpp demo.hpp)add_executable(yolov4Dect ${SOURCE_FILES})include_directories(${OpenCV_INCLUDE_DIRS} "./" "/usr/include" "/headless/docker_mapping/YOLOv4/darknet/include/")link_directories("/headless/docker_mapping/YOLOv4/darknet/" "/usr/local/cuda-10.1/lib64")target_link_libraries(yolov4Dect  ${OpenCV_LIBS}"/headless/docker_mapping/YOLOv4/darknet/libdarknet.so")

简单案例写的比较粗糙,回头有时间在进一步完善

这篇关于c++项目中使用YOLOv4模型简单案例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1053669

相关文章

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

C++中RAII资源获取即初始化

《C++中RAII资源获取即初始化》RAII通过构造/析构自动管理资源生命周期,确保安全释放,本文就来介绍一下C++中的RAII技术及其应用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、核心原理与机制二、标准库中的RAII实现三、自定义RAII类设计原则四、常见应用场景1. 内存管理2. 文件操

C++中零拷贝的多种实现方式

《C++中零拷贝的多种实现方式》本文主要介绍了C++中零拷贝的实现示例,旨在在减少数据在内存中的不必要复制,从而提高程序性能、降低内存使用并减少CPU消耗,零拷贝技术通过多种方式实现,下面就来了解一下... 目录一、C++中零拷贝技术的核心概念二、std::string_view 简介三、std::stri

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

C++高效内存池实现减少动态分配开销的解决方案

《C++高效内存池实现减少动态分配开销的解决方案》C++动态内存分配存在系统调用开销、碎片化和锁竞争等性能问题,内存池通过预分配、分块管理和缓存复用解决这些问题,下面就来了解一下... 目录一、C++内存分配的性能挑战二、内存池技术的核心原理三、主流内存池实现:TCMalloc与Jemalloc1. TCM

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删