一、首先需要訓練yolov5_seg的模型,可以去網上學習,或者你直接用我的,
訓練環境和yolov5—7.0的環境一樣,你可以直接拷過來用。
yolov5_seg算法
鏈接:https://pan.baidu.com/s/1m-3lFWRHwg5t8MmIOKm4FA?
提取碼:6qiz
或者你直接下載我的環境,都喂你嘴里,就看你吃不吃了,
標注就用pip安裝labelme就行
yolov5_seg算法的環境
鏈接:https://pan.baidu.com/s/1mwl2poblQUuFEwSpE2dvqA?
提取碼:age7
然后訓練完成后
轉化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文件
引用到控件程序中
主程序代碼如下
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_CheckedChangedprivate 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){// 用戶點擊了OKreturn;}}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;}#endregionprivate void btn_time_Click(object sender, EventArgs e){log.print();}}
}
效果演示
有問題可以加我 qq 2045618826?
當然你可以直接在大佬的代碼上面修改