ML.NET 图像分类

参考文档:https://www.cnblogs.com/mq0036/p/18302572

复制代码
using MLNET_Image;
using static MLNET_Image.MLModel1;

namespace MLNET.Image
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void btnSelectImage_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Title = "Select Image";
                openFileDialog.Filter = "Image Files (*.jpg, *.png, *.bmp)|*.jpg;*.png;*.bmp|All Files (*.*)|*.*";

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    // 获取用户选择的文件路径
                    string selectedImagePath = openFileDialog.FileName;

                    // 从文件加载图片
                    System.Drawing.Image img = System.Drawing.Image.FromFile(openFileDialog.FileName);
                    this.pictureBox1.Image = img;

                    var imageBytes = File.ReadAllBytes(selectedImagePath);
                    ModelInput sampleData = new MLModel1.ModelInput()
                    {
                        ImageSource = imageBytes,
                    };

                    //Load model and predict output
                    var result = MLModel1.Predict(sampleData);

                    var label = result.PredictedLabel;
                    var score = MLModel1.GetSortedScoresWithLabels(result).Where(x => x.Key == label).First().Value; 

                    this.lblResult.Text = result.PredictedLabel + " " + score; // 格式化为百分比
                }
            }
        }
    }
}
相关推荐
~plus~9 小时前
.NET 8 C# 委托与事件实战教程
网络·c#·.net·.net 8·委托与事件·c#进阶
rockey62713 小时前
AScript动态脚本多语言环境支持
sql·c#·.net·script·eval·function·动态脚本
dotNET实验室13 小时前
ASP.NET Core 内存缓存实战:一篇搞懂该怎么配、怎么避坑
.net
龙侠九重天14 小时前
ML.NET 实战:快速构建分类模型
分类·数据挖掘·c#·.net
无风听海16 小时前
.NET10之内置日志配置与使用指南
asp.net·.net
沃尔威武1 天前
数据库 Sinks(.net8)
数据库·.net·webview
大尚来也1 天前
告别“字符串拼接”:在.NET中用LINQ重塑数据查询
.net·solr·linq
无风听海1 天前
.NET10之Record 深度解析
.net
CodeCraft Studio2 天前
LightningChart .NET v12.5.1 发布:高性能数据可视化再升级,赋能工业与实时数据场景
信息可视化·.net·gpu·数据可视化·lightningchart·高性能图表开发·数据可视化引擎
CyL_Cly2 天前
.net framework 3.5下载( 2.0-4.8大全 )
.net