simple OCR and photo capture (from a webcam) in C#

After being recently assigned a task to perform OCR on text within a photograph I decided to try and find the most simplistic solution to this.  The last thing I wanted to do was write my own wrapper for a library, I was pressed for time.  All my code was produced in Microsoft Visual Studio 2008, I didn’t test these lib’s in 2010.

Capturing an image from a webcam

There were a few routes to go down to enable capturing a photograph from a webcam.  Most websites and blogs I found concentrated on WIA or TWAIN, both these options are limited as not all webcam’s are compatible with these standards. This would of caused an issue.

Emgu CV was my chosen library of choice, the website includes instructions for downloading,installing and including into your project if your struggling (note OpenCV is a requirement.

using System.Drawing;
using Emgu.CV;
using Emgu.CV.Structure;

namespace ImageOCRTestApplication
{
    class ImageCapture
    {
        private Capture capture;
        public ImageCapture()
        {
            capture = new Capture();
        }

        //performs photo capture and returns the image as a bitmap
        public Bitmap GetImage()
        {
            Image frame = capture.QueryFrame(); 
            return frame.ToBitmap();
        }             
    }
}

The above GetImage() returns an image in Bitmap format. Now onto the OCR.

OCR!

There are many examples of this online tessnet2 is a popular wrapper for tesseract , but it has its issues, including a nasty memory leak.  So I decided to go with the easiest possible solution. MODI, the only issue with MODI is you do need to have the library that comes with Office, never fear though there is a solution (http://support.microsoft.com/kb/982760) to downloading and installing MODI for free!

Once  you have downloaded, installed then include ‘Microsoft Office Document Imaging 12.0 Type Library’ into your references.

using System.Drawing;

namespace ImageOCRTestApplication
{
    class OCR
    {
        private string textToFind;

        public bool DoesTextExist(string text, Bitmap photo)
        {
            this.textToFind = text;
            return this.performOCR(photo);
        }
        //The file has to be temp saved for OCR. Wont accept .bmp ext
        private string SaveImage(Bitmap image)
        {
            image.Save("C:\\" + textToFind+ ".jpg");
            return "C:\\" + textToFind + ".jpg";
        }
        //Check if the image contains the text we are looking for
        private bool performOCR(Bitmap image)
        {
            MODI.Document document = new MODI.Document();
            document.Create(SaveImage(image));
            document.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH,true,true);
            MODI.Image ocrImage = (MODI.Image) document.Images[0];

            return ocrImage.Layout.Text.Contains(textToFind);
        }

    }
}