C#使用Tensorrt推理Yolov8-Obb和Yolov8-Det

Export-Onnx-Obb

from ultralytics import YOLO
model = YOLO("yolov8n-obb.yaml")
model = YOLO("yolov8n-obb.pt")
trainer = model.train(data="dota8.yaml", epochs=3, imgsz=640)
metrics = model.val(data="dota8.yaml")
results = model("boats.jpg")
model.export(format="onnx")

Csharp

using OpenCvSharp;
using OpenCvSharp.Dnn;
using TensorRtSharp;
using TensorRtSharp.Custom;
using System.Runtime.InteropServices;
namespace Yolov8obb
{
    class Program
    {
        public static void Main(string[] args)
        {
            float conf_threshold = 0.25f;
            float nms_threshold = 0.5f;
            string engine_path = "yolov8n-obb.engine";
            string image_path = "boats.jpg";
            string[] classes_names = ["plane","ship", "storage tank", "baseball diamond", "tennis court",
            "basketball court", "ground track field", "harbor",  "bridge",  "large vehicle",  "small vehicle",
            "helicopter",  "roundabout", "soccer ball field", "swimming pool" ];
            if (Path.GetExtension(engine_path) == ".onnx")
            {
                Nvinfer.OnnxToEngine(engine_path, 20);
            }
            string path = Path.Combine(Path.GetDirectoryName(engine_path), Path.GetFileNameWithoutExtension(engine_path) + ".engine");
            Nvinfer predictor = new Nvinfer(path);
            Dims InputDims = predictor.GetBindingDimensions("images");
            Mat image = Cv2.ImRead(image_path);
            Mat masked_img = new Mat();
            int max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;
            Mat max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);
            Rect roi = new Rect(0, 0, image.Cols, image.Rows);
            image.CopyTo(new Mat(max_image, roi));
            float[] factors = new float[4];
            factors[0] = factors[1] = (float)(max_image_length / 1024.0);
            factors[2] = image.Rows;
            factors[3] = image.Cols;
            Mat image_rgb = new Mat();
            Mat resize_image = new Mat();
            Cv2.CvtColor(max_image, image_rgb, ColorConversionCodes.BGR2RGB);
            Cv2.Resize(image_rgb, resize_image, new OpenCvSharp.Size(1024, 1024));
            resize_image.ConvertTo(resize_image, MatType.CV_32FC3, 1.0 / 255.0);
            float[] input_data = new float[resize_image.Rows * resize_image.Cols * resize_image.Channels()];
            GCHandle resultHandle = default;
            try
            {
                resultHandle = GCHandle.Alloc(input_data, GCHandleType.Pinned);
                IntPtr resultPtr = resultHandle.AddrOfPinnedObject();
                for (int i = 0; i < resize_image.Channels(); ++i)
                {
                    using Mat dest = Mat.FromPixelData(resize_image.Rows, resize_image.Cols, MatType.CV_32FC1, resultPtr + i * resize_image.Rows * resize_image.Cols * sizeof(float));
                    Cv2.ExtractChannel(resize_image, dest, i);
                }
            }
            finally
            {
                resultHandle.Free();
            }
            predictor.LoadInferenceData("images", input_data);
            predictor.infer();
            Dims dims = predictor.GetBindingDimensions("output0");
            float[] outputData = predictor.GetInferenceResult("output0");
            Mat resultData = Mat.FromPixelData(5 + 15, 21504, MatType.CV_32F, outputData);
            resultData = resultData.T();
            List<Rect2d> positionBoxes = new List<Rect2d>();
            List<int> classIds = new List<int>();
            List<float> confidences = new List<float>();
            List<float> rotations = new List<float>();
            for (int i = 0; i < resultData.Rows; i++)
            {
                Mat classesScores = new Mat(resultData, new Rect(4, i, 15, 1));
                OpenCvSharp.Point max_classId_point, min_classId_point;
                double maxScore, minScore;
                Cv2.MinMaxLoc(classesScores, out minScore, out maxScore, out min_classId_point, out max_classId_point);
                if (maxScore > 0.25)
                {
                    float cx = resultData.At<float>(i, 0);
                    float cy = resultData.At<float>(i, 1);
                    float ow = resultData.At<float>(i, 2);
                    float oh = resultData.At<float>(i, 3);
                    double x = (cx - 0.5 * ow) * factors[0];
                    double y = (cy - 0.5 * oh) * factors[1];
                    double width = ow * factors[0];
                    double height = oh * factors[1];
                    Rect2d box = new Rect2d();
                    box.X = x;
                    box.Y = y;
                    box.Width = width;
                    box.Height = height;
                    positionBoxes.Add(box);
                    classIds.Add(max_classId_point.X);
                    confidences.Add((float)maxScore);
                    rotations.Add(resultData.At<float>(i, 19));
                }
            }
            int[] indexes = new int[positionBoxes.Count];
            CvDnn.NMSBoxes(positionBoxes, confidences, conf_threshold, nms_threshold, out indexes);
            for (int i = 0; i < indexes.Length; i++)
            {
                int index = indexes[i];
                float w = (float)positionBoxes[index].Width;
                float h = (float)positionBoxes[index].Height;
                float x = (float)positionBoxes[index].X + w / 2;
                float y = (float)positionBoxes[index].Y + h / 2;
                float r = rotations[index];
                float w_ = w > h ? w : h;
                float h_ = w > h ? h : w;
                r = (float)((w > h ? r : (float)(r + Math.PI / 2)) % Math.PI);
                RotatedRect rotate = new RotatedRect(new Point2f(x, y), new Size2f(w_, h_), (float)(r * 180.0 / Math.PI));
                Point2f[] points = rotate.Points();
                for (int j = 0; j < 4; j++)
                {
                    Cv2.Line(image, (Point)points[j], (Point)points[(j + 1) % 4], new Scalar(255, 100, 200), 2);
                }
                Cv2.PutText(image, classes_names[classIds[index]] + "-" + confidences[index].ToString("0.00"),
                    (Point)points[0], HersheyFonts.HersheySimplex, 0.8, new Scalar(0, 0, 0), 2);
            }
            Cv2.ImShow("result", image);
            Cv2.WaitKey(0);
        }    
    }
}     

Export-Onnx-Det

from ultralytics import YOLO
model = YOLO("yolov8n.yaml")
model = YOLO("yolov8n.pt")
trainer = model.train(data="coco8.yaml", epochs=4, batch=2, imgsz=640)
metrics = model.val(data="coco8.yaml", imgsz=640, batch=2, conf=0.25, iou=0.6)
results = model("boats.jpg")
model.export(format="onnx")

Csharp

using OpenCvSharp;
using OpenCvSharp.Dnn;
using TensorRtSharp;
using TensorRtSharp.Custom;
using System.Runtime.InteropServices;
namespace Yolov8obb
{
    class Program
    {
        public static string[] read_class_names(string path)
        {
            string[] class_names;
            List<string> str = new List<string>();
            StreamReader sr = new StreamReader(path);
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                str.Add(line);
            }
            class_names = str.ToArray();
            return class_names;
        }
        public static void Main(string[] args)
        {
            float conf_threshold = 0.25f;
            float nms_threshold = 0.5f;
            string engine_path = "yolov8n.engine";
            string image_path = "bus.jpg";
            string[] classes_names = read_class_names("coco.names");
            List<Mat> masks = new List<Mat>();
            List<Rect> position_boxes = new List<Rect>();
            List<int> class_ids = new List<int>();
            List<float> class_scores = new List<float>();
            List<float> confidences = new List<float>();
            if (Path.GetExtension(engine_path) == ".onnx")
            {
                Nvinfer.OnnxToEngine(engine_path, 20);
            }
            string path = Path.Combine(Path.GetDirectoryName(engine_path), Path.GetFileNameWithoutExtension(engine_path) + ".engine");
            Nvinfer predictor = new Nvinfer(path);
            Dims InputDims = predictor.GetBindingDimensions("images");
            Mat image = Cv2.ImRead(image_path);
            Mat masked_img = new Mat();
            int max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;
            Mat max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);
            Rect roi = new Rect(0, 0, image.Cols, image.Rows);
            image.CopyTo(new Mat(max_image, roi));
            float[] factors = new float[4];
            factors[0] = factors[1] = (float)(max_image_length / 640.0);
            factors[2] = image.Rows;
            factors[3] = image.Cols;
            Mat image_rgb = new Mat();
            Mat resize_image = new Mat();
            Cv2.CvtColor(max_image, image_rgb, ColorConversionCodes.BGR2RGB);
            Cv2.Resize(image_rgb, resize_image, new OpenCvSharp.Size(640, 640));
            resize_image.ConvertTo(resize_image, MatType.CV_32FC3, 1.0 / 255.0);
            float[] input_data = new float[resize_image.Rows * resize_image.Cols * resize_image.Channels()];
            double e = 1.0;
            GCHandle resultHandle = default;
            try
            {
                resultHandle = GCHandle.Alloc(input_data, GCHandleType.Pinned);
                IntPtr resultPtr = resultHandle.AddrOfPinnedObject();
                for (int i = 0; i < resize_image.Channels(); ++i)
                {
                    using Mat dest = Mat.FromPixelData(resize_image.Rows, resize_image.Cols, MatType.CV_32FC1, resultPtr + i * resize_image.Rows * resize_image.Cols * sizeof(float));
                    Cv2.ExtractChannel(resize_image, dest, i);
                }
            }
            finally
            {
                resultHandle.Free();
            }
            predictor.LoadInferenceData("images", input_data);
            predictor.infer();
            Dims d = predictor.GetBindingDimensions("output0");
            float[] detect = predictor.GetInferenceResult("output0");
            Mat detect_data = Mat.FromPixelData(84, 8400, MatType.CV_32FC1, detect);
            detect_data = detect_data.T();
            for (int i = 0; i < detect_data.Rows; i++)
            {
                Mat classes_scores = detect_data.Row(i).ColRange(4, 84);
                OpenCvSharp.Point max_classId_point, min_classId_point;
                double max_score, min_score;
                Cv2.MinMaxLoc(classes_scores, out min_score, out max_score,
                    out min_classId_point, out max_classId_point);
                if (max_score > 0.25)
                {
                    float cx = detect_data.At<float>(i, 0);
                    float cy = detect_data.At<float>(i, 1);
                    float ow = detect_data.At<float>(i, 2);
                    float oh = detect_data.At<float>(i, 3);
                    int x = (int)((cx - 0.5 * ow) * factors[0]);
                    int y = (int)((cy - 0.5 * oh) * factors[1]);
                    int width = (int)(ow * factors[0]);
                    int height = (int)(oh * factors[1]);
                    Rect box = new Rect();
                    box.X = x;
                    box.Y = y;
                    box.Width = width;
                    box.Height = height;
                    position_boxes.Add(box);
                    class_ids.Add(max_classId_point.X);
                    classes_scores.Add((Scalar)(max_score));
                    confidences.Add((float)max_score);
                }
            }
            int[] indexes = new int[position_boxes.Count];
            CvDnn.NMSBoxes(position_boxes, confidences, conf_threshold, nms_threshold, out indexes);
            Mat rgb_mask = Mat.Zeros(new OpenCvSharp.Size((int)factors[3], (int)factors[2]), MatType.CV_8UC3);
            Random rd = new Random();
            for (int i = 0; i < indexes.Length; i++)
            {
                int index = indexes[i];
                Rect box = position_boxes[index];
                int box_x1 = Math.Max(0, box.X);
                int box_y1 = Math.Max(0, box.Y);
                int box_x2 = Math.Max(0, box.BottomRight.X);
                int box_y2 = Math.Max(0, box.BottomRight.Y);
                Cv2.Rectangle(image, position_boxes[index], new Scalar(0, 0, 255), 2, LineTypes.Link8);
                Cv2.Rectangle(image, new OpenCvSharp.Point(position_boxes[index].TopLeft.X, position_boxes[index].TopLeft.Y - 20),
                    new OpenCvSharp.Point(position_boxes[index].BottomRight.X, position_boxes[index].TopLeft.Y), new Scalar(0, 255, 255), -1);
                Cv2.PutText(image, classes_names[class_ids[index]] + "-" + confidences[index].ToString("0.00"),
                    new OpenCvSharp.Point(position_boxes[index].X, position_boxes[index].Y - 10),
                    HersheyFonts.HersheySimplex, 0.6, new Scalar(0, 0, 0), 1);
            }
            Cv2.ImShow("Result", image);
            Cv2.WaitKey(5000);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值