private static void CollectTrainingExamples(OCR.ShapeNet ShapeNet, List <Example> Examples) { List <String> Filenames = new List <string>(0); Example newExample; int NumberExamples = ShapeNet.lNumberExamples; //init learning data //collect all available examples for (int lIndex = 0; lIndex < ShapeNet.ShapeList.Count; lIndex++) { Filenames.Clear(); MakeExampleList(Filenames, ShapeNet.ShapeList[lIndex].SampleFolder); if (Filenames.Count > 0) { for (long lIndex2 = 0; lIndex2 < NumberExamples; lIndex2++) { newExample = new Example(); newExample.Filename = Filenames[_r.Next(Filenames.Count)]; newExample.ShapeId = lIndex; Examples.Add(newExample); } } } }
private void CreateNetwork(OCR.ShapeNet ShapeNet) { ShapeNet.NeuralNetwork.ClearNetwork(); int OutputNodes = ShapeNet.ShapeList.Count; int Layer1Nodes = Convert.ToInt32(txtNodesLayer1.Text); int Layer2Nodes = Convert.ToInt32(txtNodesLayer2.Text); //fill the network with nodes for (int lIndex = 0; lIndex < 48; lIndex++) { ShapeNet.NeuralNetwork.AddNode(0, eNeuralNodeType.eInput); } for (int lIndex = 0; lIndex < Layer1Nodes; lIndex++) { ShapeNet.NeuralNetwork.AddNode(1, eNeuralNodeType.eHidden); } for (int lIndex = 0; lIndex < Layer2Nodes; lIndex++) { ShapeNet.NeuralNetwork.AddNode(2, eNeuralNodeType.eHidden); } for (int lIndex = 0; lIndex < OutputNodes; lIndex++) { ShapeNet.NeuralNetwork.AddNode(3, eNeuralNodeType.eOutput); } ShapeNet.NeuralNetwork.InitNetworkForLearning(); }
/// <summary> /// This function checks if this component is recognised as a connected component, meaning that /// it may consist of multiple characters. /// Rules /// 1. Recognised as connected /// 2. Low probability in recognition /// </summary> public void CheckAndRepairConnected(ShapeNet ShapeNet) { if (CouldBeConnected) { RecogniseComponent.NumberConnectedComponents++; if (DebugTrace.DebugTrace.ApplySplitOnConnectedComponents) { Split(ShapeNet); } } }
private void FillListView(OCR.ShapeNet SelectedShapeNet) { lvShapes.SelectedItems.Clear(); lvShapes.ItemsSource = SelectedShapeNet.ShapeList; lvShapes.Items.Refresh(); //foreach (OCR.ShapeListEntry Shape in SelectedShapeNet.ShapeList) //{ // lvShapes.Items.Add(Shape); //} }
/// <summary> /// This functions 'recognises' the given compontent by running its features through /// a neural network. /// </summary> /// <param name="Component"></param> public static void RecogniseWithoutConnectedRepair(ShapeNet ShapeNet, PageComponent Component) { sNeuralOutput Result = new sNeuralOutput(); ShapeNet.NeuralNetwork.ComputeOutput(RecogniseComponent.CalculateNetworkInput(Component), Result); for (int i = 0; i < Result.fOutputs.Count; i++) { if (Result.fOutputs[i] >= 0.01) { Component.AddRecognitionResult(Result.fOutputs[i], ShapeNet.ShapeList[i].Shape); } } }
private static void CreateTrainingData(OCR.ShapeNet ShapeNet, List <Example> Examples) { NeuralNetwork.LearningData oLearningData; OCR.PageComponent PageComponent; sNeuralInput newInput; foreach (Example Example in Examples) { PageComponent = new OCR.PageComponent(); PageComponent.LoadBitmap(Example.Filename); ExtractFeatures.ExecuteExtractFeatures(PageComponent, true); PageComponent.Position.Ascent = ShapeNet.ShapeList[Example.ShapeId].Position.Ascent; PageComponent.Position.Height = ShapeNet.ShapeList[Example.ShapeId].Position.Height; PageComponent.Position.Center = ShapeNet.ShapeList[Example.ShapeId].Position.Center; PageComponent.Position.Base = ShapeNet.ShapeList[Example.ShapeId].Position.Base; PageComponent.Position.Descent = ShapeNet.ShapeList[Example.ShapeId].Position.Descent; //Fill the oLearningData = new NeuralNetwork.LearningData(); oLearningData.oInput.fInputs.Clear(); oLearningData.oOutput.fOutputs.Clear(); newInput = RecogniseComponent.CalculateNetworkInput(PageComponent); oLearningData.oInput = newInput; for (long lIndex2 = 0; lIndex2 < ShapeNet.ShapeList.Count; lIndex2++) { if (Example.ShapeId == lIndex2) { oLearningData.oOutput.fOutputs.Add(1); } else { oLearningData.oOutput.fOutputs.Add(0); } } ShapeNet.NeuralNetwork.AddSituation(oLearningData); } ShapeNet.NeuralNetwork.ComputeInputRatios(); }
/// <summary> /// This functions 'recognises' the given compontent by running its features through /// a neural network. /// </summary> /// <param name="Component"></param> public static void Recognise(ShapeNet ShapeNet, PageComponent Component) { RecogniseWithoutConnectedRepair(ShapeNet, Component); Component.CheckAndRepairConnected(ShapeNet); }
private void FillShapeList(OCR.ShapeNet SelectedShapeNet) { lstShapes.UnselectAll(); lstShapes.ItemsSource = SelectedShapeNet.ShapeList; lstShapes.Items.Refresh(); }
public void DoFillFileList(string Filter, OCR.ShapeNet ShapeNet) { try { if (ShapeFiles == null) { ShapeFiles = new List <ShapeFileList>(0); } if (ShapeFiles.Count == 0) { DirectoryInfo di = new DirectoryInfo("D:\\OCR\\Images\\"); FileInfo[] Files = di.GetFiles("*_org.bmp"); foreach (FileInfo fi in Files) { if (Filter.Length > 0) { String Filename; RecognitionResult MostLikelyShape; // OCR.ShapeNet Shapenet = new OCR.ShapeNet(); PageComponent Component = new PageComponent(); //Get the selected item Filename = fi.Name.ToString(); //Load the image Component.LoadBitmap(fi.FullName.ToString()); //Extract features and recognize ExtractFeatures.ExecuteExtractFeatures(Component, true); RecogniseComponent.Recognise(ShapeNet, Component); //Fill the recognition list MostLikelyShape = new RecognitionResult(); MostLikelyShape.Probability = 0; foreach (RecognitionResult Result in Component.RecognitionResults) { if (Result.Probability > MostLikelyShape.Probability) { MostLikelyShape = Result; } } //Add ShapeFile to cache ShapeFileList ShapeFile; ShapeFile.Shape = MostLikelyShape.Content; ShapeFile.File = fi.Name.ToString(); ShapeFiles.Add(ShapeFile); //Add file to filelist if (MostLikelyShape.Content == Filter) { bwFileList.ReportProgress(0, fi.Name.ToString()); } } else { bwFileList.ReportProgress(0, fi.Name.ToString()); } } } else { foreach (ShapeFileList ShapeFile in ShapeFiles) { if (Filter.Length == 0) { bwFileList.ReportProgress(0, ShapeFile.File); } else { if (Filter == ShapeFile.Shape) { bwFileList.ReportProgress(0, ShapeFile.File); } } } } } catch (Exception e) { Console.WriteLine("Exception caught: " + e.Message); Console.WriteLine(" In: " + e.StackTrace); } }
/// <summary> /// This functions splits a character in multiple components. Used for splitting connected characters /// </summary> public void Split(ShapeNet ShapeNet) { List <int> SplitLines; //Step 1: Get the position for possible splits SplitLines = SplitDeterminePositions(); if (SplitLines.Count == 0) { return; } RecognitionResults.Clear(); //Step 2: Find the combination of the best (highest scores) recognised components List <PageComponent> Components = new List <PageComponent>(0); PageComponent newComponent; PageComponent prevComponent; Rectangle SplitArea; int start, end; SplitLines.Insert(0, 0); SplitLines.Add(Width - 1); start = 0; end = 1; while (end < SplitLines.Count) { SplitArea = new Rectangle(SplitLines[start] + 1, 0, SplitLines[end] - SplitLines[start] - 2, Height); if (SplitArea.Width > 0 && SplitArea.Height > 0) { while (NumberPixelsOnRow(BinaryBytes, SplitArea, 0, 0) == 0) { SplitArea.Y = SplitArea.Y + 1; SplitArea.Height = SplitArea.Height - 1; } while (NumberPixelsOnRow(BinaryBytes, SplitArea, SplitArea.Height - 1, 0) == 0) { SplitArea.Height = SplitArea.Height - 1; } newComponent = PartialCopy(SplitArea); ExtractFeatures.ExecuteExtractFeatures(newComponent, false); RecogniseComponent.RecogniseWithoutConnectedRepair(ShapeNet, newComponent); if (Components.Count > 0 && end - start > 1) { prevComponent = Components.Last(); if (prevComponent.ContentProbability < newComponent.ContentProbability && newComponent.Content != "connected" && newComponent.Content != "garbage") { Components.Remove(prevComponent); Components.Add(newComponent); } else { start = end - 1; end--; } } else { Components.Add(newComponent); } } end++; } //Add the new recognition result RecognitionResult newResult; newResult = new RecognitionResult(); newResult.Content = ""; newResult.Probability = 0; foreach (PageComponent Component in Components) { newResult.Content += Component.Content; newResult.Probability += Component.ContentProbability; } newResult.Probability = newResult.Probability / Components.Count; RecognitionResults.Add(newResult); //Save a copy of the image to the disc if (DebugTrace.DebugTrace.TraceFeatures) { String ComponentID = "000000" + ID; ComponentID = ComponentID.Substring(ComponentID.Length - 6); foreach (int SplitLine in SplitLines) { int pointer = SplitLine; for (int y = 0; y < Height; y++) { if (BinaryBytes[SplitLine, y] == 0xFF) { BinaryBytes[SplitLine, y] = 0x10; } pointer += Stride; } } Bitmap Bitmap = DebugTrace.DebugTrace.CreateBitmapFromByteArray(BinaryBytes, new Size(Width, Height)); String Filename = DebugTrace.DebugTrace.TraceFeatureFolder + "image_" + ComponentID + "_split.bmp"; Bitmap.Save(Filename); } }