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; // 格式化为百分比
                }
            }
        }
    }
}
相关推荐
长孙豪翔10 小时前
在.net中读写config文件的各种方法
java·数据库·.net
GV191rLvq16 小时前
查看w3wp进程占用的内存及.NET内存泄露,死锁分析
.net
界面开发小八哥18 小时前
界面控件DevExpress .NET Reports v26.1新版亮点 - 持续增强可访问性
.net·界面控件·devexpress·ui开发·报表控件
wei1986211 天前
.net添加web引用和添加服务引用有什么区别?
java·前端·.net
pW3g3lLuu2 天前
.NET 高级开发 | http 接口对接和客户端开发技巧
网络协议·http·.net
CSharp精选营3 天前
.NET 8 与 .NET 9 支持终止倒计时:开发者需要了解什么
.net·lts·.net8·.net9·码农刚子·升级迁移·sts·支持终止
hez20105 天前
在 .NET 上构建超大托管数组
c#·.net·.net core·gc·clr
唐青枫12 天前
线程不是越多越快:C#.NET Thread 生命周期、同步与后台工作线程实战
c#·.net
唐青枫13 天前
别只会反射:C#.NET Emit 动态生成代码实战详解
c#·.net