FrmTongsLearn.cs 3.7 KB
using OnlineStore.DeviceLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace OnlineStore.XLRStore
{
    public partial class FrmTongsLearn : Form
    {
        public FrmTongsLearn()
        {
            InitializeComponent();
            boxEquip = StoreManager.XLRStore.boxEquip;
            txtScoreThreshold.Text = boxEquip.Config.TongsDetectThreshold.ToString();
        }
        Bitmap srcBitmap = null;
        BoxEquip boxEquip;
        string camName = "box_A";
        private void btnSelect_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.InitialDirectory = Application.StartupPath+Common.ConfigAppSettings.GetValue(Common.Setting_Init.ImagePath);
            fileDialog.Filter = "图片|*.bmp";
            fileDialog.RestoreDirectory = true;
            if(fileDialog.ShowDialog()==DialogResult.OK)
            {
                txtImagePath.Text = System.IO.Path.GetFullPath(fileDialog.FileName);
                srcBitmap = new Bitmap(txtImagePath.Text);
                picSrc.Image = srcBitmap;
            }
        }

        private void btnAcquire_Click(object sender, EventArgs e)
        {
            srcBitmap = boxEquip.AcqImage(camName);
            picSrc.Image = srcBitmap;
        }

        private void SideCheckedChanged(object sender, EventArgs e)
        {
            if(rdoAside.Checked)
            {
                camName = "box_A";
            }
            else if(rdoBside.Checked)
            {
                camName = "box_B";
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            boxEquip.Config.TongsDetectThreshold = float.Parse(txtScoreThreshold.Text);
            StoreManager.SaveBoxConfig(boxEquip.Config);
        }

        private void btnMatch_Click(object sender, EventArgs e)
        {
            if(srcBitmap !=null)
            {
               float score= boxEquip.match(srcBitmap, out string pn, out Bitmap bitmap);
                picResult.Image = bitmap;
                updateResult(score);
            }
        }
        private void updateResult(float score)
        {
            lblResult.Text = score.ToString("f2");
            if (score<boxEquip.Config.TongsDetectThreshold)
            {
                lblResult.BackColor = Color.Red;
            }
            else
            {
                lblResult.BackColor = Color.GreenYellow;
            }
        }

        private void btnLearn_Click(object sender, EventArgs e)
        {
            if (srcBitmap != null)
                boxEquip.learn(srcBitmap, Guid.NewGuid().ToString());
        }

        private void btnSelectPatch_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderDialog = new FolderBrowserDialog();
            folderDialog.Description = "请选择需要训练图像的路径";
            if (folderDialog.ShowDialog() == DialogResult.OK)
            {
                txtImagePath.Text = folderDialog.SelectedPath+@"\";
                string[] files = System.IO.Directory.GetFiles(txtImagePath.Text);
                lblResult.Text = "开始学习...";
                System.Threading.Tasks.Task task =  System.Threading.Tasks.Task.Factory.StartNew(delegate {
                    foreach (var item in files)
                    {
                        boxEquip.learn(item, Guid.NewGuid().ToString());
                    }
                });
                task.Wait();
                lblResult.Text = "学习完成";
                lblResult.BackColor = Color.AliceBlue;
            }
        }
    }
}