Exemple #1
0
        public void OpenPicture(string file)
        {
            if (!File.Exists(file))
            {
                MessageService.ShowError(null, string.Format("Cannot open file : File {0} doesn't exist", file));
                return;
            }

            Original = new BitmapImage(new Uri(file));

            VerticalProjection = new Projection(Original)
            {
                Type = ProjectionType.Vertical
            };
            VerticalProjection.Compute();

            HorizontalProjection = new Projection(Original)
            {
                Type = ProjectionType.Horizontal
            };
            HorizontalProjection.Compute();

            CharacterIsolation   = new CharacterIsolation(Original);
            CharacterRecognition = new CharacterRecognition(Original);

            Correction = new Correction(Original, new WordTree(new HammingDistance(false),
                                                               new HammingDistance('?', false), new FrequencyResolver()));
        }
Exemple #2
0
        private void OnRecognizeText(object parameter)
        {
            if (!CanRecognizeText(parameter))
            {
                return;
            }

            int i           = 0;
            var textBuilder = new StringBuilder();

            foreach (var word in CharacterIsolation.Words)
            {
                if (word.LineIndex != i)
                {
                    i = word.LineIndex;
                    textBuilder.Append("\n");
                    Correction.Text = textBuilder.ToString();
                }

                foreach (var character in word.Characters)
                {
                    CharacterRecognition.CharacterZone = character;
                    CharacterRecognition.Compute();
                    textBuilder.Append(CharacterRecognition.RecognizedCharacter);
                }

                textBuilder.Append(" ");
            }

            Correction.Text = textBuilder.ToString();
            CorrectTextCommand.RaiseCanExecuteChanged();
        }
Exemple #3
0
        private void OnRecognize(object parameter)
        {
            if (parameter == null || !CanRecognize(parameter))
            {
                return;
            }

            var zone = (Int32Rect)parameter;

            CharacterRecognition.CharacterZone = zone;
            CharacterRecognition.Compute();
        }
Exemple #4
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            if (!this._memoryCache.TryGetValue <string>(_cacheKey, out string value))
            {
                value = CharacterRecognition.GetAccessToken();

                MemoryCacheEntryOptions cacheOptions = new MemoryCacheEntryOptions();
                cacheOptions.AbsoluteExpiration = DateTime.Now.AddDays(1);
                this._memoryCache.Set <string>(_cacheKey, value, cacheOptions);
            }
            var text = CharacterRecognition.GetContent(value);

            return(View("Index", text));
        }
Exemple #5
0
    public static void Can_Parse_Characters()
    {
        // Arrange
        string[] letters =
        {
            ".**..***...**..****.****..**..*..*..***...**.*..*.*.....**..***..***..*..*.*...*****.",
            "*..*.*..*.*..*.*....*....*..*.*..*...*.....*.*.*..*....*..*.*..*.*..*.*..*.*...*...*.",
            "*..*.***..*....***..***..*....****...*.....*.**...*....*..*.*..*.*..*.*..*..*.*...*..",
            "****.*..*.*....*....*....*.**.*..*...*.....*.*.*..*....*..*.***..***..*..*...*...*...",
            "*..*.*..*.*..*.*....*....*..*.*..*...*..*..*.*.*..*....*..*.*....*.*..*..*...*..*....",
            "*..*.***...**..****.*.....***.*..*..***..**..*..*.****..**..*....*..*..**....*..****.",
        };

        // Act
        string actual = CharacterRecognition.Read(string.Join(Environment.NewLine, letters));

        // Assert
        actual.ShouldBe("ABCEFGHIJKLOPRUYZ");
    }
Exemple #6
0
    /// <summary>
    /// Gets the number of pixels lit in the specified grid after following the specified instructions.
    /// </summary>
    /// <param name="instructions">The instructions to use to manipulate the grid of lights.</param>
    /// <param name="width">The width of the grid.</param>
    /// <param name="height">The height of the grid.</param>
    /// <param name="logger">The logger to use.</param>
    /// <returns>
    /// The number of pixels lit in the grid once the instructions are processed,
    /// the code displayed, and a visualization.
    /// </returns>
    internal static (int PixelsLit, string Code, string Visualization) GetPixelsLit(
        IEnumerable <string> instructions,
        int width,
        int height,
        ILogger logger)
    {
        var operations = instructions
                         .Select((p) => ParseInstruction(p))
                         .ToArray();

        bool[,] grid = new bool[width, height];

        foreach (Instruction instruction in operations)
        {
            if (instruction.IsRotation)
            {
                if (instruction.IsColumn)
                {
                    RotateColumn(grid, column: instruction.A, pixels: instruction.B);
                }
                else
                {
                    RotateRow(grid, row: instruction.A, pixels: instruction.B);
                }
            }
            else
            {
                LightRectangle(grid, width: instruction.A, height: instruction.B);
            }
        }

        int pixelsLit = CountLitPixels(grid);

        string code          = CharacterRecognition.Read(grid);
        string visualization = logger.WriteGrid(grid, ' ', 'X');

        return(pixelsLit, code, visualization);
    }