Wednesday, January 27, 2016

Microsoft OCR (Optical Character Recognization) library for Windows Runtime Apps

Microsoft recently announced that their OCR Library for Windows Runtime has been released as a NuGet package. This library enables you to add text reading capabilities to your Windows Phone 8/8.1 and Windows 8.1 Store apps.

A superb example of this great library can be seen in the Microsoft Translator apps where images are OCRd and the extracted text is then translated to the selected language.



I tested it with a newspaper in English and since I know Hindi I can say that this works perfectly !
Isn't that awesome !

Supported Languages
There are 21 supported languages. Based on recognition accuracy and performance, supported languages are divided into three groups:
  • Excellent: Czech, Danish, Dutch, English, Finnish, French, German, Hungarian, Italian, Norwegian, Polish, Portuguese, Spanish and Swedish.
  • Very good: Chinese Simplified, Greek, Japanese, Russian and Turkish.
  • Good: Chinese Traditional and Korean.

OCR Limitations
  • Image dimension should be >= 40*40 pixels and <= 2600*2600 pixels


  • Image text lines must have written in same orientations and same directions.Fortunately OCR can able to correct rotation up to ±40 degrees.


An inaccurate reading may be caused by the following:
  • Blurry images
  • Handwritten or cursive text
  • Artistic font styles
  • Small text size (less than 15 pixels for Western languages, or less than 20 pixels for East Asian languages)
  • Complex backgrounds
  • Shadows or glare over text
  • Perspective distortion
  • Oversized or dropped capital letters at the beginnings of words
  • Subscript, superscript, or strikethrough text
So lets get started to build a basic app that uses the OCR Library. 1.Download and install the OCR Library Go to manage Nuget Packages and search for "Microsoft.Windows.Ocr" 2.Change the solution platform The solution platform is by default "AnyCPU". Change it to "ARM" or "x86". Go to Build -> Configuration Manager and change the active solution platform. 3.Write a function to select an image Obtain a reference of the FileOpenPicker class and call the PickSingleFileAndContinue() method which handles the selection of a single file.
Code:
private void PickImage_Click(object sender, RoutedEventArgs e)
{
    FileOpenPicker openPicker = new FileOpenPicker();
    openPicker.ViewMode = PickerViewMode.Thumbnail;
    openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
    openPicker.FileTypeFilter.Add(".jpg");
    openPicker.FileTypeFilter.Add(".jpeg");
    openPicker.FileTypeFilter.Add(".png");
    openPicker.PickSingleFileAndContinue();
}
4.Display the recognized text
Code:
public async void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
{
    displayText.Text = string.Empty;
    if (args.Files.Count > 0)
    {
        StorageFile file = args.Files[0];
        ImageProperties imgProp = await file.Properties.GetImagePropertiesAsync();
        using (var stream = await file.OpenAsync(FileAccessMode.Read))
        {
            WriteableBitmap bitmap = new WriteableBitmap((int)imgProp.Width, (int)imgProp.Height);
            bitmap.SetSource(stream);
            DisplayImage.Source = bitmap;
            // Extract text from image.
            OcrResult result = await ocrEngine.RecognizeAsync(imgProp.Height, imgProp.Width, bitmap.PixelBuffer.ToArray());
            // Check whether text is detected.
            if (result.Lines != null)
            {
                // Collect recognized text.
                string recognizedText = "";
                foreach (var line in result.Lines)
                {
                    foreach (var word in line.Words)
                    {
                        recognizedText += word.Text + " ";
                    }
                    recognizedText += Environment.NewLine;
                }
                // Display recognized text.
                displayText.Text = recognizedText;
            }
        }
    }
}
Download the source code Happy Coding !

2 comments :