Ejemplo n.º 1
0
        private List <float> GetDatasetValuesFromImage(string base64Image)
        {
            base64Image = base64Image.Replace("data:image/png;base64,", "");
            var imageBytes = Convert.FromBase64String(base64Image).ToArray();

            Image image;

            using (var stream = new MemoryStream(imageBytes))
            {
                image = Image.FromStream(stream);
            }

            var res = new Bitmap(SizeOfImage, SizeOfImage);

            using (var g = Graphics.FromImage(res))
            {
                g.Clear(Color.White);
                g.DrawImage(image, 0, 0, SizeOfImage, SizeOfImage);
            }

            var datasetValue = handwritingRecognition.GetDatasetValues(res, "ffffffff");

            return(datasetValue);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            var fontsList = new List <string>()
            {
                "Arial", "Arial Narrow", "Calibri", "Times New Roman", "Calibri Light"
            };
            var fontStyles = new List <FontStyle>()
            {
                FontStyle.Bold
            };
            var pathDataset      = @"../../../../MulticlassClassificationML.ConsoleApp/dataset.csv";
            var drawString       = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            var backgroundColor  = "ffffffff";
            var saveFiles        = false;
            var filesCatalogName = "images";

            var tw = new StreamWriter(pathDataset);

            tw.WriteLine("PixelValues,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,Number");

            if (saveFiles)
            {
                var dn = new DirectoryInfo(filesCatalogName);
                if (!dn.Exists)
                {
                    dn.Create();
                }
            }

            foreach (var c in drawString)
            {
                foreach (var fontName in fontsList)
                {
                    for (int size = 10; size < 26; size++)
                    {
                        foreach (var fontStyle in fontStyles)
                        {
                            for (int angle = -13; angle < 13; angle++)
                            {
                                var res = new Bitmap(32, 32);

                                // Create font and brush.
                                Font drawFont  = new Font(fontName, size, fontStyle);
                                var  drawBrush = new SolidBrush(Color.Black);

                                // Create point for upper-left corner of drawing.
                                float x = 1;
                                float y = 1;

                                // Set format of string.
                                StringFormat drawFormat = new StringFormat();

                                using (var g = Graphics.FromImage(res))
                                {
                                    g.Clear(Color.White);
                                    g.RotateTransform(angle); // set up rotate
                                    g.DrawString(c.ToString(), drawFont, drawBrush, x, y, drawFormat);
                                }
                                if (saveFiles)
                                {
                                    var path = $"{filesCatalogName}\\{c}_{Guid.NewGuid()}.png";
                                    res.Save(path);
                                }

                                var handwritingRecognition = new HandwritingRecognition();

                                var datasetValue = handwritingRecognition.GetDatasetValues(res, backgroundColor);

                                tw.WriteLine($"{string.Join(",", datasetValue)},{c - 'A'}");
                            }
                        }
                    }
                }
            }
            tw.Close();
        }