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使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

精选20个好玩又实用的的Python实战项目(有图文代码)

《精选20个好玩又实用的的Python实战项目(有图文代码)》文章介绍了20个实用Python项目,涵盖游戏开发、工具应用、图像处理、机器学习等,使用Tkinter、PIL、OpenCV、Kivy等库... 目录① 猜字游戏② 闹钟③ 骰子模拟器④ 二维码⑤ 语言检测⑥ 加密和解密⑦ URL缩短⑧ 音乐播放

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

python使用try函数详解

《python使用try函数详解》Pythontry语句用于异常处理,支持捕获特定/多种异常、else/final子句确保资源释放,结合with语句自动清理,可自定义异常及嵌套结构,灵活应对错误场景... 目录try 函数的基本语法捕获特定异常捕获多个异常使用 else 子句使用 finally 子句捕获所

C++11范围for初始化列表auto decltype详解

《C++11范围for初始化列表autodecltype详解》C++11引入auto类型推导、decltype类型推断、统一列表初始化、范围for循环及智能指针,提升代码简洁性、类型安全与资源管理效... 目录C++11新特性1. 自动类型推导auto1.1 基本语法2. decltype3. 列表初始化3