C# OpenVino Yolov8 Seg 分割

2024-03-05 1428阅读

温馨提示:这篇文章已超过389天没有更新,请注意相关的内容是否还可用!

目录

效果

模型信息

项目

代码

下载


效果

C# OpenVino Yolov8 Seg 分割

模型信息

Model Properties

-------------------------

date:2023-09-07T17:11:46.798385

description:Ultralytics YOLOv8n-seg model trained on coco.yaml

author:Ultralytics

task:segment

license:AGPL-3.0 https://ultralytics.com/license

version:8.0.172

stride:32

batch:1

imgsz:[640, 640]

names:{0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus', 6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant', 11: 'stop sign', 12: 'parking meter', 13: 'bench', 14: 'bird', 15: 'cat', 16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear', 22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag', 27: 'tie', 28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard', 32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove', 36: 'skateboard', 37: 'surfboard', 38: 'tennis racket', 39: 'bottle', 40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl', 46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli', 51: 'carrot', 52: 'hot dog', 53: 'pizza', 54: 'donut', 55: 'cake', 56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining table', 61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard', 67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink', 72: 'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors', 77: 'teddy bear', 78: 'hair drier', 79: 'toothbrush'}

---------------------------------------------------------------

Inputs

-------------------------

name:images

tensor:Float[1, 3, 640, 640]

---------------------------------------------------------------

Outputs

-------------------------

name:output0

tensor:Float[1, 116, 8400]

name:output1

tensor:Float[1, 32, 160, 160]

---------------------------------------------------------------

项目

C# OpenVino Yolov8 Seg 分割

代码

using OpenCvSharp;
using Sdcb.OpenVINO;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace OpenVino_Yolov8_Seg
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";
        string model_path;
        string classer_path;
        Mat src;
        StringBuilder sb = new StringBuilder();
        SegmentationResult result_pro;
        Result result;
        CompiledModel cm;
        InferRequest ir;
        Shape inputShape;
        private void Form1_Load(object sender, EventArgs e)
        {
            model_path = "model\\yolov8n-seg.onnx";
            classer_path = "model\\lable.txt";
            Model rawModel = OVCore.Shared.ReadModel(model_path);
            var ad = OVCore.Shared.AvailableDevices;
            Console.WriteLine("可用设备");
            foreach (var item in ad)
            {
                Console.WriteLine(item);
            }
            cm = OVCore.Shared.CompileModel(rawModel, "CPU");
            ir = cm.CreateInferRequest();
            inputShape = cm.Inputs.Primary.Shape;
            image_path = "test_img\\demo_2.jpg";
            pictureBox1.Image = new Bitmap(image_path);
            textBox1.Text = "";
            src = new Mat(image_path);
            pictureBox2.Image = null;
            result_pro = new SegmentationResult(classer_path, null);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            pictureBox1.Image = null;
            image_path = ofd.FileName;
            pictureBox1.Image = new Bitmap(image_path);
            textBox1.Text = "";
            src = new Mat(image_path);
            pictureBox2.Image = null;
        }
        unsafe private void button2_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null)
            {
                return;
            }
            pictureBox2.Image = null;
            textBox1.Text = "";
            sb.Clear();
            button2.Enabled = false;
            Application.DoEvents();
            Stopwatch stopwatch = new Stopwatch();
            //图片缩放
            Mat image = new Mat(image_path);
            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));
            Mat image_rgb = new Mat();
            Cv2.CvtColor(max_image, image_rgb, ColorConversionCodes.BGR2RGB);
            Mat resize_image = new Mat();
            Cv2.Resize(image_rgb, resize_image, new OpenCvSharp.Size(640, 640));
            float[] factors = new float[4];
            factors[0] = factors[1] = (float)(max_image_length / 640.0);
            factors[2] = src.Rows;
            factors[3] = src.Cols;
            result_pro.scales = factors;
            resize_image.ConvertTo(resize_image, MatType.CV_32FC3, 1.0 / 255);
            float[] input_tensor_data = Common.ExtractMat(resize_image);
            resize_image.Dispose();
            image_rgb.Dispose();
            using (Tensor input_x = Tensor.FromArray(input_tensor_data, new Shape(1, 3, 640, 640)))
            {
                ir.Inputs[0] = input_x;
            }
            double preprocessTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Restart();
            ir.Run();
            double inferTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Restart();
            using (Tensor output_det = ir.Outputs[0])
            using (Tensor output_proto = ir.Outputs[1])
            {
                float[] det_result_array = output_det.GetData().ToArray();
                float[] proto_result_array = output_proto.GetData().ToArray();
                result = result_pro.process_result(det_result_array, proto_result_array);
               
                double postprocessTime = stopwatch.Elapsed.TotalMilliseconds;
                stopwatch.Stop();
                double totalTime = preprocessTime + inferTime + postprocessTime;
                Mat result_image = result_pro.draw_result(result, image.Clone());
                sb.AppendLine($"Preprocess: {preprocessTime:F2}ms");
                sb.AppendLine($"Infer: {inferTime:F2}ms");
                sb.AppendLine($"Postprocess: {postprocessTime:F2}ms");
                sb.AppendLine($"Total: {totalTime:F2}ms");
                sb.AppendLine("---------------------------------------");
                for (int i = 0; i  

下载

源码下载

可执行程序exe下载

想尝试自行编译OpenVinoSharp的,博客地址:https://blog.csdn.net/lw112190/article/details/132753726

VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]