【测试通过环境】
win10 x64
vs2019
cuda11.7+cudnn8.8.0
TensorRT-8.6.1.6
opencvsharp==4.9.0
.NET Framework4.7.2
NVIDIA GeForce RTX 2070 Super
版本和上述环境版本不一样的需要重新编译TensorRtExtern.dll,TensorRtExtern源码地址:TensorRT-CSharp-API/src/TensorRtExtern at TensorRtSharp2.0 · guojin-yan/TensorRT-CSharp-API · GitHub
Windows版 CUDA安装参考:Windows版 CUDA安装_win cuda安装-CSDN博客
【特别注意】
tensorrt依赖不同硬件需要自己从onnx转换tensorrt,转换就是调用api实现,比如
TensorRtSharp.Custom.Nvinfer.OnnxToEngine(@"C:\Users\Administrator\Desktop\yolov8n.onnx",1024);
【视频演示和解说】
【部分实现源码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FIRC;
using OpenCvSharp;
using TrtCommon;
using TensorRtSharp;
using TensorRtSharp.Custom;
using System.Diagnostics;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Yolov8Det yolov8Det = new Yolov8Det("yolov8n.engine");
Mat image1 = Cv2.ImRead(@"E:\person.jpg");
List<DetResult> detResults = yolov8Det.Predict(new List<Mat> { image1 });
Mat re_image1 = Visualize.DrawDetResult(detResults[0], image1);
Cv2.ImShow("image1", re_image1);
Cv2.WaitKey(0);
}
private void button2_Click(object sender, EventArgs e)
{
TensorRtSharp.Custom.Nvinfer.OnnxToEngine(@"C:\Users\Administrator\Desktop\yolov8n.onnx",1024);
}
private void button3_Click(object sender, EventArgs e)
{
Yolov8Det detector = new Yolov8Det("yolov8n.engine");
VideoCapture capture = new VideoCapture(0);
if (!capture.IsOpened())
{
Console.WriteLine("video not open!");
return;
}
Mat frame = new Mat();
var sw = new Stopwatch();
int fps = 0;
while (true)
{
capture.Read(frame);
if (frame.Empty())
{
Console.WriteLine("data is empty!");
break;
}
sw.Start();
List<DetResult> detResults = detector.Predict(new List<Mat> { frame });
Mat resultImg = Visualize.DrawDetResult(detResults[0], frame);
sw.Stop();
fps = Convert.ToInt32(1 / sw.Elapsed.TotalSeconds);
sw.Reset();
Cv2.PutText(resultImg, "FPS=" + fps, new OpenCvSharp.Point(30, 30), HersheyFonts.HersheyComplex, 1.0, new Scalar(255, 0, 0), 3);
//显示结果
Cv2.ImShow("Result", resultImg);
int key = Cv2.WaitKey(10);
if (key == 27)
break;
}
capture.Release();
}
}
}
【演示源码下载地址】https://download.csdn.net/download/FL1623863129/89372271
注意源码提供上面对应环境的dll,只需要安装上面一样cuda+cudnn和tensorrt版本即可正常运行。如果您不安装一样版本不能正常运行。此时需要重新编译TensorRtExtern.dll,此外由于tensorrt依赖硬件不一样电脑可能无法共用tensorrt模型,所以必须要重新转换onnx模型到engine才可以运行。