目录
效果
Model Properties
-------------------------
date:2023-09-05T13:17:15.396588
description:Ultralytics YOLOv8n model trained on coco.yaml
author:Ultralytics
task:detect
license:AGPL-3.0 https://ultralytics.com/license
version:8.0.170
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, 84, 8400]
---------------------------------------------------------------
VS2022
.net framework 4.8
OpenCvSharp 4.8
openvino_2023.0.1.11005
// 图片缩放
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));
float[] result_array = new float[8400 * 84];
float[] factors = new float[2];
factors = new float[2];
factors[0] = factors[1] = (float)(max_image_length / 640.0);
byte[] image_data = max_image.ImEncode(".bmp");
//存储byte的长度
ulong image_size = Convert.ToUInt64(image_data.Length);
// 加载推理图片数据
core.load_input_data("images", image_data, image_size, 1);
// 模型推理
dt1 = DateTime.Now;
core.infer();
dt2 = DateTime.Now;
// 读取推理结果
result_array = core.read_infer_result
DetectionResult result_pro = new DetectionResult(classer_path, factors);
Mat result_image = result_pro.draw_result(result_pro.process_result(result_array), image.Clone());
pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
textBox1.Text = "耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
- using OpenCvSharp;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using static System.Net.Mime.MediaTypeNames;
-
- namespace OpenVino_Yolov8_Detect
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
- string image_path = "";
-
- String startupPath;
-
- DateTime dt1 = DateTime.Now;
- DateTime dt2 = DateTime.Now;
- String model_path;
- string classer_path;
- StringBuilder sb = new StringBuilder();
- Core core;
- Mat image;
-
- 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 = "";
- image = new Mat(image_path);
- }
-
- private void Form1_Load(object sender, EventArgs e)
- {
- startupPath = System.Windows.Forms.Application.StartupPath;
- model_path = startupPath + "\\yolov8n.onnx";
- classer_path = startupPath + "\\det_lable.txt";
- core = new Core(model_path, "CPU");
- }
-
- private void button2_Click(object sender, EventArgs e)
- {
- if (image_path == "")
- {
- return;
- }
-
- // 配置图片数据
- 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));
-
- float[] result_array = new float[8400 * 84];
- float[] factors = new float[2];
- factors = new float[2];
- factors[0] = factors[1] = (float)(max_image_length / 640.0);
-
- byte[] image_data = max_image.ImEncode(".bmp");
- //存储byte的长度
- ulong image_size = Convert.ToUInt64(image_data.Length);
- // 加载推理图片数据
- core.load_input_data("images", image_data, image_size, 1);
- // 模型推理
- dt1 = DateTime.Now;
- core.infer();
- dt2 = DateTime.Now;
- // 读取推理结果
- result_array = core.read_infer_result<float>("output0", 8400 * 84);
-
- DetectionResult result_pro = new DetectionResult(classer_path, factors);
- Mat result_image = result_pro.draw_result(result_pro.process_result(result_array), image.Clone());
-
- pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
-
- textBox1.Text = "耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
- }
-
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- core.delet();
- }
- }
- }