예제 #1
0
        public void CreateTrainingFile(HObject region, HImage img, string Class)
        {
            //HRegion region, selectedRegion;
            string trainfile = directory + trainFile + trainFileExtension;

            // region = img.Threshold(100.0, 255).Connection().SortRegion("character", "true", "row");
            // for object, the index start from 1.
            //selectedRegion = region.SelectObj(1);
            HOperatorSet.AppendOcrTrainf(region, img, Class, trainfile);
        }
예제 #2
0
        private void btn_TrainOcr_Click(object sender, EventArgs e)
        {
            if (hDisplay1.GetSearchRegions().Count == 0)
            {
                MessageBox.Show("请先添加搜索区域");
                return;
            }
            if (string.IsNullOrEmpty(txt_CharacterTrained.Text))
            {
                MessageBox.Show("请先填写要训练的字符");
                return;
            }
            HTuple  hv_Number;
            HObject _roi              = hDisplay1.GetSearchRegions().ElementAt(0);
            HObject _region           = new HObject();
            HObject _imagereduced     = new HObject();
            HObject ho_ObjectSelected = new HObject();

            HOperatorSet.ReduceDomain(m_image, _roi, out _imagereduced);
            HOperatorSet.Threshold(_imagereduced, out _region, m_OcrParam.GrayMin, m_OcrParam.GrayMax);
            HOperatorSet.Connection(_region, out _region);
            HOperatorSet.SelectShape(_region, out _region, "area", "and", m_OcrParam.AreaMin, m_OcrParam.AreaMax);
            if (cklb_SortRegionModle.GetItemChecked(0))
            {
                HOperatorSet.SortRegion(_region, out _region, "character", "true", "row");
            }
            else
            {
                HOperatorSet.SortRegion(_region, out _region, "character", "true", "column");
            }
            HOperatorSet.CountObj(_region, out hv_Number);

            if (hv_Number.I == 0)
            {
                MessageBox.Show("分割字符为空");
                return;
            }
            string[] _characterTrained = new string[] { };
            _characterTrained = txt_CharacterTrained.Text.Split(',');
            if (_characterTrained.Length != hv_Number.I)
            {
                MessageBox.Show("字符区域与字符个数不相等");
                return;
            }

            HTuple         hv_Index     = new HTuple();
            List <RegionX> _listregionx = new List <RegionX>();

            for (hv_Index = 1; hv_Index.Continue(hv_Number, 1); hv_Index = hv_Index.TupleAdd(1))
            {
                ho_ObjectSelected.Dispose();
                HOperatorSet.SelectObj(_region, out ho_ObjectSelected, hv_Index);
                RegionX _hregion = new RegionX(ho_ObjectSelected.CopyObj(1, -1), "green");
                _listregionx.Add(_hregion);
            }
            string path             = string.Empty;
            string Foldername       = string.Empty;
            string trffilepath      = string.Empty;
            FolderBrowserDialog fbd = new FolderBrowserDialog();

            fbd.ShowNewFolderButton = true;
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                path = fbd.SelectedPath;
                if (!Directory.Exists(path))
                {
                    MessageBox.Show($"不存在 '{path}' 路径!");
                    return;
                }
                string[] _f = path.Split('\\');
                Foldername  = _f[_f.Length - 1];
                trffilepath = path + "\\" + Foldername + ".trf";
            }
            else
            {
                return;
            }
            HObject _characterRegion = new HObject();

            HOperatorSet.GenEmptyObj(out _characterRegion);
            for (int i = 0; i < hv_Number.I; i++)
            {
                HOperatorSet.AppendOcrTrainf(_region[i + 1], m_image, _characterTrained[i], trffilepath);
            }

            HTuple _ocrhandle = new HTuple();
            HTuple error      = new HTuple();
            HTuple errorlog   = new HTuple();

            HOperatorSet.CreateOcrClassMlp(m_TrainParam.CharacterWidth, m_TrainParam.CharacterHeiht, m_TrainParam.ZoomModle, "default", _characterTrained,
                                           80, "none", 10, 42, out _ocrhandle);
            HOperatorSet.TrainfOcrClassMlp(_ocrhandle, trffilepath, 200, 1, 0.01, out error, out errorlog);
            HOperatorSet.WriteOcrTrainf(_region, m_image, _characterTrained, trffilepath);
            HOperatorSet.WriteOcrClassMlp(_ocrhandle, path + "\\" + Foldername);
            DelegateUIControl.GetInstance().UpdateHDisplay("FormSetHDisplay", m_image, _listregionx, null);
        }