c# 通过emgucv调用gpu推理yolov7和yolov7-tiny,darknet版本

4 篇文章 0 订阅
1 篇文章 0 订阅

c# 通过emgucv4.5.5调用gpu推理yolov7和yolov7-tiny,darknet版本。注意不是pytorch。

GitHub - WongKinYiu/yolov7 at darknet

RTX2060显卡推理yolov7总耗时26msyolov7-tiny总耗时6ms。

全部demo代码下载,由于带了cuda,cudnn,文件很多。

https://download.csdn.net/download/vokxchh/86561885https://download.csdn.net/download/vokxchh/86561885部分推理代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Emgu.CV.Dnn;
using Emgu.CV;
using Emgu.CV.Util;
using System.Drawing;
using Emgu.CV.Structure;
using DarknetYolo.Models;
using System.IO;


namespace WindowsFormsApp11
{
    public class DarknetYOLO
    {
        /// <summary>
        /// Model to load
        /// </summary>
        public Net Network { get; set; }

        /// <summary>
        /// Prediction confidence threshold
        /// </summary>
        public float ConfidenceThreshold { get; set; }

        /// <summary>
        /// Non-Max Suppression Threshold
        /// </summary>
        public float NMSThreshold { get; set; }

        private string[] _labels;

        /// <summary>
        /// Initialize Darknet network.
        /// </summary>
        /// <param name="labelsPath">Path to the labels file.</param>
        /// <param name="weightsPath">Path to the weights file.</param>
        /// <param name="configPath">Path to the config file.</param>
        /// <param name="backend">Preferred computation implementation.</param>
        /// <param name="target">Preferred computation target.</param>
        public DarknetYOLO(string labelsPath, string weightsPath, string configPath, PreferredBackend backend = PreferredBackend.OpenCV, PreferredTarget target = PreferredTarget.Cuda)
        {
            Enum.TryParse(backend.ToString(), out Emgu.CV.Dnn.Backend b);
            Enum.TryParse(target.ToString(), out Emgu.CV.Dnn.Target t);
            Network = DnnInvoke.ReadNetFromDarknet(configPath, weightsPath);
            Network.SetPreferableBackend(b);
            Network.SetPreferableTarget(t);
            _labels = File.ReadAllLines(labelsPath);
        }

        /// <summary>
        /// Detect objects from image.
        /// </summary>
        /// <param name="inputImage">The network's input image.</param>
        /// <param name="resizedWidth">(Optional) Resize image width before feeding it to the network (smaller results in faster predictions but may hurt accuracy).</param>
        /// <param name="resizedHeight">(Optional) Resize image height before feeding it to the network (smaller results in faster predictions but may hurt accuracy)</param>
        /// <returns>List of all detected objects.</returns>
        public List<YoloPrediction> Predict(Image<Bgr, byte> inputImage, int resizedWidth = 416, int resizedHeight =416)
        {
            if (resizedWidth % 32 is int rest)
            {
                if (resizedWidth < 32)
                    resizedWidth = 32;
                if (rest < 16)
                    resizedWidth = (int)(32 * Math.Floor(resizedWidth / 32f));
                else
                    resizedWidth = (int)(32 * Math.Ceiling(resizedWidth / 32f));
            }

            if (resizedHeight % 32 is int rest2)
            {
                if (resizedHeight < 32)
                    resizedHeight = 32;
                if (rest2 < 16)
                    resizedHeight = (int)(32 * Math.Floor(resizedHeight / 32f));
                else
                    resizedHeight = (int)(32 * Math.Ceiling(resizedHeight / 32f));
            }

          //  Mat t = new Mat();
            int width = inputImage.Width;
            int height = inputImage.Height;
            VectorOfMat layerOutputs = new VectorOfMat();
            string[] outNames = Network.UnconnectedOutLayersNames;
            var blob = DnnInvoke.BlobFromImage(inputImage, 1 / 255f, new System.Drawing.Size(resizedWidth, resizedHeight), swapRB: true, crop: false);
            Network.SetInput(blob);
            Network.Forward(layerOutputs, outNames);

            List<Rectangle> boxes = new List<Rectangle>();
            List<float> confidences = new List<float>();
            List<int> classIDs = new List<int>();
            for (int k = 0; k < layerOutputs.Size; k++)
            {
                float[,] lo = (float[,])layerOutputs[k].GetData();
                int len = lo.GetLength(0);
                for (int i = 0; i < len; i++)
                {
                    if (lo[i, 4] < ConfidenceThreshold)
                        continue;
                    float max = 0;
                    int idx = 0;

                    int len2 = lo.GetLength(1);
                    for (int j = 5; j < len2; j++)
                        if (lo[i, j] > max)
                        {
                            max = lo[i, j];
                            idx = j - 5;
                        }

                    if (max > ConfidenceThreshold)
                    {
                        lo[i, 0] *= width;
                        lo[i, 1] *= height;
                        lo[i, 2] *= width;
                        lo[i, 3] *= height;

                        int x = (int)(lo[i, 0] - (lo[i, 2] / 2));
                        int y = (int)(lo[i, 1] - (lo[i, 3] / 2));

                        var rect = new Rectangle(x, y, (int)lo[i, 2], (int)lo[i, 3]);

                        rect.X = rect.X < 0 ? 0 : rect.X;
                        rect.X = rect.X > width ? width - 1 : rect.X;
                        rect.Y = rect.Y < 0 ? 0 : rect.Y;
                        rect.Y = rect.Y > height ? height - 1 : rect.Y;
                        rect.Width = rect.X + rect.Width > width ? width - rect.X - 1 : rect.Width;
                        rect.Height = rect.Y + rect.Height > height ? height - rect.Y - 1 : rect.Height;

                        boxes.Add(rect);
                        confidences.Add(max);
                        classIDs.Add(idx);
                    }
                }
            }
            int[] bIndexes = DnnInvoke.NMSBoxes(boxes.ToArray(), confidences.ToArray(), ConfidenceThreshold, NMSThreshold);

            List<YoloPrediction> filteredBoxes = new List<YoloPrediction>();
            if (bIndexes.Length > 0)
            {
                foreach (var idx in bIndexes)
                {
                    filteredBoxes.Add(new YoloPrediction()
                    {
                        Rectangle = boxes[idx],
                        Confidence = Math.Round(confidences[idx], 4),
                        Label = _labels[classIDs[idx]]
                    });
                }
            }
            return filteredBoxes;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DarknetYolo.Models
{
    public enum PreferredBackend
    {
        /// <summary>
        /// Default equals to InferenceEngine if OpenCV is built with Intel's Inference Engine library or Opencv otherwise.
        /// </summary> 
        Default = 0,
        /// <summary>
        /// Halide backend
        /// </summary>
        Halide = 1,
        /// <summary>
        /// Intel's Inference Engine library
        /// </summary>    Intel's Inference Engine library
        InferenceEngine = 2,
        /// <summary>
        /// OpenCV's implementation
        /// </summary>
        OpenCV = 3,
        /// <summary>
        /// Vulkan based backend
        /// </summary>
        VkCom = 4,
        /// <summary>
        /// Cuda backend
        /// </summary>
        Cuda = 5,
        /// <summary>
        /// Inference Engine NGraph
        /// </summary>
        InferenceEngineNgraph = 1000000,
        /// <summary>
        /// Inference Engine NN Builder 2019
        /// </summary>
        InferenceEngineNnBuilder2019 = 1000001
    }
}
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
using DarknetYolo.Models;
//using DarknetYOLO.Bitmap0;
using Emgu.CV;
using Emgu.CV.Structure;
using static System.Windows.Forms.DataFormats;

namespace WindowsFormsApp11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DarknetYOLO model;
        ConcurrentQueue<Pic> listqueue = new ConcurrentQueue<Pic>();// 
       
        System.Timers.Timer ti2 = new System.Timers.Timer(10);//
        Stopwatch _stopWath = new Stopwatch();
        int cout = 0;
        class Pic
        {
            private string name;
      
            private Image<Bgr, byte> data;
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
           
            public Image<Bgr, byte> Data
            {
                get { return data; }
                set { data = value; }
            }

        }
        private void button1_Click(object sender, EventArgs e)
        {
            cout = 0;
            icout = 0;
           
            DirectoryInfo dir = new DirectoryInfo(Environment.CurrentDirectory + "\\picture\\");
            FileInfo[] fileInfo = dir.GetFiles("*.jpg");
            cout = fileInfo.Count();
            _stopWath = new Stopwatch();
            ti2.Enabled = true;
            richTextBox1.Clear();

            sb.Clear();

            _stopWath.Start();
            //Parallel.ForEach(fileInfo, i =>
            foreach (var i in fileInfo)
            {
                string imagePath = Environment.CurrentDirectory + "\\picture\\" + i.Name;
                //Image<Bgr, byte> frame = new Image<Bgr, byte>(imagePath);//low
                var frame = new Bitmap(imagePath).ToImage<Bgr, byte>();//5ms
                                                                      
                listqueue.Enqueue(new Pic { Name = i.Name, Data = frame}  );

            }
            //);
        }



           
     
       
        private void Form1_Load(object sender, EventArgs e)
        {
         
            Form2 fm = new Form2();
            fm.Show();
            ti2.Elapsed += new System.Timers.ElapsedEventHandler(this.ti2_Elapsed);//到达时间的时候执行事件;
            string labels = @"vocTR.names";
            // string weights = @"yolov7_last.weights";
            // string cfg = @"yolov7.cfg";
             string weights = @"yolov7-tiny_last.weights";
             string cfg = @"yolov7-tiny.cfg";

           
            model = new DarknetYOLO(labels, weights, cfg, PreferredBackend.Cuda, PreferredTarget.Cuda);
            model.NMSThreshold = 0.4f;
            model.ConfidenceThreshold = 0.5f;
            var frame = new Bitmap("demo.jpg").ToImage<Bgr, byte>();
            List<YoloPrediction> results = model.Predict(frame, 416, 416);//预热
            fm.Close();
           
        }
        StringBuilder sb = new StringBuilder();
        int icout = 0;
        private void ti2_Elapsed(object sender, ElapsedEventArgs e)//1# 
        {
            ti2.Enabled = false;
            while (icout<cout)
            {
                if (!listqueue.IsEmpty)
                {

                    if (listqueue.TryDequeue(out Pic result0))
                    {
                        List<YoloPrediction> results = model.Predict(result0.Data, 416, 416);//
                        foreach (var item in results)
                        {
                            sb.AppendLine(result0.Name+" " + item.Label + " " + item.Confidence);
                        }
                        icout++;
                    }
                    if (icout == cout)
                    {
                        _stopWath.Stop();
                        string aa = _stopWath.ElapsedMilliseconds.ToString();
                        richTextBox1.BeginInvoke(new MethodInvoker(delegate ()
                        {
                            richTextBox1.AppendText(sb.ToString() + "\r\n" + "Total: " + aa + " ms " + " Count:" + icout+" each time:"+(( _stopWath.ElapsedMilliseconds / (float)icout)).ToString("0.00")+"ms");
                        }));
                        ti2.Enabled = false;
                        return;
                    }
                }
            }
            ti2.Enabled = true;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DarknetYolo.Models
{
    public enum PreferredTarget
    {
        /// <summary>
        /// CPU
        /// </summary>
        Cpu = 0,
        /// <summary>
        /// OpenCL
        /// </summary>
        OpenCL = 1,
        /// <summary>
        /// Will fall back to OPENCL if the hardware does not support FP16
        /// </summary>
        OpenCLFp16 = 2,
        /// <summary>
        /// Myriad
        /// </summary>
        Myriad = 3,
        /// <summary>
        /// Vulkan
        /// </summary>
        Vulkan = 4,
        /// <summary>
        /// FPGA device with CPU fallbacks using Inference Engine's Heterogeneous plugin.
        /// </summary>
        FPGA = 5,
        /// <summary>
        /// Cuda
        /// </summary>
        Cuda = 6,
        /// <summary>
        /// Cuda Fp16
        /// </summary>
        CudaFp16 = 7
    }
}

C#运用emgucv调用gpu,推理yolov7和yolov7-tiny。darknet版本80分类原始weights和cfg-数据集文档类资源-CSDN文库https://download.csdn.net/download/vokxchh/86590551原始80分类demo 代码下载

      更新emgucv4.6.0 

c#emgucv4.6.0版本gpu推理yolov7-tiny,darknet版本-C#文档类资源-CSDN文库icon-default.png?t=M85Bhttps://download.csdn.net/download/vokxchh/86854081

 

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值