目录
效果
模型信息
项目
代码
下载
Model Properties
-------------------------
date:2023-09-07T17:11:46.798385
description:Ultralytics YOLOv8n-seg model trained on coco.yaml
author:Ultralytics
task:segment
license:AGPL-3.0 https://ultralytics.com/license
version:8.0.172
stride:32
batch:1
imgsz:[640, 640]
names:{0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus', 6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant', 11: 'stop sign', 12: 'parking meter', 13: 'bench', 14: 'bird', 15: 'cat', 16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear', 22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag', 27: 'tie', 28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard', 32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove', 36: 'skateboard', 37: 'surfboard', 38: 'tennis racket', 39: 'bottle', 40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl', 46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli', 51: 'carrot', 52: 'hot dog', 53: 'pizza', 54: 'donut', 55: 'cake', 56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining table', 61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard', 67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink', 72: 'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors', 77: 'teddy bear', 78: 'hair drier', 79: 'toothbrush'}
---------------------------------------------------------------
Inputs
-------------------------
name:images
tensor:Float[1, 3, 640, 640]
---------------------------------------------------------------
Outputs
-------------------------
name:output0
tensor:Float[1, 116, 8400]
name:output1
tensor:Float[1, 32, 160, 160]
---------------------------------------------------------------
using OpenCvSharp;
using Sdcb.OpenVINO;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace OpenVino_Yolov8_Seg
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
string image_path = "";
string model_path;
string classer_path;
Mat src;
StringBuilder sb = new StringBuilder();
SegmentationResult result_pro;
Result result;
CompiledModel cm;
InferRequest ir;
Shape inputShape;
private void Form1_Load(object sender, EventArgs e)
{
model_path = "model\\yolov8n-seg.onnx";
classer_path = "model\\lable.txt";
Model rawModel = OVCore.Shared.ReadModel(model_path);
var ad = OVCore.Shared.AvailableDevices;
Console.WriteLine("可用设备");
foreach (var item in ad)
{
Console.WriteLine(item);
}
cm = OVCore.Shared.CompileModel(rawModel, "CPU");
ir = cm.CreateInferRequest();
inputShape = cm.Inputs.Primary.Shape;
image_path = "test_img\\demo_2.jpg";
pictureBox1.Image = new Bitmap(image_path);
textBox1.Text = "";
src = new Mat(image_path);
pictureBox2.Image = null;
result_pro = new SegmentationResult(classer_path, null);
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = fileFilter;
if (ofd.ShowDialog() != DialogResult.OK) return;
pictureBox1.Image = null;
image_path = ofd.FileName;
pictureBox1.Image = new Bitmap(image_path);
textBox1.Text = "";
src = new Mat(image_path);
pictureBox2.Image = null;
}
unsafe private void button2_Click(object sender, EventArgs e)
{
if (pictureBox1.Image == null)
{
return;
}
pictureBox2.Image = null;
textBox1.Text = "";
sb.Clear();
button2.Enabled = false;
Application.DoEvents();
Stopwatch stopwatch = new Stopwatch();
//图片缩放
Mat image = new Mat(image_path);
int max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;
Mat max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);
Rect roi = new Rect(0, 0, image.Cols, image.Rows);
image.CopyTo(new Mat(max_image, roi));
Mat image_rgb = new Mat();
Cv2.CvtColor(max_image, image_rgb, ColorConversionCodes.BGR2RGB);
Mat resize_image = new Mat();
Cv2.Resize(image_rgb, resize_image, new OpenCvSharp.Size(640, 640));
float[] factors = new float[4];
factors[0] = factors[1] = (float)(max_image_length / 640.0);
factors[2] = src.Rows;
factors[3] = src.Cols;
result_pro.scales = factors;
resize_image.ConvertTo(resize_image, MatType.CV_32FC3, 1.0 / 255);
float[] input_tensor_data = Common.ExtractMat(resize_image);
resize_image.Dispose();
image_rgb.Dispose();
using (Tensor input_x = Tensor.FromArray(input_tensor_data, new Shape(1, 3, 640, 640)))
{
ir.Inputs[0] = input_x;
}
double preprocessTime = stopwatch.Elapsed.TotalMilliseconds;
stopwatch.Restart();
ir.Run();
double inferTime = stopwatch.Elapsed.TotalMilliseconds;
stopwatch.Restart();
using (Tensor output_det = ir.Outputs[0])
using (Tensor output_proto = ir.Outputs[1])
{
float[] det_result_array = output_det.GetData
float[] proto_result_array = output_proto.GetData
result = result_pro.process_result(det_result_array, proto_result_array);
double postprocessTime = stopwatch.Elapsed.TotalMilliseconds;
stopwatch.Stop();
double totalTime = preprocessTime + inferTime + postprocessTime;
Mat result_image = result_pro.draw_result(result, image.Clone());
sb.AppendLine($"Preprocess: {preprocessTime:F2}ms");
sb.AppendLine($"Infer: {inferTime:F2}ms");
sb.AppendLine($"Postprocess: {postprocessTime:F2}ms");
sb.AppendLine($"Total: {totalTime:F2}ms");
sb.AppendLine("---------------------------------------");
for (int i = 0; i < result.length; i++)
{
sb.AppendLine(result.classes[i] + " " + result.scores[i].ToString("P2"));
}
if (pictureBox2.Image != null)
{
pictureBox2.Image.Dispose();
}
pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
textBox1.Text = sb.ToString();
}
image.Dispose();
button2.Enabled = true;
}
}
}
using OpenCvSharp;
using Sdcb.OpenVINO;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace OpenVino_Yolov8_Seg
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
string image_path = "";
string model_path;
string classer_path;
Mat src;
StringBuilder sb = new StringBuilder();
SegmentationResult result_pro;
Result result;
CompiledModel cm;
InferRequest ir;
Shape inputShape;
private void Form1_Load(object sender, EventArgs e)
{
model_path = "model\\yolov8n-seg.onnx";
classer_path = "model\\lable.txt";
Model rawModel = OVCore.Shared.ReadModel(model_path);
var ad = OVCore.Shared.AvailableDevices;
Console.WriteLine("可用设备");
foreach (var item in ad)
{
Console.WriteLine(item);
}
cm = OVCore.Shared.CompileModel(rawModel, "CPU");
ir = cm.CreateInferRequest();
inputShape = cm.Inputs.Primary.Shape;
image_path = "test_img\\demo_2.jpg";
pictureBox1.Image = new Bitmap(image_path);
textBox1.Text = "";
src = new Mat(image_path);
pictureBox2.Image = null;
result_pro = new SegmentationResult(classer_path, null);
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = fileFilter;
if (ofd.ShowDialog() != DialogResult.OK) return;
pictureBox1.Image = null;
image_path = ofd.FileName;
pictureBox1.Image = new Bitmap(image_path);
textBox1.Text = "";
src = new Mat(image_path);
pictureBox2.Image = null;
}
unsafe private void button2_Click(object sender, EventArgs e)
{
if (pictureBox1.Image == null)
{
return;
}
pictureBox2.Image = null;
textBox1.Text = "";
sb.Clear();
button2.Enabled = false;
Application.DoEvents();
Stopwatch stopwatch = new Stopwatch();
//图片缩放
Mat image = new Mat(image_path);
int max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;
Mat max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);
Rect roi = new Rect(0, 0, image.Cols, image.Rows);
image.CopyTo(new Mat(max_image, roi));
Mat image_rgb = new Mat();
Cv2.CvtColor(max_image, image_rgb, ColorConversionCodes.BGR2RGB);
Mat resize_image = new Mat();
Cv2.Resize(image_rgb, resize_image, new OpenCvSharp.Size(640, 640));
float[] factors = new float[4];
factors[0] = factors[1] = (float)(max_image_length / 640.0);
factors[2] = src.Rows;
factors[3] = src.Cols;
result_pro.scales = factors;
resize_image.ConvertTo(resize_image, MatType.CV_32FC3, 1.0 / 255);
float[] input_tensor_data = Common.ExtractMat(resize_image);
resize_image.Dispose();
image_rgb.Dispose();
using (Tensor input_x = Tensor.FromArray(input_tensor_data, new Shape(1, 3, 640, 640)))
{
ir.Inputs[0] = input_x;
}
double preprocessTime = stopwatch.Elapsed.TotalMilliseconds;
stopwatch.Restart();
ir.Run();
double inferTime = stopwatch.Elapsed.TotalMilliseconds;
stopwatch.Restart();
using (Tensor output_det = ir.Outputs[0])
using (Tensor output_proto = ir.Outputs[1])
{
float[] det_result_array = output_det.GetData().ToArray();
float[] proto_result_array = output_proto.GetData().ToArray();
result = result_pro.process_result(det_result_array, proto_result_array);
double postprocessTime = stopwatch.Elapsed.TotalMilliseconds;
stopwatch.Stop();
double totalTime = preprocessTime + inferTime + postprocessTime;
Mat result_image = result_pro.draw_result(result, image.Clone());
sb.AppendLine($"Preprocess: {preprocessTime:F2}ms");
sb.AppendLine($"Infer: {inferTime:F2}ms");
sb.AppendLine($"Postprocess: {postprocessTime:F2}ms");
sb.AppendLine($"Total: {totalTime:F2}ms");
sb.AppendLine("---------------------------------------");
for (int i = 0; i < result.length; i++)
{
sb.AppendLine(result.classes[i] + " " + result.scores[i].ToString("P2"));
}
if (pictureBox2.Image != null)
{
pictureBox2.Image.Dispose();
}
pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
textBox1.Text = sb.ToString();
}
image.Dispose();
button2.Enabled = true;
}
}
}
源码下载
想尝试自行编译OpenVinoSharp的,地址:https://blog.csdn.net/lw112190/article/details/132753726