参考博客:
1. yolov8的TensorRT部署(C++版本):https://blog.youkuaiyun.com/liujiahao123987/article/details/133892746( 代码是在该博客代码基础上改的,将识别一张图片改成了识别一个视频 )
2. 关于visual studio和vc版本之间的对应关系(更新至2020.07):https://blog.youkuaiyun.com/tanmx219/article/details/100824775/
1. 代码结构:
src
include
common.hpp
objectCheck.hpp
objectCheck.cpp
test.cpp
build
test.exe
build.bat
2. 完整代码:
2.1. common.hpp:
#ifndef DEF_COMMON_HEAD
#define DEF_COMMON_HEAD
#include <iostream>
#include <vector>
#include <list>
#include<iostream>
#include<opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>
#include<fstream>
#include "NvInfer.h"
//#include "logging.h"
#include <string>
#include "opencv2/highgui.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/imgcodecs/legacy/constants_c.h"
#include "opencv2/imgproc/types_c.h"
#include "opencv2/videoio.hpp"
// using namespace std;
// using namespace nvinfer1;
#endif
2.2. objectCheck.hpp:
#ifndef DEF_OBJECT_CHECK_HEAD
#define DEF_OBJECT_CHECK_HEAD
#include "./common.hpp"
using namespace std;
struct BOX
{
float x;
float y;
float width;
float height;
};
struct Object
{
BOX box; // lu点和wh
int label;
float confidence; //这里的confidence实际指的是score 即 objectness*confidence
};
float iou_of(const Object &obj1, const Object &obj2);
std::vector<int> hardNMS(std::vector<Object> &input, std::vector<Object> &output, float iou_threshold, unsigned int topk);
// 对图片进行 物体检测的函数的声明
int doObjectCheck(cv::Mat input_image);
// 加载 trt 模型的函数声明
void loadTrtModel( string trtModelPath );
#endif
2.3. objectCheck.cpp:
#include "./include/objectCheck.hpp"
const int model_width = 640;
const int model_height = 640;
//以coco数据集为例
string names[] = {"person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light",
"fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow",
"elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
"'skis'", "'snowboard'", "'sports ball'", "'kite'", "'baseball bat'", "'baseball glove'", "'skateboard'", "'surfboard'",
"'tennis racket'", "'bottle'", "'wine glass'", "'cup'", "'fork'", "'knife'", "'spoon'", "'bowl'", "'banana'", "'apple'",
"'sandwich'", "'orange'", "'broccoli'", "'carrot'", "'hot dog'", "'pizza'", "'donut'