private void btnTestNetwork_Click(object sender, RoutedEventArgs e) { OCR.ShapeNet SelectedShapeNet; PageComponent PageComponent; List <Example> Examples = new List <Example>(0); bool Result; //Collect the examples SelectedShapeNet = (OCR.ShapeNet)lstNeuralNetworks.SelectedItem; List <String> Filenames = new List <string>(0); Example newExample; //collect all available examples for (int lIndex = 0; lIndex < SelectedShapeNet.ShapeList.Count; lIndex++) { Filenames.Clear(); MakeExampleList(Filenames, SelectedShapeNet.ShapeList[lIndex].SampleFolder); for (int lIndex2 = 0; lIndex2 < Filenames.Count; lIndex2++) { newExample = new Example(); newExample.Filename = Filenames[lIndex2]; newExample.ShapeId = lIndex; Examples.Add(newExample); } } //Test all examples against the network foreach (Example Example in Examples) { PageComponent = new PageComponent(); PageComponent.LoadBitmap(Example.Filename); Result = false; ExtractFeatures.ExecuteExtractFeatures(PageComponent, true); RecogniseComponent.RecogniseWithoutConnectedRepair(SelectedShapeNet, PageComponent); if (PageComponent.RecognitionResults.Count > 0 && Example.ShapeId < SelectedShapeNet.ShapeList.Count) { if (SelectedShapeNet.ShapeList[Example.ShapeId].Shape.ToLower() == PageComponent.RecognitionResults[0].Content.ToLower()) { Result = true; } } if (!Result) { lstFailedExamples.Items.Add(Example.Filename); } } }
private void lstFiles_SelectionChanged(object sender, SelectionChangedEventArgs e) { String Filename; String ResultString; RecognitionResult MostLikelyShape; try { // OCR.ShapeNet Shapenet = new OCR.ShapeNet(); PageComponent Component = new PageComponent(); //Get the selected item Filename = "D:\\OCR\\Images\\" + (String)lstFiles.SelectedItem; if (lstFiles.SelectedItem == null) { return; } //Load the image Component.LoadBitmap(Filename); //Extract features and recognize ExtractFeatures.ExecuteExtractFeatures(Component, true); RecogniseComponent.Recognise((OCR.ShapeNet)lstNeuralNetworks.SelectedItem, Component); //Fill the imgContainer ExtractFeatures.CreateCompareMatrixWithoutPruning(Component, Component.Bytes); imgTrainingDataOriginal.Bytes = Component.CompareMatrix; for (int index = 0; index < 256; index++) { imgTrainingDataOriginal.GridBrushes[index] = new SolidColorBrush(System.Windows.Media.Color.FromRgb((byte)index, (byte)index, (byte)index)); } imgTrainingDataOriginal.InvalidateVisual(); //Fill the recognition list lstRecognitionResult.Items.Clear(); MostLikelyShape = new RecognitionResult(); MostLikelyShape.Probability = 0; foreach (RecognitionResult Result in Component.RecognitionResults) { if (Result.Probability > MostLikelyShape.Probability) { MostLikelyShape = Result; } } ShapeListEntry Shape = new ShapeListEntry();; //Select the result with the highest probability in the shape list. if (MostLikelyShape.Probability > 0) { ResultString = MostLikelyShape.Content + " (" + MostLikelyShape.Probability + ")"; lstRecognitionResult.Items.Add(ResultString); foreach (Object Item in lstShapes.Items) { Shape = (ShapeListEntry)Item; if (Shape.Shape == MostLikelyShape.Content) { lstShapes.SelectedItem = Item; lstShapes.ScrollIntoView(Item); lstShapes.Focus(); } } } } catch (Exception exp) { Console.WriteLine("Exception caught: " + exp.Message); Console.WriteLine(" In: " + exp.StackTrace); } }
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); } }
private void btnTest_Click(object sender, RoutedEventArgs e) { OCR.ShapeNet SelectedShapeNet; OCR.PageComponent PageComponent; SelectedShapeNet = (OCR.ShapeNet)lstNeuralNetworks.SelectedItem; PageComponent.newID = 0; PageComponent = new OCR.PageComponent(); PageComponent.LoadBitmap(txtSample.Text); ExtractFeatures.ExecuteExtractFeatures(PageComponent, PageComponent, true); RecogniseComponent.Recognise(SelectedShapeNet, PageComponent); //Recognise the component lstResults.Items.Clear(); String ResultText = ""; foreach (RecognitionResult Result in PageComponent.RecognitionResults) { ResultText = Result.Content + " (" + Result.Probability + ")"; lstResults.Items.Add(ResultText); } //Build the original image PageComponent Original = new PageComponent(); Original.LoadBitmap(txtSample.Text); ExtractFeatures.CreateCompareMatrixWithoutPruning(Original, Original.Bytes); imgSample.Bytes = Original.CompareMatrix; for (int index = 0; index < 256; index++) { imgSample.GridBrushes[index] = new SolidColorBrush(System.Windows.Media.Color.FromRgb((byte)index, (byte)index, (byte)index)); } imgSample.InvalidateVisual(); ExtractFeatures.CreateCompareMatrixWithoutPruning(PageComponent); //Build the thinned and stroked image for (int x = 0; x < 32; x++) { for (int y = 0; y < 32; y++) { if (PageComponent.StrokeMatrix[x, y] == 0xFF && PageComponent.CompareMatrix[x, y] != 0xFF) { PageComponent.StrokeMatrix[x, y] = 0x04; } if (PageComponent.PixelTypeMatrix[x, y] == ePixelType.End) { PageComponent.StrokeMatrix[x, y] = 0xFE; } if (PageComponent.PixelTypeMatrix[x, y] == ePixelType.Junction) { PageComponent.StrokeMatrix[x, y] = 0xFD; } } } imgProjection.Bytes = PageComponent.StrokeMatrix; imgProjection.GridBrushes[0] = new SolidColorBrush(Colors.Black); imgProjection.GridBrushes[4] = new SolidColorBrush(Colors.LightGray); imgProjection.GridBrushes[11] = new SolidColorBrush(Colors.Brown); imgProjection.GridBrushes[12] = new SolidColorBrush(Colors.Maroon); imgProjection.GridBrushes[13] = new SolidColorBrush(Colors.Magenta); imgProjection.GridBrushes[14] = new SolidColorBrush(Colors.Lime); imgProjection.GridBrushes[15] = new SolidColorBrush(Colors.LightCyan); imgProjection.GridBrushes[16] = new SolidColorBrush(Colors.Purple); imgProjection.GridBrushes[32] = new SolidColorBrush(Colors.Blue); imgProjection.GridBrushes[48] = new SolidColorBrush(Colors.Green); imgProjection.GridBrushes[64] = new SolidColorBrush(Colors.Yellow); imgProjection.GridBrushes[253] = new SolidColorBrush(Colors.Red); imgProjection.GridBrushes[254] = new SolidColorBrush(Colors.Red); imgProjection.InvalidateVisual(); lblStrokes.Content = "#Strokes: " + Convert.ToString(PageComponent.Strokes); //Bitmap StrokeMatrixBitmap = OCR.DebugTrace.DebugTrace.CreateBitmapFromByteArray(PageComponent.StrokeMatrix, new System.Drawing.Size(32, 32)); //StrokeMatrixBitmap.Save("d:\\ocr\\temp.bmp"); //imgProjection.Source = new BitmapImage(new Uri("d:\\ocr\\temp.bmp")); pbHorizontal.Clear(); int ProjectionValue; for (int x = 0; x < 3; x++) { ProjectionValue = 0; for (int y = 0; y < 3; y++) { ProjectionValue += PageComponent.PixelTypeProjectionJunction[x, y]; ProjectionValue += PageComponent.PixelTypeProjectionEndpoint[x, y]; } pbHorizontal.AddValue(ProjectionValue); } pbVertical.Clear(); for (int y = 0; y < 3; y++) { ProjectionValue = 0; for (int x = 0; x < 3; x++) { ProjectionValue += PageComponent.PixelTypeProjectionJunction[x, y]; ProjectionValue += PageComponent.PixelTypeProjectionEndpoint[x, y]; } pbVertical.AddValue(ProjectionValue); } pbStrokeHorizontal.Clear(); foreach (int Value in PageComponent.lStrokeDirectionX) { pbStrokeHorizontal.AddValue(Value); } pbStrokeVertical.Clear(); foreach (int Value in PageComponent.lStrokeDirectionY) { pbStrokeVertical.AddValue(Value); } }
private void lstFailedExamples_SelectionChanged(object sender, SelectionChangedEventArgs e) { PageComponent PageComponent; String item; OCR.ShapeNet SelectedShapeNet; //Load the image item = (String)lstFailedExamples.SelectedItem; PageComponent = new PageComponent(); PageComponent.LoadBitmap(item); //Recognise the image and fill the result list SelectedShapeNet = (OCR.ShapeNet)lstNeuralNetworks.SelectedItem; ExtractFeatures.ExecuteExtractFeatures(PageComponent, true); RecogniseComponent.RecogniseWithoutConnectedRepair(SelectedShapeNet, PageComponent); lstFailedResult.Items.Clear(); String ResultText = ""; foreach (RecognitionResult Result in PageComponent.RecognitionResults) { ResultText = Result.Content + " (" + Result.Probability + ")"; lstFailedResult.Items.Add(ResultText); } //Build the image PageComponent Original = new PageComponent(); Original.LoadBitmap(item); PageComponent OriginalWithPruning = new PageComponent(); OriginalWithPruning.LoadBitmap(item); ExtractFeatures.CreateCompareMatrixWithoutPruning(Original); ExtractFeatures.CreateCompareMatrix(OriginalWithPruning); for (int x = 0; x < 32; x++) { for (int y = 0; y < 32; y++) { PageComponent.CompareMatrix[x, y] = Original.CompareMatrix[x, y]; if (Original.CompareMatrix[x, y] != 0xFF && PageComponent.CompareMatrix[x, y] == 0xFF) { PageComponent.CompareMatrix[x, y] = 4; } if (PageComponent.StrokeMatrix[x, y] != 0xFF) { PageComponent.CompareMatrix[x, y] = PageComponent.StrokeMatrix[x, y]; } } } imgFailed.Bytes = PageComponent.CompareMatrix; imgFailed.GridBrushes[0] = new SolidColorBrush(Colors.Gray); imgFailed.GridBrushes[4] = new SolidColorBrush(Colors.LightGray); imgFailed.GridBrushes[11] = new SolidColorBrush(Colors.Brown); imgFailed.GridBrushes[12] = new SolidColorBrush(Colors.Maroon); imgFailed.GridBrushes[13] = new SolidColorBrush(Colors.Magenta); imgFailed.GridBrushes[14] = new SolidColorBrush(Colors.Lime); imgFailed.GridBrushes[15] = new SolidColorBrush(Colors.LightCyan); imgFailed.GridBrushes[16] = new SolidColorBrush(Colors.Purple); imgFailed.GridBrushes[32] = new SolidColorBrush(Colors.Blue); imgFailed.GridBrushes[48] = new SolidColorBrush(Colors.Green); imgFailed.GridBrushes[64] = new SolidColorBrush(Colors.Yellow); imgFailed.GridBrushes[253] = new SolidColorBrush(Colors.Red); imgFailed.GridBrushes[254] = new SolidColorBrush(Colors.Red); imgFailed.GridBrushes[255] = new SolidColorBrush(Colors.White); imgFailed.InvalidateVisual(); //imgFailed.Bytes = PageComponent.StrokeMatrix; //imgFailed.InvalidateVisual(); }