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; // 格式化为百分比
                }
            }
        }
    }
}
相关推荐
mudtools7 小时前
在.NET现有系统中快速集成飞书任务分配能力
后端·c#·.net
小羊没烦恼!11 小时前
【译】利用Asp.net MVC处理文件的上传下载
开发语言·缓存·c#·word·powerpoint·.net
唐青枫2 天前
我用 C# 开发了一款电子发票自动整理工具
c#·.net
我是唐青枫2 天前
我用 C# 开发了一款电子发票自动整理工具
c#·.net
唐青枫2 天前
别把 WPF 写成一个 MainWindow:C#.NET Prism 模块化开发详解
c#·.net
小羊没烦恼!7 天前
微服务化的基石——持续集成
java·大数据·word·powerpoint·.net
_Jonas7 天前
.net framework开发环境正式环境配置文件的切换
.net·wpf
Y3815326629 天前
C# 调用谷歌搜索 API:HttpClient 与 System.Text.Json 实战
搜索引擎·.net
qq_410194299 天前
.NET 8.0 下基于 PaddleOCRSharp 的离线文字识别落地指南
算法·.net
智码看视界10 天前
第三篇:卷积神经网络架构演进——从 LeNet 到 ResNet
计算机视觉·cnn·resnet·卷积神经网络·图像分类