【测试环境】
vs2019
net framework4.7.2
onnxruntime==1.16.3
opencvsharp
注意源码运行在CPU上不支持GPU运行,由于net framework限制GPU会很慢因此没有GPU版本提供。
【运行步骤】
打开sln项目

选择x64 debug运行即可

如需要再x64 release运行可以将x64 debug文件夹里面所有文件复制到x64 release文件夹里面然后再vs2019切换到x64 release运行即可。
【效果展示】


【界面设计代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenCvSharp;
namespace FIRC
{
public partial class Form1 : Form
{
Mat src = new Mat();
Yolov8SegManager ym = new Yolov8SegManager();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "图文件(*.*)|*.jpg;*.png;*.jpeg;*.bmp";
openFileDialog.RestoreDirectory = true;
openFileDialog.Multiselect = false;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
src = Cv2.ImRead(openFileDialog.FileName);
pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(src);
}
}
private void button2_Click(object sender, EventArgs e)
{
if(pictureBox1.Image==null)
{
return;
}
Stopwatch sw = new Stopwatch();
sw.Start();
var result = ym.Inference(src);
sw.Stop();
this.Text = "耗时" + sw.Elapsed.TotalSeconds + "秒";
var resultMat = ym.DrawImage(src,result);
pictureBox2.Image= OpenCvSharp.Extensions.BitmapConverter.ToBitmap(resultMat); //Mat转Bitmap
}
private void Form1_Load(object sender, EventArgs e)
{
ym.LoadWeights(Application.StartupPath+ "\\weights\\yolov8n-seg.onnx", Application.StartupPath + "\\weights\\labels.txt");
}
private void btn_video_Click(object sender, EventArgs e)
{
var detector = new Yolov8SegManager();
detector.LoadWeights(Application.StartupPath + "\\weights\\yolov8n-seg.onnx", Application.StartupPath + "\\weights\\labels.txt");
VideoCapture capture = new VideoCapture(0);
if (!capture.IsOpened())
{
Console.WriteLine("video not open!");
return;
}
Mat frame = new Mat();
var sw = new Stopwatch();
int fps = 0;
while (true)
{
capture.Read(frame);
if (frame.Empty())
{
Console.WriteLine("data is empty!");
break;
}
sw.Start();
var result = detector.Inference(frame);
var resultImg = detector.DrawImage(frame,result);
sw.Stop();
fps = Convert.ToInt32(1 / sw.Elapsed.TotalSeconds);
sw.Reset();
Cv2.PutText(resultImg, "FPS=" + fps, new OpenCvSharp.Point(30, 30), HersheyFonts.HersheyComplex, 1.0, new Scalar(255, 0, 0), 3);
//显示结果
Cv2.ImShow("Result", resultImg);
int key = Cv2.WaitKey(10);
if (key == 27)
break;
}
capture.Release();
}
}
}
【训练数据集介绍】
注意数据集中有增强图片
数据集格式:labelme格式(不包含mask文件,仅仅包含jpg图片和对应的json文件)
图片数量(jpg文件个数):9339
标注数量(json文件个数):9339
标注类别数:1
标注类别名称:["Nail"]
每个类别标注的框数:
Nail count = 38632
使用标注工具:labelme=5.5.0
所在仓库:firc-dataset
图片分辨率:640x640
标注规则:对类别进行画多边形框polygon
重要说明:可以将数据集用labelme打开编辑,json数据集需自己转成mask或者yolo格式或者coco格式作语义分割或者实例分割
特别声明:本数据集不对训练的模型或者权重文件精度作任何保证
图片预览:


标注例子:


【提供文件】
C#源码+所有DLL文件
yolov8-seg.onnx模型文件(注意不提供pytorch模型)
测试图片若干
不包含训练的数据集
【源码地址】