c++调用yolov4模型进行目标检测-使用yolov4官方接口

前言

yolo系列用c写的,在工程中的部署特别方便。4月份yolov4横空出世,之前试了试效果,精度确实有了很大的提升,AB大神nb。最近需要在C++项目中使用yolov4,尝试了opencv的调用(见我的上一篇博客这里),yolov4官方也提供了C++接口,并不需要opencv4.4的版本,调用起来也特别简单。

darknet

使用官方接口调用,我们首先得编译darknet动态库,下载yolov4源码

git clone https://github.com/AlexeyAB/darknet.git

进入darknet目录下,修改makefile文件

cd darknet
vim Makefile

其中需要修改的几个地方,OPENCV=1,LIBSO=1,如果需要使用GPU,则设置GPU=1,CUDNN=1。

GPU=1
CUDNN=1
CUDNN_HALF=0
OPENCV=1
AVX=0
OPENMP=0
LIBSO=1
ZED_CAMERA=0
ZED_CAMERA_v2_8=0

计算能力根据自己GPU设定,由于我使用的titan v,所以计算能力为7.0,不使用GPU则忽略。

# Tesla A100 (GA100), DGX-A100, RTX 3080
# ARCH= -gencode arch=compute_80,code=[sm_80,compute_80]

# Tesla V100
ARCH= -gencode arch=compute_70,code=[sm_70,compute_70]

# GeForce RTX 2080 Ti, RTX 2080, RTX 2070, Quadro RTX 8000, Quadro RTX 6000, Quadro RTX 5000, Tesla T4, XNOR Tensor Cores
# ARCH= -gencode arch=compute_75,code=[sm_75,compute_75]

# Jetson XAVIER
# ARCH= -gencode arch=compute_72,code=[sm_72,compute_72]

# GTX 1080, GTX 1070, GTX 1060, GTX 1050, GTX 1030, Titan Xp, Tesla P40, Tesla P4
# ARCH= -gencode arch=compute_61,code=sm_61 -gencode arch=compute_61,code=compute_61

# GP100/Tesla P100 - DGX-1
# ARCH= -gencode arch=compute_60,code=sm_60

# For Jetson TX1, Tegra X1, DRIVE CX, DRIVE PX - uncomment:
# ARCH= -gencode arch=compute_53,code=[sm_53,compute_53]

# For Jetson Tx2 or Drive-PX2 uncomment:
# ARCH= -gencode arch=compute_62,code=[sm_62,compute_62]

修改自己的cuda和cudnn的路径,然后make,之后会生成一个libdarknet.so动态库。

ifeq ($(GPU), 1)
COMMON+= -DGPU -I/usr/local/cuda-10.1/include/
CFLAGS+= -DGPU
ifeq ($(OS),Darwin) #MAC
LDFLAGS+= -L/usr/local/cuda-10.1/lib -lcuda -lcudart -lcublas -lcurand
else
LDFLAGS+= -L/usr/local/cuda-10.1/lib64 -lcuda -lcudart -lcublas -lcurand
endif
endif

ifeq ($(CUDNN), 1)
COMMON+= -DCUDNN
ifeq ($(OS),Darwin) #MAC
CFLAGS+= -DCUDNN -I/usr/local/cuda-10.1.0/include
LDFLAGS+= -L/usr/local/cuda-10.1.0/lib -lcudnn
else
CFLAGS+= -DCUDNN -I/usr/local/cuda-10.1.0/include
LDFLAGS+= -L/usr/local/cuda-10.1.0/lib64 -lcudnn
endif
endif

检测步骤

检测部分很简单,需要用到刚刚编译的动态库,以及yolov4源码下的yolo_v2_class.hpp。
其实就那么几步,首先加载网络

	string classesFile = "./yolo/coco.names";
	string modelConfig = "./yolo/yolov4.cfg";
	string modelWeights = "./yolo/yolov4.weights";

	//加载类别名
	vector<string> classes;
	ifstream ifs(classesFile.c_str());
	string line;
	while (getline(ifs, line)) classes.push_back(line);
	//加载网络模型,0是指定第一块GPU
	Detector detector(modelConfig, modelWeights, 0);

然后将读取的Mat图像转为yolo输入格式

shared_ptr<image_t> detImg = detector.mat_to_image_resize(frame);

然后检测

vector<bbox_t> outs = detector.detect_resized(*detImg, frame.cols, frame.rows, 0.25);

over了,是不是很简单。
实际上上面转为yolo输入格式和检测两步只需调用一个函数

vector<bbox_t> outs = detector.detect(frame, 0.25);

输出结果结构体bbox_t可以在yolo_v2_class.hpp看到,包括检测框左上角坐标,高和宽,置信度,类别等,有了它们画图也很简单了。

struct bbox_t {
    unsigned int x, y, w, h;       // (x,y) - top-left corner, (w, h) - width & height of bounded box
    float prob;                    // confidence - probability that the object was found correctly
    unsigned int obj_id;           // class of object - from range [0, classes-1]
    unsigned int track_id;         // tracking id for video (0 - untracked, 1 - inf - tracked object)
    unsigned int frames_counter;   // counter of frames on which the object was detected
    float x_3d, y_3d, z_3d;        // center of object (in Meters) if ZED 3D Camera is used
};

下面附上完整代码

#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <vector>
#include <fstream>
#include "yolo_v2_class.hpp"

using namespace std;
using namespace cv;
#define GPU

//画出检测框和相关信息
void DrawBoxes(Mat &frame, vector<string> classes, int classId, float conf, int left, int top, int right, int bottom)
{
	//画检测框
	rectangle(frame, Point(left, top), Point(right, bottom), Scalar(255, 178, 50), 3);
	//该检测框对应的类别和置信度
	string label = format("%.2f", conf);
	if (!classes.empty())
	{
		CV_Assert(classId < (int)classes.size());
		label = classes[classId] + ":" + label;
	}
	//将标签显示在检测框顶部
	int baseLine;
	Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
	top = max(top, labelSize.height);
	rectangle(frame, Point(left, top - round(1.5*labelSize.height)), Point(left + round(1.5*labelSize.width), top + baseLine), Scalar(255, 255, 255), FILLED);
	putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 0, 0), 1);
}


//画出检测结果
void Drawer(Mat &frame, vector<bbox_t> outs, vector<string> classes)
{
	//获取所有最佳检测框信息
	for (int i = 0; i < outs.size(); i++)
	{
		DrawBoxes(frame, classes, outs[i].obj_id, outs[i].prob, outs[i].x, outs[i].y,
			outs[i].x + outs[i].w, outs[i].y + outs[i].h);
	}
}


int main(void)
{
	string classesFile = "./yolo/coco.names";
	string modelConfig = "./yolo/yolov4.cfg";
	string modelWeights = "./yolo/yolov4.weights";

	//加载类别名
	vector<string> classes;
	ifstream ifs(classesFile.c_str());
	string line;
	while (getline(ifs, line)) classes.push_back(line);
	//加载网络模型,0是指定第一块GPU
	Detector detector(modelConfig, modelWeights, 0);

	string mode = "video";
	//图像
	if (mode == "image")
	{
		Mat frame = imread("./data/test.jpg");
		//Mat图像转为yolo输入格式
		shared_ptr<image_t> detImg = detector.mat_to_image_resize(frame);
		//前向预测
		vector<bbox_t> outs = detector.detect_resized(*detImg, frame.cols, frame.rows, 0.25);
		//画图
		Drawer(frame, outs, classes);
		imwrite("./data/result.jpg", frame);
	}
	//视频
	else if (mode == "video")
	{
		VideoCapture cap("./data/test.avi");
		Size size(1920, 1080);
		VideoWriter writer("./data/result.avi", cv::VideoWriter::fourcc('M', 'J', 'P', 'G'), 25, size);
		while (1) 
		{
			Mat frame;
			cap >> frame;
			if (frame.empty()) 
				break;

			//Mat图像转为yolo输入格式
			shared_ptr<image_t> detImg = detector.mat_to_image_resize(frame);
			//前向预测
			vector<bbox_t> outs = detector.detect_resized(*detImg, frame.cols, frame.rows, 0.25);
			//画图
			Drawer(frame, outs, classes);
			writer << frame;
		}
		cap.release();
	}
    return 0;
}

代码我放到了github上。

运行

1、需要用到刚刚编译的libdarknet.so,将它放到项目lib目录下或者/usr/lib目录下,另外需要用到yolov4源码下的yolo_v2_class.hpp文件。
2、将coco.names、yolov4.cfg、yolov4.weights三个文件放到项目yolo目录下,我放到了百度网盘中,提取码:2zfk。
3、在detection.cpp中修改自己检测图像或视频的路径,然后编译

make

检测结果

在这里插入图片描述
输入网络中的图像分辨率设为416*416,用titan v跑一帧在20ms左右。
如果有不对的地方望大佬指点。

  • 15
    点赞
  • 150
    收藏
    觉得还不错? 一键收藏
  • 71
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 71
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值