C# OpenCvSharp DNN 部署yoloX

目录

效果

模型信息

项目

代码

下载


C# OpenCvSharp DNN 部署yoloX

效果

模型信息

Inputs


name:images

tensor:Float1, 3, 640, 640


Outputs


name:output

tensor:Float1, 8400, 85


项目

代码

using OpenCvSharp;

using OpenCvSharp.Dnn;

using System;

using System.Collections.Generic;

using System.Drawing;

using System.IO;

using System.Linq;

using System.Windows.Forms;

namespace OpenCvSharp_DNN_Demo

{

public partial class frmMain : Form

{

public frmMain()

{

InitializeComponent();

}

string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";

string image_path = "";

DateTime dt1 = DateTime.Now;

DateTime dt2 = DateTime.Now;

float prob_threshold;

float nms_threshold;

float\[\] stride = new float3 { 8.0f, 16.0f, 32.0f };

int\[\] input_shape = new int\[\] { 640, 640 }; // height, width

float\[\] mean = new float3 { 0.485f, 0.456f, 0.406f };

float\[\] std = new float3 { 0.229f, 0.224f, 0.225f };

float scale = 1.0f;

string modelpath;

int inpHeight;

int inpWidth;

List<string> class_names;

int num_class;

Net opencv_net;

Mat BN_image;

Mat image;

Mat result_image;

public Mat Normalize(Mat src)

{

Cv2.CvtColor(src, src, ColorConversionCodes.BGR2RGB);

Mat\[\] bgr = src.Split();

for (int i = 0; i < bgr.Length; ++i)

{

bgri.ConvertTo(bgri, MatType.CV_32FC1, 1.0 / (255.0 * stdi), (0.0 - meani) / stdi);

}

Cv2.Merge(bgr, src);

foreach (Mat channel in bgr)

{

channel.Dispose();

}

return src;

}

private void button1_Click(object sender, EventArgs e)

{

OpenFileDialog ofd = new OpenFileDialog();

ofd.Filter = fileFilter;

if (ofd.ShowDialog() != DialogResult.OK) return;

pictureBox1.Image = null;

pictureBox2.Image = null;

textBox1.Text = "";

image_path = ofd.FileName;

pictureBox1.Image = new Bitmap(image_path);

image = new Mat(image_path);

}

private void Form1_Load(object sender, EventArgs e)

{

prob_threshold = 0.6f;

nms_threshold = 0.6f;

modelpath = "model/yolox_s.onnx";

inpHeight = 640;

inpWidth = 640;

opencv_net = CvDnn.ReadNetFromOnnx(modelpath);

class_names = new List<string>();

StreamReader sr = new StreamReader("model/coco.names");

string line;

while ((line = sr.ReadLine()) != null)

{

class_names.Add(line);

}

num_class = class_names.Count();

image_path = "test_img/dog.jpg";

pictureBox1.Image = new Bitmap(image_path);

}

Mat ResizeImage(Mat srcimg)

{

float r = (float)Math.Min(input_shape1 / (srcimg.Cols * 1.0), input_shape0 / (srcimg.Rows * 1.0));

scale = r;

int unpad_w = (int)(r * srcimg.Cols);

int unpad_h = (int)(r * srcimg.Rows);

Mat re = new Mat(unpad_h, unpad_w, MatType.CV_8UC3);

Cv2.Resize(srcimg, re, new OpenCvSharp.Size(unpad_w, unpad_h));

Mat outMat = new Mat(input_shape1, input_shape0, MatType.CV_8UC3, new Scalar(114, 114, 114));

re.CopyTo(new Mat(outMat, new Rect(0, 0, re.Cols, re.Rows)));

return outMat;

}

private unsafe void button2_Click(object sender, EventArgs e)

{

if (image_path == "")

{

return;

}

textBox1.Text = "检测中,请稍等......";

pictureBox2.Image = null;

Application.DoEvents();

image = new Mat(image_path);

Mat dstimg = ResizeImage(image);

dstimg = Normalize(dstimg);

BN_image = CvDnn.BlobFromImage(dstimg);

//配置图片输入数据

opencv_net.SetInput(BN_image);

//模型推理,读取推理结果

Mat\[\] outs = new Mat\[\] { new Mat() };

string\[\] outBlobNames = opencv_net.GetUnconnectedOutLayersNames().ToArray();

dt1 = DateTime.Now;

opencv_net.Forward(outs, outBlobNames);

dt2 = DateTime.Now;

int num_proposal = outs0.Size(1);

outs0 = outs0.Reshape(0, num_proposal);

float* pdata = (float*)outs0.Data;

int row_ind = 0;

int nout = num_class + 5;

List<Rect> boxes = new List<Rect>();

List<float> confidences = new List<float>();

List<int> classIds = new List<int>();

for (int n = 0; n < 3; n++)

{

int num_grid_x = (int)(inpWidth / striden);

int num_grid_y = (int)(inpHeight / striden);

for (int i = 0; i < num_grid_y; i++)

{

for (int j = 0; j < num_grid_x; j++)

{

float box_score = pdata4;

Mat scores = outs0.Row(row_ind).ColRange(5, outs0.Cols);

double minVal, max_class_socre;

OpenCvSharp.Point minLoc, classIdPoint;

// Get the value and location of the maximum score

Cv2.MinMaxLoc(scores, out minVal, out max_class_socre, out minLoc, out classIdPoint);

int class_idx = classIdPoint.X;

float cls_score = pdata5 + class_idx;

float box_prob = box_score * cls_score;

if (box_prob > prob_threshold)

{

float x_center = (pdata0 + j) * striden;

float y_center = (pdata1 + i) * striden;

float w = (float)(Math.Exp(pdata2) * striden);

float h = (float)(Math.Exp(pdata3) * striden);

float x0 = x_center - w * 0.5f;

float y0 = y_center - h * 0.5f;

classIds.Add(class_idx);

confidences.Add(box_prob);

boxes.Add(new Rect((int)x0, (int)y0, (int)w, (int)h));

}

pdata += nout;

row_ind++;

}

}

}

int\[\] indices;

CvDnn.NMSBoxes(boxes, confidences, prob_threshold, nms_threshold, out indices);

result_image = image.Clone();

for (int ii = 0; ii < indices.Length; ++ii)

{

int idx = indicesii;

Rect box = boxesidx;

// adjust offset to original unpadded

float x0 = box.X / scale; ;

float y0 = box.Y / scale; ;

float x1 = (box.X + box.Width) / scale;

float y1 = (box.Y + box.Height) / scale;

// clip

x0 = Math.Max(Math.Min(x0, (float)(image.Cols - 1)), 0.0f);

y0 = Math.Max(Math.Min(y0, (float)(image.Rows - 1)), 0.0f);

x1 = Math.Max(Math.Min(x1, (float)(image.Cols - 1)), 0.0f);

y1 = Math.Max(Math.Min(y1, (float)(image.Rows - 1)), 0.0f);

Cv2.Rectangle(result_image, new OpenCvSharp.Point(x0, y0), new OpenCvSharp.Point(x1, y1), new Scalar(0, 255, 0), 2);

string label = class_namesclassIds\[idx] + ":" + confidencesidx.ToString("0.00");

Cv2.PutText(result_image, label, new OpenCvSharp.Point(x0, y0 - 5), HersheyFonts.HersheySimplex, 1, new Scalar(0, 0, 255), 2);

}

pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());

textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";

}

private void pictureBox2_DoubleClick(object sender, EventArgs e)

{

Common.ShowNormalImg(pictureBox2.Image);

}

private void pictureBox1_DoubleClick(object sender, EventArgs e)

{

Common.ShowNormalImg(pictureBox1.Image);

}

}

}

复制代码
using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace OpenCvSharp_DNN_Demo
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";

        DateTime dt1 = DateTime.Now;
        DateTime dt2 = DateTime.Now;

        float prob_threshold;
        float nms_threshold;

        float[] stride = new float[3] { 8.0f, 16.0f, 32.0f };

        int[] input_shape = new int[] { 640, 640 };   // height, width

        float[] mean = new float[3] { 0.485f, 0.456f, 0.406f };
        float[] std = new float[3] { 0.229f, 0.224f, 0.225f };
        float scale = 1.0f;

        string modelpath;

        int inpHeight;
        int inpWidth;

        List<string> class_names;
        int num_class;

        Net opencv_net;
        Mat BN_image;

        Mat image;
        Mat result_image;

        public Mat Normalize(Mat src)
        {
            Cv2.CvtColor(src, src, ColorConversionCodes.BGR2RGB);
            Mat[] bgr = src.Split();
            for (int i = 0; i < bgr.Length; ++i)
            {
                bgr[i].ConvertTo(bgr[i], MatType.CV_32FC1, 1.0 / (255.0 * std[i]), (0.0 - mean[i]) / std[i]);
            }
            Cv2.Merge(bgr, src);
            foreach (Mat channel in bgr)
            {
                channel.Dispose();
            }
            return src;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;

            pictureBox1.Image = null;
            pictureBox2.Image = null;
            textBox1.Text = "";

            image_path = ofd.FileName;
            pictureBox1.Image = new Bitmap(image_path);
            image = new Mat(image_path);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            prob_threshold = 0.6f;
            nms_threshold = 0.6f;

            modelpath = "model/yolox_s.onnx";

            inpHeight = 640;
            inpWidth = 640;

            opencv_net = CvDnn.ReadNetFromOnnx(modelpath);

            class_names = new List<string>();
            StreamReader sr = new StreamReader("model/coco.names");
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                class_names.Add(line);
            }
            num_class = class_names.Count();

            image_path = "test_img/dog.jpg";
            pictureBox1.Image = new Bitmap(image_path);

        }

        Mat ResizeImage(Mat srcimg)
        {

            float r = (float)Math.Min(input_shape[1] / (srcimg.Cols * 1.0), input_shape[0] / (srcimg.Rows * 1.0));
            scale = r;
            int unpad_w = (int)(r * srcimg.Cols);
            int unpad_h = (int)(r * srcimg.Rows);
            Mat re = new Mat(unpad_h, unpad_w, MatType.CV_8UC3);
            Cv2.Resize(srcimg, re, new OpenCvSharp.Size(unpad_w, unpad_h));
            Mat outMat = new Mat(input_shape[1], input_shape[0], MatType.CV_8UC3, new Scalar(114, 114, 114));
            re.CopyTo(new Mat(outMat, new Rect(0, 0, re.Cols, re.Rows)));
            return outMat;
        }


        private unsafe void button2_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }
            textBox1.Text = "检测中,请稍等......";
            pictureBox2.Image = null;
            Application.DoEvents();

            image = new Mat(image_path);

            Mat dstimg = ResizeImage(image);

            dstimg = Normalize(dstimg);

            BN_image = CvDnn.BlobFromImage(dstimg);

            //配置图片输入数据
            opencv_net.SetInput(BN_image);

            //模型推理,读取推理结果
            Mat[] outs = new Mat[] { new Mat() };
            string[] outBlobNames = opencv_net.GetUnconnectedOutLayersNames().ToArray();

            dt1 = DateTime.Now;

            opencv_net.Forward(outs, outBlobNames);

            dt2 = DateTime.Now;

            int num_proposal = outs[0].Size(1);
            outs[0] = outs[0].Reshape(0, num_proposal);

            float* pdata = (float*)outs[0].Data;

            int row_ind = 0;
            int nout = num_class + 5;

            List<Rect> boxes = new List<Rect>();
            List<float> confidences = new List<float>();
            List<int> classIds = new List<int>();

            for (int n = 0; n < 3; n++)
            {
                int num_grid_x = (int)(inpWidth / stride[n]);
                int num_grid_y = (int)(inpHeight / stride[n]);

                for (int i = 0; i < num_grid_y; i++)
                {
                    for (int j = 0; j < num_grid_x; j++)
                    {
                        float box_score = pdata[4];
                        Mat scores = outs[0].Row(row_ind).ColRange(5, outs[0].Cols);

                        double minVal, max_class_socre;
                        OpenCvSharp.Point minLoc, classIdPoint;
                        // Get the value and location of the maximum score
                        Cv2.MinMaxLoc(scores, out minVal, out max_class_socre, out minLoc, out classIdPoint);

                        int class_idx = classIdPoint.X;

                        float cls_score = pdata[5 + class_idx];
                        float box_prob = box_score * cls_score;
                        if (box_prob > prob_threshold)
                        {
                            float x_center = (pdata[0] + j) * stride[n];
                            float y_center = (pdata[1] + i) * stride[n];
                            float w = (float)(Math.Exp(pdata[2]) * stride[n]);
                            float h = (float)(Math.Exp(pdata[3]) * stride[n]);
                            float x0 = x_center - w * 0.5f;
                            float y0 = y_center - h * 0.5f;

                            classIds.Add(class_idx);
                            confidences.Add(box_prob);
                            boxes.Add(new Rect((int)x0, (int)y0, (int)w, (int)h));
                        }

                        pdata += nout;
                        row_ind++;
                    }
                }
            }

            int[] indices;
            CvDnn.NMSBoxes(boxes, confidences, prob_threshold, nms_threshold, out indices);

            result_image = image.Clone();

            for (int ii = 0; ii < indices.Length; ++ii)
            {
                int idx = indices[ii];
                Rect box = boxes[idx];

                // adjust offset to original unpadded
                float x0 = box.X / scale; ;
                float y0 = box.Y / scale; ;
                float x1 = (box.X + box.Width) / scale;
                float y1 = (box.Y + box.Height) / scale;

                // clip
                x0 = Math.Max(Math.Min(x0, (float)(image.Cols - 1)), 0.0f);
                y0 = Math.Max(Math.Min(y0, (float)(image.Rows - 1)), 0.0f);
                x1 = Math.Max(Math.Min(x1, (float)(image.Cols - 1)), 0.0f);
                y1 = Math.Max(Math.Min(y1, (float)(image.Rows - 1)), 0.0f);

                Cv2.Rectangle(result_image, new OpenCvSharp.Point(x0, y0), new OpenCvSharp.Point(x1, y1), new Scalar(0, 255, 0), 2);
                string label = class_names[classIds[idx]] + ":" + confidences[idx].ToString("0.00");
                Cv2.PutText(result_image, label, new OpenCvSharp.Point(x0, y0 - 5), HersheyFonts.HersheySimplex, 1, new Scalar(0, 0, 255), 2);
            }

            pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
            textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";

        }

        private void pictureBox2_DoubleClick(object sender, EventArgs e)
        {
            Common.ShowNormalImg(pictureBox2.Image);
        }

        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            Common.ShowNormalImg(pictureBox1.Image);
        }
    }
}

下载

源码下载

相关推荐
袋鼠云数栈UED团队8 分钟前
一套 Spec-First 的 AI 编程工作流
前端·人工智能
Awu122718 分钟前
⚡从零开发 Agent CLI(二):CLI 框架搭建与子命令路由
人工智能·aigc
码上天下22 分钟前
React Query 缓存 AI 对话历史的几个权衡
人工智能
Scout-leaf26 分钟前
C#摸鱼实录——IoC与DI案例详解
c#
米小虾32 分钟前
2026半年盘点:AI界发生的6件大事,正在彻底改变产业格局
人工智能
咕白m6252 小时前
使用 C# 在 Excel 中应用多种字体样式
后端·c#
道友可好3 小时前
让 AI 自己验收,等于让学生自己批卷
前端·人工智能·后端
美团技术团队3 小时前
美团海报生成 AIGC 技术创新与实践
人工智能
冬哥聊AI3 小时前
放弃 Spring AI?这 3 个开源框架,才是让 SpringBoot 玩转 AI Agent 的正解
人工智能