using RichCreator.Editor.Utils; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using forms=System.Windows.Forms; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using ZTImage; namespace RichCreator.Editor.Tools.CV { /// /// Trainor.xaml 的交互逻辑 /// public partial class Trainor : Window { public Trainor() { InitializeComponent(); } public string PosDir { get; set; } public string NegDir { get; set; } public string GenericXmlName { get; set; } public Int32 WinWidth { get; set; } public Int32 WinHeight { get; set; } public Int32 BlockWidth { get; set; } public Int32 BlockHeight { get; set; } public Int32 StrideWidth { get; set; } public Int32 StrideHeight { get; set; } public Int32 CellWidth { get; set; } public Int32 CellHeight { get; set; } public Int32 ChannelNumber { get; set; } private void Button_Click(object sender, RoutedEventArgs e) { if (!CheckAndFillAllParameter()) { return; } if (System.IO.File.Exists(this.GenericXmlName)) { try { System.IO.File.Delete(this.GenericXmlName); } catch(Exception ex) { ShowMessage("已经存在的xml无法移除,"+ex.ToString()); return; } } HOGTrain train = new HOGTrain( new System.Drawing.Size (this.WinWidth,this.WinHeight), new System.Drawing.Size (this.BlockWidth,this.BlockHeight), new System.Drawing.Size (this.StrideWidth,this.StrideHeight), new System.Drawing.Size (this.CellWidth,this.CellHeight), this.ChannelNumber ); string[] files = null; //训练正样本 files = System.IO.Directory.GetFiles(this.PosDir,"*.jpg",System.IO.SearchOption.AllDirectories); if (files.Length <= 0) { ShowMessage("正样本不存在"); return; } train.Train(files, 1); //训练负样本 files = System.IO.Directory.GetFiles(this.NegDir, "*.jpg", System.IO.SearchOption.AllDirectories); if (files.Length <= 0) { ShowMessage("负样本不存在"); return; } train.Train(files, -1); train.Save(this.GenericXmlName); ShowMessage("train complete"); } private Boolean CheckAndFillAllParameter() { this.GenericXmlName = this.txtGenericXmlName.Text.Trim(); if (string.IsNullOrEmpty(this.GenericXmlName)) { ShowMessage("请填写结果Xml名"); return false; } if (System.IO.File.Exists(this.GenericXmlName)) { if (MessageBox.Show("xml已经存在,是否覆盖?", "提示", MessageBoxButton.YesNo) == MessageBoxResult.No) { return false; } } this.PosDir = this.txtPosDir.Text.Trim(); if (string.IsNullOrEmpty(this.PosDir)) { ShowMessage("请选择正样本目录"); return false; } if (!System.IO.Directory.Exists(this.PosDir)) { ShowMessage("选择的正样本目录不存在"); return false; } this.NegDir = this.txtNegDir.Text.Trim(); if (string.IsNullOrEmpty(this.NegDir)) { ShowMessage("请选择负样本目录"); return false; } if (!System.IO.Directory.Exists(this.NegDir)) { ShowMessage("选择的负样本目录不存在"); return false; } this.WinWidth = TypeConverter.StringToInt(this.txtWinWidth.Text, 0); if (this.WinWidth <= 0) { ShowMessage("取样宽不合格"); return false; } this.WinHeight = TypeConverter.StringToInt(this.txtWinHeight.Text, 0); if (this.WinHeight <= 0) { ShowMessage("取样高不合格"); return false; } this.BlockWidth = TypeConverter.StringToInt(this.txtBlockWidth.Text, 0); if (this.BlockWidth <= 0||(this.WinWidth%this.BlockWidth)!=0) { ShowMessage("Block宽不合格"); return false; } this.BlockHeight = TypeConverter.StringToInt(this.txtBlockHeight.Text, 0); if (this.BlockHeight <= 0 || (this.WinHeight % this.BlockHeight) != 0) { ShowMessage("Block高不合格"); return false; } this.StrideWidth = TypeConverter.StringToInt(this.txtStrideWidth.Text, 0); if (this.StrideWidth <= 0 ) { ShowMessage("Stride宽不合格"); return false; } this.StrideHeight = TypeConverter.StringToInt(this.txtStrideHeight.Text, 0); if (this.StrideHeight <= 0) { ShowMessage("Stride高不合格"); return false; } this.CellWidth = TypeConverter.StringToInt(this.txtCellWidth.Text, 0); if (this.CellWidth <= 0||this.CellWidth*2!=this.BlockWidth) { ShowMessage("Cell宽不合格"); return false; } this.CellHeight = TypeConverter.StringToInt(this.txtCellHeight.Text, 0); if (this.CellHeight <= 0 || this.CellHeight * 2 != this.BlockHeight) { ShowMessage("Cell高不合格"); return false; } this.ChannelNumber = TypeConverter.StringToInt(this.txtChannelNumber.Text, 0); if (this.ChannelNumber <= 0) { ShowMessage("channel不合格"); return false; } return true; } private void ShowMessage(string msg) { System.Windows.MessageBox.Show(msg); } private void SelPosDir_Click(object sender, RoutedEventArgs e) { //选择正样本目录 forms.FolderBrowserDialog m_Dialog = new forms.FolderBrowserDialog(); m_Dialog.SelectedPath = AppDomain.CurrentDomain.BaseDirectory; forms.DialogResult result = m_Dialog.ShowDialog(); if (result == System.Windows.Forms.DialogResult.Cancel) { return; } string m_Dir = m_Dialog.SelectedPath.Trim(); this.txtPosDir.Text = m_Dir; } private void SelNegDir_Click(object sender, RoutedEventArgs e) { //选择负样本目录 forms.FolderBrowserDialog m_Dialog = new forms.FolderBrowserDialog(); m_Dialog.SelectedPath = AppDomain.CurrentDomain.BaseDirectory; forms.DialogResult result = m_Dialog.ShowDialog(); if (result == System.Windows.Forms.DialogResult.Cancel) { return; } string m_Dir = m_Dialog.SelectedPath.Trim(); this.txtNegDir.Text = m_Dir; } } }