LEADTOOLS表單識別應用:自動歸檔掃描文件

文檔圖像雖然可以節約物理儲存空間,但是某些情況下,它無法節約時間和省去麻煩。無紙化報表和手動掃描紙質文檔是歸檔賬單、發票和財務報表的比較好的方式。然而,工作人員需要花較多的時間和精力來整理這些數字文件夾。並且,隨着數字文檔的增多,即使整理歸檔的人具有很好的記憶力和習慣,也容易出現差錯。

LEADTOOLS Forms Recognition and Processing擁有強大而靈活的圖像庫。通過LEADTOOLS構建出的應用程序可以自動對比掃描文檔與已知模板,然後準確地對掃描文檔進行歸類。當文檔被準確識別後,LEADTOOLS可以從表單上所定義的位置提取OCR、 OMR和Barcodes等。

處理文檔庫

解決文檔歸類問題的首要步驟就是處理和管理所有掃描文檔歸類時所存放的文檔庫。處理文檔庫的方法很多,本示例選擇使用控制檯應用程序來處理文檔庫。管理文檔庫的代碼比較簡單,因爲它主要使用基本文件和帶有System.IO命名空間的文件夾操作。而最關鍵的部分就是將應用程序傳遞到封裝了LEADTOOLS Forms Recognition的DocumentClassifier,從而返回移動的數據並重命名文件。

// Check the scanned document repository for new documents
string[] newDocuments = Directory.GetFiles(docRepositoryNewDocs);
DocumentClassifier docClassifier = new DocumentClassifier(docRepositoryMasterForms);
string movedDocumentName, masterFormSubFolder;
foreach (string currentDoc in newDocuments)
{
movedDocumentName = null;
// Try to match this document against known document types
ClassifiedDocument classifiedDoc = docClassifier.ClassifyDocument(currentDoc);
if (classifiedDoc.MasterFormName != null)
{
// Add the subfolder for the master form if it doesn't exist
masterFormSubFolder = string.Format(@"{0}{1}\",
docRepositoryRoot,classifiedDoc.MasterFormName);
if (!Directory.Exists(masterFormSubFolder))
Directory.CreateDirectory(masterFormSubFolder);

// rename the file according to the date found
if (classifiedDoc.DocumentDate != DateTime.MinValue)
{
movedDocumentName = string.Format("{0}{1}{2}",
masterFormSubFolder,
classifiedDoc.DocumentDate.ToString("yyyyMMdd"),
currentDoc.Substring(currentDoc.LastIndexOf('.'),
currentDoc.Length - currentDoc.LastIndexOf('.')));
}
else
{
// Didn't find a date to rename with, so just move it
movedDocumentName = currentDoc.Replace(docRepositoryNewDocs, masterFormSubFolder);
}
}
else
{
movedDocumentName = currentDoc.Replace(docRepositoryNewDocs,
docRepositoryUnclassifiedDocs);
}

if (!string.IsNullOrEmpty(movedDocumentName))
File.Move(currentDoc, movedDocumentName);
}

使用LEADTOOLS表單識別功能

在 LEADTOOLS歸類文檔前,必須創建一個Master Form模板集,使LEADTOOLS知道如何對文檔進行分類。LEADTOOLS自帶一個Master Form編輯器演示,我們根據演示爲包含了發票日期字段的兩張不同發票添加一個Master Form。

我們定義好Master Form後,接下來準備處理文檔。我們已經掃描了2張基於Master Form的發票和tax form,將每個文件放置到 "New" 中, LEADTOOLS會自動對比主模板。如果LEADTOOLS找到匹配文件,它會處理文檔的字段,然後返回表單名稱和日期字段。

// Create an OCR Engine for each processor on the machine. This
// allows for optimal use of thread during recognition and processing.
ocrEngines = new List<IOcrEngine>();
for (int i = 0; i < Environment.ProcessorCount; i++)
{
ocrEngines.Add(OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false));
ocrEngines[i].Startup(formsCodec, null, String.Empty, String.Empty);
}
// Point repository to directory with existing master forms
formsRepository = new DiskMasterFormsRepository(formsCodec, _MasterFormFolder);
autoEngine = new AutoFormsEngine(formsRepository, ocrEngines, null, AutoFormsRecognitionManager.Default | AutoFormsRecognitionManager.Ocr, 30, 70, true);

// Run the forms recognition on this document
AutoFormsRunResult runResult = autoEngine.Run(document, null);
if (runResult != null)
{
// In this example we use two pieces of information to organize the classified forms:
// 1. Form name is used for the sub folder
// 2. "ClassificationRenameDate" field for the file name
retClassifiedDocument.MasterFormName = runResult.RecognitionResult.MasterForm.Name;

// Process the recognized form and extract desired info
foreach (FormPage formPage in runResult.FormFields)
{
foreach (FormField field in formPage)
{
if (field != null && field.Name == "ClassificationRenameDate")
{
retClassifiedDocument.DocumentDate = DateTime.Parse((
field.Result as TextFormFieldResult).Text);
}
}
}
}

你會看到兩張發票完全與Master Form完全匹配,並根據日期重新命名。

發佈了6 篇原創文章 · 獲贊 2 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章