OCR (C#) : IRIS (iDRS 14.0) Part 2 - Writing some code
In
part 1 we had a look at how to get the OCR "engine" up and running, in this post we're going have a look at how
to use the SDK itself.
Under the samples folder of the SDK supplied by IRIS you'll find some C++/C# and VB.net examples, but I feel these
samples are too bulky, so in this post I am going to show you in just a few lines of code the basics of using this SDK.
Observe the following snippet:
using IDRSNET;
static string ImageToText(String imagePath)
{
using (OCRContext context = new OCRContext(Languages.English))
using (Reader reader = new Reader())
{
reader.SetOCRParameters(context);
reader.SetOrientationDetectionParameters(true, true);
using (Page page = new Page())
{
using (Doc doc = new Doc())
{
page.LoadSourceImage(imagePath);
reader.ReadFmt(page);
DocPage docPage = doc.ConvertPage(page);
return docPage.GetText();
}
}
}
}
Nothing much to explain here, we pass a language to the OCRContext, which we pass to our Reader
class, set a few parameters and pass an image to the page class.
You will notice however that the GetText method on the DocPage class doesn't actually exist, I
added this as extension method to simplify retrieving text from a DocPage object, this is what
the method looks like:
static class Extensions
{
public static String GetText(this DocPage docPage)
{
using (StringWriter sw = new StringWriter())
{
foreach (DocZone zone in docPage.Zones)
{
foreach (DocLine line in zone.Lines)
foreach (DocWord word in line.Words)
{
foreach (DocBlob blob in word.Blobs)
sw.Write(blob.Text);
sw.Write(" ");
}
sw.WriteLine();
}
return sw.ToString();
}
}
}
In the following snippet we pass images to the ImageToText method and save the output
to a text file, notice the IDRSLicenses class:
static void Main(string[] args)
{
using (IDRSLicenses licenses = new IDRSLicenses())
{
FileInfo[] files = new DirectoryInfo(@"c:\ocr\temp").GetFiles("*.jpg");
using (StreamWriter sw = new StreamWriter(@"c:\ocr\text.txt"))
{
foreach (FileInfo file in files)
{
string content = ImageToText(file.FullName);
sw.WriteLine(content);
}
}
}
}
The IDRSLicenses class is a class I wrote to simplify the licensing needed to use these classes, observe:
class IDRSLicenses : IDisposable
{
LicenseDRS _licenseDRS = new LicenseDRS();
LicensePreprocessing _licensePrepro = new LicensePreprocessing();
LicenseFormatting _licenseFmt = new LicenseFormatting();
LicenseImageFile _licenseImageFile = new LicenseImageFile();
public IDRSLicenses()
{
SetupKeys();
SetupModules();
}
// Retrieve licenses from app.config
private void SetupKeys()
{
_licenseDRS.LicenseType = _licensePrepro.LicenseType = _licenseFmt.LicenseType = _licenseImageFile.LicenseType = LicenseTypes.Software;
_licenseDRS.SoftwareKey = ConfigurationManager.AppSettings["LicenseDRS"];
_licenseDRS.ResourcesPath = ConfigurationManager.AppSettings["ResourcesPath"];
_licensePrepro.SoftwareKey = ConfigurationManager.AppSettings["LicensePreprocessing"]; ;
_licenseFmt.SoftwareKey = ConfigurationManager.AppSettings["LicenseFormatting"];
_licenseImageFile.SoftwareKey = ConfigurationManager.AppSettings["LicenseImageFile"];
_licenseImageFile.ExtensionsToLoad[ImageFileExtensions.JPEG] = true;
_licenseImageFile.ExtensionsSoftwareKeys[ImageFileExtensions.JPEG] = ConfigurationManager.AppSettings["ImageFileExtensions.JPEG"];
}
private void SetupModules()
{
IDRSManager.SetupModule(ModuleTypes.DRS, _licenseDRS);
IDRSManager.SetupModule(ModuleTypes.Preprocessing, _licensePrepro);
IDRSManager.SetupModule(ModuleTypes.Formatting, _licenseFmt);
IDRSManager.SetupModule(ModuleTypes.ImageFile, _licenseImageFile);
}
// We need to unload the IDRSManager once we're done, else we get an ugly exception
public void Dispose()
{
IDRSManager.Unload();
}
}
Just one last rant before I sign off and leave you to fend for yourself and that is about the IDRSException class or should I
rather say glorified error code handler?
I really hate the design of this class, if any exception occurs you simply get a
message telling you "An error occured in the iDRS" along with an error code
(in the IDRSErrorCode property of the exception)
which you need to reference in the documentation provided with the SDK.
It would have been a great deal more elegant if the message
contained the actual message relating to the error, perhaps consider adding an
extension method mapping the error code to an actual useful message like seen in the following snippet:
public static String GetMessage(this IDRSException ex)
{
switch (ex.IDRSErrorCode)
{
case 6005:
return "Cannot talk to the license server on the specified host. The license server is not running.";
case 6092:
return "The given license code is invalid. Hence, it could not be added to this license server.";
}
return ex.Message;
}
Posted by - Christoff Truter
Date - 2011-08-04 16:46:45
Comments
Post comment