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

相关文章

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是