yolov5实例分割跑通以及C#读取yolov5_Seg实例分割转换onnx进行检测部署

一、首先需要训练yolov5_seg的模型,可以去网上学习,或者你直接用我的,

训练环境和yolov5---7.0的环境一样,你可以直接拷过来用。

yolov5_seg算法

链接:https://pan.baidu.com/s/1m-3lFWRHwg5t8MmIOKm4FA

提取码:6qiz

或者你直接下载我的环境,都喂你嘴里,就看你吃不吃了,

yolov5_seg算法的环境

链接:https://pan.baidu.com/s/1mwl2poblQUuFEwSpE2dvqA

提取码:age7

标注就用pip安装labelme就行

标注后是json文件,需要转化成txt

import os
import cv2
import json
import numpy as np


def txt_write(x, img_x, img_y, txt):
    data = x['points']
    n = 1
    for x in data:
        for i in x:
            if n % 2 == 0:
                txt.write(' ' + str(round(i / img_x, 6)))
                n += 1
            else:
                txt.write(' ' + str(round(i / img_y, 6)))
                n += 1
    txt.write('\n')


def json2txt(json_path, save_path):
    txt = open(save_path, 'w')
    with open(json_path, "r") as f:
        data = f.read()
    data = json.loads(data)
    img_x = data['imageHeight']
    img_y = data['imageWidth']
    shapes = data['shapes']

    for x in shapes:
        # print(x['label'])
        # 此处面向不同分类,需要改动下面的标签值,如果是多分类,那么需要增加新的if
        # 只是单分类的话,可以直接去掉if,把里面的模块拿出来用

        if x['label'] == 'FG':
            txt.write('0')
            txt_write(x, img_x, img_y, txt)

        if x['label'] == 'KP':
            txt.write('1')
            txt_write(x, img_x, img_y, txt)
        #
        # if x['label'] == 'ssdp':
        #     txt.write('2')
        #     txt_write(x, img_x, img_y, txt)

    txt.close()


# 单文件测试
# save_dir = "/workspace/" #文件路径
# name = 'test'
# save_path = save_dir + name + '.txt' # 也可以是.doc
# json_path = '/json/65161.json'
# json2txt(json_path,save_path)

# 文件夹
json_dir = r'H:\DL\yolov5-seg-master\masketcp\PCB_dataset\labels\train'
save_dir = r'H:\DL\yolov5-seg-master\masketcp\PCB_dataset\labels\train'
files = os.listdir(json_dir)
os.makedirs(save_dir, exist_ok=True)
num = 1
for file in files:
    name = file[0:-5]
    json_path = json_dir + '/' + name + '.json'
    save_path = save_dir + '/' + name + '.txt'
    json2txt(json_path, save_path)
    print(num, '/', len(files), ':', name)
    num += 1

然后训练完成后

转化onnx 用export.py 转化就行

二、部署

下载源码 这是一个大佬的代码里面什么都有

https://github.com/guojin-yan/YoloDeployCsharp.git

打开vs2022

安装 OpenCvSharp4 相关的包

openvino 相关的包

剩下的少什么就下载什么就行了

你可能会报错 这个错是因为你的框架是net4.6.1左右的 但是框架换到net6.0就不会错

修改的方法就是

将这里的

\bin\Debug\dll\win-x64

所有dll库复制到带有.exe的文件夹中

也就是 \bin\Debug 中 就可以了

更改参数:

打开

Score_Threshold 置信度也就是小于此值的都被滤掉

NMS_Threshold 非极大值抑制的值,确定框的数量

classes_count_1 类别的个数 你有多少类别你就设置几个

W_H_size_1宽高设定

class_names 是你的类别名

然后我们右键dll程序,生成dll库

将YoloSegDll_all\src\YoloV5_Seg_dll\obj\Debug\YoloV5_Seg_dll.dll路径下的dll文件

引用到控件程序中

主程序代码如下

cs 复制代码
using Microsoft.ML.OnnxRuntime;
using OpenCvSharp;
using OpenVinoSharp.Extensions.result;
using OpenVinoSharp.Extensions.process;
using SharpCompress.Common;
using System;
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;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
using YoloDeployPlatform;
using YoloDeployPlatform.predictor;
using Timer = System.Windows.Forms.Timer;
using OpenVinoSharp;
using System.Security.Cryptography;
using YoloDeployPlatform.Properties;


namespace YoloDeployPlatform
{
    public partial class YoloDeployPlatform : Form
    {
        private YOLO yolo = new YOLO();
        private Log log = Log.Instance;
        private Stopwatch sw = new Stopwatch();
        private List<string> class_names= new List<string>();

        private VideoCapture video;

        private Timer video_timer = new Timer();
        private bool timerRunning = false;

        private string infer_type = "det"; 

        public YoloDeployPlatform()
        {
            InitializeComponent();
        }

        //===============================标签===============已封装为库中===========================
        //private void btn_class_select_Click(object sender, EventArgs e)
        //{
        //    //OpenFileDialog dlg = new OpenFileDialog();

        //    classesLabel label = new classesLabel();
        //    List<string> classes_name = label.class_names;
  
        //}

        //########################## 输入图片 ##########################
        //private void btn_input_select_Click(object sender, EventArgs e)
        //{
        //    OpenFileDialog dlg = new OpenFileDialog();

        //    if (dlg.ShowDialog() == DialogResult.OK)
        //    {

        //        string filePath = "";
        //        filePath=dlg.FileName;

        //    }
        //}


        #region RadioButton_CheckedChanged
        private void rb_openvino_CheckedChanged(object sender, EventArgs e)
        {
            if (rb_openvino.Checked) 
            {
                cb_device.Items.Clear();
                cb_device.Items.AddRange(new object[] { "AUTO", "CPU", "GPU.0", "GPU.1" });
                cb_device.SelectedIndex = 1;
            }
        }


        private void rb_opencv_CheckedChanged(object sender, EventArgs e)
        {
            if (rb_opencv.Checked)
            {
                cb_device.Items.Clear();
                cb_device.Items.AddRange(new object[] { "CPU"});
                cb_device.SelectedIndex = 0;
            }
        }

        #endregion

        //##############################  模型读取 &  模型推理 #########################################

        private void btn_load_model_Click(object sender, EventArgs e)
        {
            //读取图片
            OpenFileDialog dlg = new OpenFileDialog();
            string filePath = "";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                //tb_input_path.Text = dlg.FileName;
                

                filePath = dlg.FileName;
            }
            //YOLOv5Seg gb_model = new YOLOv5Seg;
            string model_type_str = check_rb(gb_model.Controls);
            if (model_type_str == "")
            {
                show_worn_msg_box("Please select a model category.");
                return;
            }
            string engine_type_str = check_rb(gb_engine.Controls);
            if (engine_type_str == "")
            {
                show_worn_msg_box("Please select an inference engine.");
                return;
            }
            ModelType model_type = MyEnum.GetModelType<ModelType>(model_type_str);
            EngineType engine_type = MyEnum.GetEngineType<EngineType>(engine_type_str);


            if ((model_type == ModelType.YOLOv5Seg))
            {
                infer_type = "seg";
            }
           

            //================================ model read =======================================

            //string model_path = tb_model_path.Text;
            string model_path = "F:\\Desk\\models\\bestsegMd.onnx";
            string device = cb_device.SelectedItem.ToString();

            string extension = Path.GetExtension(model_path);
            

            yolo.Dispose();


            //####################################### 阈 值 #################################//

            classesLabel my = new classesLabel();
            float score = my.Score_Threshold;
            float nms = my.NMS_Threshold;
            //int categ_num = my.classes_count_1;
            int categ_num = 1;
            int input_size = my.W_H_size_1;


            
            yolo = YOLO.GetYolo(model_type, model_path, engine_type, device, categ_num, score, nms, input_size);
           

            //############################# 图片处理阶段 ################################################

            DateTime start = DateTime.Now;
            //string input_path = filePath;
            Mat img = Cv2.ImRead(filePath);
            sw.Restart();
            pictureBox1.BackgroundImage = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(img);
            Mat re_img = image_predict(img);
            sw.Stop();
            pictureBox2.BackgroundImage = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(re_img);

            //DateTime end = DateTime.Now;
            //label1.Text = "耗时:" + (end - start).TotalMilliseconds.ToString();
            label2.Text = sw.ElapsedMilliseconds.ToString();

        }


        
        #region 
        private string check_rb(Control.ControlCollection controls)
        {
            string key = "";
            foreach (Control ctr in controls)
            {
                if (ctr is RadioButton && (ctr as RadioButton).Checked)
                {
                    key = ctr.Text;
                }
            }
            return key;
        }


        private void show_worn_msg_box(string message)
        {
            string caption = "Warning";
            MessageBoxButtons buttons = MessageBoxButtons.OK; // 设置按钮
            MessageBoxIcon icon = MessageBoxIcon.Warning; // 设置图标
            DialogResult result = MessageBox.Show(this, message, caption, buttons, icon);

            // 根据用户的点击按钮处理逻辑
            if (result == DialogResult.OK)
            {
                // 用户点击了OK
                return;
            }
        }

        Mat image_predict(Mat img, bool is_video = false)
        {
            Mat re_img = new Mat();


            BaseResult result;
            if (log.flag_time && !is_video)
            {
                log.flag_time = false;
                yolo.predict(img);
                log.flag_time = true;
                result = yolo.predict(img);
            }
            else
            {
                result = yolo.predict(img);
            }

            if (class_names.Count > 0)
            {
                result.update_lable(class_names);
            }

            
            re_img = Visualize.draw_seg_result(result, img);
            //}

            if (log.flag_time && log.flag_fps)
            {
                Cv2.Rectangle(re_img, new OpenCvSharp.Point(30, 20), new OpenCvSharp.Point(250, 60),
                    new Scalar(0.0, 255.0, 255.0), -1);
                Cv2.PutText(re_img, "FPS: " + (1000.0 / log.infer_time).ToString("0.00"), new OpenCvSharp.Point(50, 50),
                    HersheyFonts.HersheySimplex, 0.8, new Scalar(0, 0, 0), 2);
            }

            return re_img;
        }



        #endregion

        private void btn_time_Click(object sender, EventArgs e)
        {
            log.print();
        }

        
    }
}

效果演示

有问题可以加我 qq 2045618826

当然你可以直接在大佬的代码上面修改

相关推荐
dundunmm16 分钟前
机器学习之scikit-learn(简称 sklearn)
python·算法·机器学习·scikit-learn·sklearn·分类算法
古希腊掌管学习的神16 分钟前
[机器学习]sklearn入门指南(1)
人工智能·python·算法·机器学习·sklearn
一道微光29 分钟前
Mac的M2芯片运行lightgbm报错,其他python包可用,x86_x64架构运行
开发语言·python·macos
向宇it1 小时前
【从零开始入门unity游戏开发之——unity篇01】unity6基础入门开篇——游戏引擎是什么、主流的游戏引擎、为什么选择Unity
开发语言·unity·c#·游戏引擎
四口鲸鱼爱吃盐1 小时前
Pytorch | 利用AI-FGTM针对CIFAR10上的ResNet分类器进行对抗攻击
人工智能·pytorch·python
是娜个二叉树!1 小时前
图像处理基础 | 格式转换.rgb转.jpg 灰度图 python
开发语言·python
互联网杂货铺1 小时前
Postman接口测试:全局变量/接口关联/加密/解密
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·postman
仰望大佬0071 小时前
Avalonia实例实战五:Carousel自动轮播图
数据库·microsoft·c#
糖朝1 小时前
c#读取json
c#·json