Get Contact Info from Business Card with OCR: 25 Projects in 25 Days

As part of the LEAD Technologies 25th anniversary, we are creating 25 projects in 25 days to celebrate LEAD’s depth of features and ease of use. Today’s project comes from Hadi.

If you’ve been keeping track, this is actually our 25th project and the last one in our #LEAD25 series. But don’t be sad, we post examples like these to our blog and forums on a regular basis so keep in touch! If you missed any of them, take a look back at our series introduction where we’ve kept a running list as each project was posted.

What it Does

This ASP.NET C# application recognizes zones of text from business cards using LEADTOOLS Version 19.

Features Used

Development Progress Journal

Hello, my name is Hadi I am writing a sample application that will allow users to upload an image of a business card and then let them select zones to be recognized using OCR for uploading the text as a contact.

I am using an ASP.NET application so that I can combine server-side C# code in the code behind and HTML5 and JavaScript on the client side. I will use the LEADTOOLS JavaScript ImageViewer and Annotation SDK in order to display the zones the user wants to recognize from the business card and on the server side I will use the .NET Annotations and OCR SDK.

To start I will need to create the aspx Default page for the user interface. I want the user to be able to upload an image, display the image, and then allow them to add/remove annotation rectangles to depict the desired zones.

On the server side, I want to be able to take the uploaded file and use the LEADTOOLS OCR AutoZone method in order to have some premade zones available for the user to manipulate on the front end. I also need to be sure that the file uploaded is a valid MIME type for display in the browser and if not convert it to one.

The AutoZone method gave me the zones and I used the LEADTOOLS Annotations to save an XML file that contains the zone bounds. This will let me easily display the zones to the user on the front end. I need to add a function in the JavaScript that will load the image and the XML zone file for displaying. I need to call the JavaScript from the server, so I used the ClientScriptManager.RegisterStartupScript method from the System.Web.Ui namespace.

Now that I have the XML filename in the JavaScript, I need to load it with AnnCodecs from the LEADTOOLS.Annotations.Core.JS namespace. I am using the XMLHttpRequest open and send methods to open the XML file and process the results in the onreadystatechange event. To load the image, I just set the ImageViewer.ImageUrl property to the file of the image I passed to the function. It was very easy to load the image with the LEADTOOLS ImageViewer:

Documentation: ImageViewer

Now that most of the functionality is implemented, I am going to add additional features that will allow the user to manipulate the zones. I want the user to be able to set the name of the zone so that they can know what the field belongs to. For this I am using the AnnObject.AnnLabel property:

Documentation: AnnLabel

A couple other features I have added allow the user to delete selected zones or clear all the zones. This is easily achievable using the AnnAutomation.DeleteObject and DeleteObjects methods:

Documentation: AnnAutomation

Now that the additional functionalities are implemented, the final step is to recognize the new zones. Since the project already had to load the image and use the OcrEngine to AutoZone, I kept those in memory on the server so that everything is ready. I just need to pass the new XML data back to the server using PageMethods and the .NET AnnCodecs to load the new XML and get the zones out of it. Once I update the zones, I call the OcrDocument.Recognize method and then alert the user with the recognized text.

Overall this app was fun to write because it taught me a lot about client and server interaction. It was made much simpler by using the LEADTOOLS libraries since any one aspect of it (Annotations, OCR, Viewer) would have been extremely hard to implement without it. If I had more time I would look into porting the recognized text into an actual Outlook or Google contact.

Download the Project

The source code for this sample project can be downloaded from here.

Posted in OCR | Tagged , , , , , , , , , | 4 Comments

Linux OCR, Barcode and Format Conversion Batch Processor: 25 Projects in 25 Days

As part of the LEAD Technologies 25th anniversary, we are creating 25 projects in 25 days to celebrate LEAD’s depth of features and ease of use. Today’s project comes from Nathan.

Download the Project

What it Does

This C project will perform OCR, barcode recognition and file conversion using LEADTOOLS Version 19.

Features Used

Development Progress Journal

Hello, my name is Nathan and I am going to write a Linux application that combines OCR, barcode recognition, and file conversion into one batch processing application. It’s been quite a while since I’ve written a program in C so this should be fun!

Since I already have the LEADTOOLS SDK installed, I’m going to start with something simple to get my C gears turning. I’ll start by doing input verification, as I want to make sure the user uses the application properly and print out how to use it if they don’t.

That took a couple hours and I think I have input down pat. I’m going to store all of the options in a struct and then call functions that will do all the LEADTOOLS stuff from a header file if the flags are passed.

I’m going to go ahead and write a makefile that links all the libraries we’re going to need in this program so I don’t have to fiddle with compiling anymore and just type “make.”

Now I’m going to write a function that does file conversion. It’ll take a char * for source and target directories, intended format and then a struct to communicate the type with LEADTOOLS.

That only took about 45 minutes! I ran into some issues when opening directories, but it only took about 35 lines of code. That includes all the code we need to convert every file in our source directory, which is pretty amazing for C.

Now that I have that working, I’m going to write a function for barcode recognition.

That took about an hour and a half. Barcode recognition was a little trickier because I needed a couple helper functions to call from within. Even still, it wasn’t too bad and now my program can handle any barcode and it will write the data to a text file. And I can now do file conversion and barcode recognition from all the files by just passing both flags as command line arguments, that’s pretty sweet.

Now last but not least, I need to do OCR, which is a really complex thing to do!

That took about 3 hours since I’m not the most C-savvy person. Now I can OCR any image from the directory and output the text to a .txt file.

I need to do some code cleaning and commenting but this should only take about 20 minutes.

That wraps things up and brings me in at less than 8 hours. In one work day I was able to write a batch processing application that can do OCR, barcode recognition and file conversion, all in one. This is extremely useful for Linux users! Write a script to run this application and you can automate a lot of work.

In a future release, I’d probably like to separate some of my code into functions, do some multi-threading for performance, and allow for the int versions of the format constants or the common terms (tif, jpg, png, etc..) to be used.

Posted in Document Imaging | Tagged , , , , , , , | Leave a comment

Scan to PDF Console: 25 Projects in 25 Days

As part of the LEAD Technologies 25th anniversary, we are creating 25 projects in 25 days to celebrate LEAD’s depth of features and ease of use. Today’s project comes from Faris.

What it Does

This C# console application will scan a page, extract text with OCR and save to PDF using LEADTOOLS Version 19.

Features Used

Development Progress Journal

Hello, my name is Faris and I’m going to create a console application which will scan a page from a scanner using TWAIN, recognize the text in the scanned page, and finally, save the recognized page as searchable PDF.

In this project, I will use LEADTOOLS Recognition Imaging SDK and program using .NET C# programming language.

The LEADTOOLS Recognition Imaging SDK provides me with the ability to scan from a TWAIN scanner, recognize the text in images using Optical Character Recognition (OCR) and save recognized images to document formats (PDF, DOCX, TXT).

It should be noted this application will recognize printed text in English alphabets, but not hand-written text. This can be customized to suit the programmer’s needs as our LEADTOOLS OCR engines support multiple languages and the Professional engine supports Intelligent Character Recognition (ICR).

First, I will start with a new Console Application in Visual Studio 2010. I will also add the needed SDK references for TWAIN, OCR and document writing functionalities.

I have now added the necessary code for starting up the TWAIN session, enumerating the scanners connected to the device and acquiring the image from the scanner. This took about 30 minutes of work.

Next came recognizing the acquired image. The OCR engine must first be started, then the text in the image should be placed inside zones, and then the image will be ready for recognition. For zoning the image, I used the `AutoZone` method as I want to recognize the whole page, not a specific part of it. I found a really useful code example when searching LEADTOOLS online help documentation for the `AutoZone` method:

Documentation: IOcrPage

I have now finished adding the code for recognizing the scanned image. This took about an hour.

The next step is to save the recognized document as a searchable PDF. Luckily, the example I referenced earlier shows the code to achieve that. All I need to do is add the code for saving the recognized document in such a way to work with the previous step. The programmer can modify this part of the project to prompt the user to specify which file format the user wishes to save the output file as and also specify the output file’s name.

This part is now done and took about 30 minutes.

Finally, I’ll work on error handling and testing the code. I will test the application when 2 scanners are connected and when one scanner is connected. Because this is a console application, I will add multiple lines of code to write on the console the current operation being done to make the application informative for the user.

The final part is now done with about 2 hours of work. The total work time on the project, including developing, debugging and testing the program, was about 4 hours. Without the help of LEADTOOLS SDK, I wouldn’t have been able to finish the core functionality code within 2 hours, thus finishing the whole application in less than a single day’s work.

Download the Project

The source code for this sample project can be downloaded from here. To run the project, extract it to the C:/LEADTOOLS 19/Examples/DotNet/CS directory.

Posted in Document Imaging, OCR | Tagged , , , , , , , | Leave a comment

Custom Annotations: 25 Projects in 25 Days

CustomAnnotations Screenshot
CustomAnnotations Screenshot

As part of the LEAD Technologies 25th anniversary, we are creating 25 projects in 25 days to celebrate LEAD’s depth of features and ease of use. Today’s project comes from Hadi.

What it Does

This C# application will create several custom annotations using LEADTOOLS Version 19.

Features Used

Development Progress Journal

Hello, my name is Hadi and I am writing a sample application that will demonstrate the various capabilities of the LEADTOOLS Annotation SDK, more specifically, how to create Custom Annotations and Designers.

My plan is to start off by writing a simple project that uses the default Annotations and ImageViewer so that I have a base to start with. The WinForms Annotations demo located in the \Examples\DotNet\CS\AnnotationsDemo directory will work perfectly.

Now that I have the sample project created, I will start with my first custom object, the AnnCircleObject. I want this custom object to be very similar to the AnnEllipseObject but instead of an oval, it will be a perfect circle.

To get started, I created new directories to keep things organized and will start by creating the new object which is based on the Ellipse.

Next, I want to control how the object is drawn, so I will need to create my own custom Draw Designer based on the AnnEllipseDrawDesigner. I will override the OnPointerDown and OnPointerMove events so that I can control the ellipse and force it into a perfect circle.

In the OnPointerDown event I set the begin and end points, and in the OnPointerMove event I do some math to get the width and height of the drawing rectangle so I can force the circle to be the same width and height, therefore creating a perfect circle.

I also don’t want the new AnnCircleObject to be modified and changed to an oval or ellipse, so I have created a custom ObjectRenderer based on the AnnEllipseObjectRenderer and overrode the RenderThumbs event to not render the top, left, right and bottom resize thumbs so the user can’t modify the height or width individually.

Next I will create a text-based custom object, the AnnNumberObject. This object will be a custom text-based Annotation object that will, when drawn, count and display the number of other AnnNumberObjects in the same AnnContainer.

I based the AnnNumberObject on the AnnTextObject and its Draw Designer on the AnnRectangleDrawDesigner. In the Draw Designer I overrode the OnPointerDown event so that it will cycle through each object in the AnnContainer and increment a counter by 1 for each object it finds, setting the Text property of the NumberObject to that value.

For the NumberObject, I also had to create a custom Edit Designer which I based on the AnnTextEditDesigner so that I can have the font size change as the size of the object changes. To do this, I overrode the OnPointerDown event to capture the starting values for the NumberObject‘s bounds and font size, then I overrode the OnEdit event to do some math to get a ratio to see how the width was being edited and forces the font size to change with it.

The third object I created was the AnnStarObject based on the AnnPolylineObject. This object will let the user click and drag their mouse to set the center and radius of a perfect gold star. To achieve this, I created a custom Draw Designer based on the AnnPolylineDrawDesigner. I overrode the OnPointerDown to set the center of the star then I overrode the OnPointerMove event and did the math necessary to create the perfect star. I calculated the distance between the center point and the current location of the mouse to generate the radius, then used a formula to find the 10 points of the star and add them to the object.

The final object added was the AnnTinyStampObject which is based on the AnnStampObject. This object is very similar to the StampObject except I have it draw the stamp on a single click. To achieve this, I created a custom Draw Designer which isn’t based on another object’s draw designer. I overrode the OnPointerDown event and calculated the 4 points of the rectangle based on the mouse location and added those to the tiny stamp object and ended the designer in the OnPointerUp event.

After all my objects were created, I had to add them to my AnnAutomationManager.AutomationObject list so that the user can access and use them. I created a method to handle adding the objects, as well as setting the ToolbarImage to make it easy for the user to find the object in the toolbar.

Overall this project took me less than a day to write since I had the AnnotationDemo to get started with and a helpful tutorial within the LEADTOOLS documentation about implementing custom annotations located here:

Documentation: Implementing User-Defined Objects With LEADTOOLS Annotations in Windows Forms

Download the Project

The source code for this sample project can be downloaded from here. To run the project, extract it to the C:\LEADTOOLS 19\Examples\DotNet\CS directory.

Posted in Document Imaging | Tagged , , | Leave a comment