Beispiel #1
0
        override public char Recognize(CharacterImage image)
        {
            inputs = image.NormalizedPixels;

            double[] hiddenLayerOutputs = new double[hiddenLayer.Length];
            for (int i = 0; i < hiddenLayer.Length; i++)
            {
                hiddenLayerOutputs[i] = hiddenLayer[i].CalculateOutput(inputs);
            }

            double[] outputLayerOutputs = new double[outputLayer.Length];
            for (int i = 0; i < outputLayer.Length; i++)
            {
                outputLayerOutputs[i] = outputLayer[i].CalculateOutput(hiddenLayerOutputs);
            }

            //Find most probable output.
            double max = outputLayerOutputs.Max();
            int    mostProbableIndex = Array.IndexOf(outputLayerOutputs, max);

            //Return guessed digit or letter.
            if (mostProbableIndex < 10)
            {
                return((char)('0' + mostProbableIndex));
            }
            else
            {
                return((char)('A' + mostProbableIndex - 10));
            }
        }
Beispiel #2
0
        private void recognize(CharacterImage image)
        {
            if (image == null)
            {
                textBoxPresetRecognition.Text = "Cannot read character image.";
                textBoxCustomRecognition.Text = "Cannot read character image.";
                return;
            }

            //Recognize in PresetNeuralNetwork
            if (PresetNeuralNetwork != null)
            {
                textBoxPresetRecognition.Text = "Correct label: " + image.Label + "\r\n" + "Recognized label: " + PresetNeuralNetwork.Recognize(image);
            }
            else
            {
                textBoxPresetRecognition.Text = "Preset Neural Network not created.";
            }

            //Recognize in CustomNeuralNetwork
            if (CustomNeuralNetwork != null)
            {
                textBoxCustomRecognition.Text = "Correct label: " + image.Label + "\r\n" + "Recognized label: " + CustomNeuralNetwork.Recognize(image);
            }
            else
            {
                textBoxCustomRecognition.Text = "Custom Neural Network not created.";
            }
        }
Beispiel #3
0
        private void selectCharacterImage(CharacterImage image)
        {
            selectedCharacterImage = image;
            //Recognize Image
            recognize(image);

            if (image != null)
            {
                //Display info
                textBoxSelectedCharacterInfo.Text =
                    "FontFamily: " + image.FontFamily + "\r\n" +
                    "FontVariant: " + image.FontVariant + "\r\n" +
                    "Label: " + image.Label + "\r\n" +
                    "Italic: " + image.Italic + "\r\n" +
                    "Strength: " + image.Strength + "\r\n";
                //Display Image
                Bitmap bitmap = new Bitmap(image.Width, image.Height);
                for (int y = 0; y < image.Height; y++)
                {
                    for (int x = 0; x < image.Width; x++)
                    {
                        int grayscaleValue = image.Pixels[y * image.Width + x];
                        bitmap.SetPixel(x, y, Color.FromArgb(255, grayscaleValue, grayscaleValue, grayscaleValue));
                    }
                }
                pictureBox.Image = resizeBitmap(bitmap, image.Width * 8, image.Height * 8);
            }
            else
            {
                textBoxSelectedCharacterInfo.Text = "Cannot read character image.";
                pictureBox.Image = null;
            }
        }
Beispiel #4
0
        public void Add(CharacterImage image)
        {
            Images.Add(image);

            if (ImagesByCharacter.TryGetValue(image.Label, out var listByCharacter))
            {
                listByCharacter.Add(image);
            }
            else
            {
                ImagesByCharacter[image.Label] = new List <CharacterImage>()
                {
                    image
                }
            };

            if (ImagesByFontFamily.TryGetValue(image.FontFamily, out var listByFont))
            {
                listByFont.Add(image);
            }
            else
            {
                ImagesByFontFamily[image.FontFamily] = new List <CharacterImage>()
                {
                    image
                }
            };

            if (ImagesByFontFamilyAndCharacter.TryGetValue(image.FontFamily, out var dictionaryByCharacter))
            {
                //Dictionary for this fontFamily exists.
                if (dictionaryByCharacter.TryGetValue(image.Label, out var listByFontFamilyAndCharacter))
                {
                    listByFontFamilyAndCharacter.Add(image);
                }
                else
                {
                    dictionaryByCharacter[image.Label] = new List <CharacterImage>()
                    {
                        image
                    }
                };
            }
            else
            {
                //Dictionary for this fontFamily does not exist yet.
                ImagesByFontFamilyAndCharacter[image.FontFamily] = new SortedDictionary <char, List <CharacterImage> >();
                ImagesByFontFamilyAndCharacter[image.FontFamily].Add(image.Label, new List <CharacterImage>()
                {
                    image
                });
            }
        }
Beispiel #5
0
        private void buttonLoadImageFromFile_Click(object sender, EventArgs e)
        {
            var openFileDialog = new OpenFileDialog();

            openFileDialog.Title  = "Please select a .bmp image to open.";
            openFileDialog.Filter = "BMP|*.bmp";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string         fileName = openFileDialog.FileName;
                CharacterImage img      = DataLoader.LoadFromBMP(fileName);
                selectCharacterImage(img);
            }
        }
Beispiel #6
0
        public static DataSet LoadDataSet(string folderPath)
        {
            DataSet dataSet = new DataSet();

            if (!Directory.Exists(folderPath))
            {
                return(dataSet);
            }
            foreach (string fileName in Directory.EnumerateFiles(folderPath, "*.csv"))
            {
                using (var reader = new StreamReader(fileName))
                {
                    //Skip first line of csv.
                    reader.ReadLine();
                    //Read the rest of csv, create image for each line and add to images list.
                    while (!reader.EndOfStream)
                    {
                        //Read one line with values for one image.
                        var values = reader.ReadLine().Split(',');
                        //Create images only for letters and digits.
                        char label = Convert.ToChar(int.Parse(values[2]));
                        if (!(label >= '0' && label <= '9' || label >= 'A' && label <= 'Z'))
                        {
                            continue;
                        }
                        //For this dataset width and height are both 20.
                        int width  = 20;
                        int height = 20;
                        //Fill pixel array.
                        int[] pixels = new int[width * height];
                        for (int i = 0; i < width * height; i++)
                        {
                            pixels[i] = int.Parse(values[12 + i]);
                        }
                        //Read information specific to this dataset.
                        string         fontFamily  = values[0];
                        string         fontVariant = values[1];
                        float          strength    = float.Parse(values[3], System.Globalization.CultureInfo.InvariantCulture);
                        bool           italic      = values[4] == "1";
                        CharacterImage newImage    = new CharacterImage(20, 20, label, pixels, fontFamily, fontVariant, italic, strength);
                        dataSet.Add(newImage);
                    }
                }
            }
            return(dataSet);
        }
Beispiel #7
0
        public static CharacterImage LoadFromBMP(string filePath)
        {
            CharacterImage characterImage = null;

            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(filePath);
            if (bmp != null)
            {
                int[] pixels = new int[20 * 20];
                for (int y = 0; y < 20; y++)
                {
                    for (int x = 0; x < 20; x++)
                    {
                        System.Drawing.Color pixelColor = bmp.GetPixel(x, y);
                        int grayscaleValue = (pixelColor.R + pixelColor.G + pixelColor.B) / 3;
                        pixels[y * 20 + x] = grayscaleValue;
                    }
                }
                characterImage = new CharacterImage(20, 20, Path.GetFileName(filePath)[0], pixels);
                bmp.Dispose();
            }
            return(characterImage);
        }
Beispiel #8
0
 public abstract char Recognize(CharacterImage image);