跳转到主要内容
此场景可提取文档正文,以及徽标、印章和除正文外其他任何元素上的文本。 文本会保留其自然顺序,即“人阅读时的顺序”。然后,您可以将这些文档输入到您侧的自然语言处理 (NLP) 引擎中,例如用于快速摘要、搜索敏感信息或进行情感分析。 为提取文档的主要文本,通过扫描获得或以电子格式保存的图像文件通常会经过多个处理阶段,而每个阶段都有其各自的特点:
  1. 对扫描图像或照片进行预处理
扫描图像在识别前可能需要进行一些预处理,例如扫描文档存在背景噪点、文本倾斜、颜色反转、黑边、方向错误或分辨率问题时。
  1. 识别文档图像中尽可能多的文本
图像识别会使用相应的设置,以确保从文档图像中找到并提取所有可能的文本。

场景实现

本主题中提供的代码示例仅适用于 Windows。
下面将详细介绍在此场景中使用 ABBYY FineReader Engine 12 的推荐方法。该方法采用了最适合此场景的处理设置。
要开始使用 ABBYY FineReader Engine,您需要创建 Engine 对象。Engine 对象是 ABBYY FineReader Engine 对象层次结构中的顶层对象,提供各种全局设置、部分处理方法以及用于创建其他对象的方法。要创建 Engine 对象,可以使用 InitializeEngine 函数。另请参阅加载 Engine 对象的其他方式 (Win) 。

C#

public class EngineLoader : IDisposable
{
    public EngineLoader()
    {
        // 使用 FREngine.dll 的完整路径、您的 Customer Project ID,
        // 以及(如适用)在线许可证令牌文件的路径和在线许可证密码初始化这些变量
        string enginePath = "";
        string customerProjectId = "";
        string licensePath = "";
        string licensePassword = "";
        // 加载 FREngine.dll 库
        dllHandle = LoadLibraryEx(enginePath, IntPtr.Zero, LOAD_WITH_ALTERED_SEARCH_PATH);
           
        try
        {
            if (dllHandle == IntPtr.Zero)
            {
                throw new Exception("无法加载 " + enginePath);
            }
            IntPtr initializeEnginePtr = GetProcAddress(dllHandle, "InitializeEngine");
            if (initializeEnginePtr == IntPtr.Zero)
            {
                throw new Exception("找不到 InitializeEngine 函数");
            }
            IntPtr deinitializeEnginePtr = GetProcAddress(dllHandle, "DeinitializeEngine");
            if (deinitializeEnginePtr == IntPtr.Zero)
            {
                throw new Exception("找不到 DeinitializeEngine 函数");
            }
            IntPtr dllCanUnloadNowPtr = GetProcAddress(dllHandle, "DllCanUnloadNow");
            if (dllCanUnloadNowPtr == IntPtr.Zero)
            {
                throw new Exception("找不到 DllCanUnloadNow 函数");
            }
            // 将指针转换为委托
            initializeEngine = (InitializeEngine)Marshal.GetDelegateForFunctionPointer(
                initializeEnginePtr, typeof(InitializeEngine));
            deinitializeEngine = (DeinitializeEngine)Marshal.GetDelegateForFunctionPointer(
                deinitializeEnginePtr, typeof(DeinitializeEngine));
            dllCanUnloadNow = (DllCanUnloadNow)Marshal.GetDelegateForFunctionPointer(
                dllCanUnloadNowPtr, typeof(DllCanUnloadNow));
            // 调用 InitializeEngine 函数,
            // 传入在线许可证文件的路径和在线许可证密码
            int hresult = initializeEngine(customerProjectId, licensePath, licensePassword, 
                "", "", false, ref engine);
            Marshal.ThrowExceptionForHR(hresult);
        }
        catch (Exception)
        {
            // 释放 FREngine.dll 库
            engine = null;
            // 在调用 FreeLibrary 前释放所有对象
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            FreeLibrary(dllHandle);
            dllHandle = IntPtr.Zero;
            initializeEngine = null;
            deinitializeEngine = null;
            dllCanUnloadNow = null;
            throw;
        }
    }
    // Kernel32.dll 函数
    [DllImport("kernel32.dll")]
    private static extern IntPtr LoadLibraryEx(string dllToLoad, IntPtr reserved, uint flags);
    private const uint LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008;
    [DllImport("kernel32.dll")]
    private static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
    [DllImport("kernel32.dll")]
    private static extern bool FreeLibrary(IntPtr hModule);
    // FREngine.dll 函数
    [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode)]
    private delegate int InitializeEngine(string customerProjectId, string licensePath, 
        string licensePassword, string tempFolder, string dataFolder, bool isSharedCPUCoresMode, 
        ref FREngine.IEngine engine);
    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    private delegate int DeinitializeEngine();
    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    private delegate int DllCanUnloadNow();
    // 私有变量
    private FREngine.IEngine engine = null;
    // FREngine.dll 的句柄
    private IntPtr dllHandle = IntPtr.Zero;
    private InitializeEngine initializeEngine = null;
    private DeinitializeEngine deinitializeEngine = null;
    private DllCanUnloadNow dllCanUnloadNow = null;
}

C++ (COM)

// 请将以下变量初始化为 FREngine.dll 的路径、您的 FineReader Engine Customer Project ID,
// 以及(如适用)Online License 令牌路径和 Online License 密码
wchar_t* FreDllPath;
wchar_t* CustomerProjectId;
wchar_t* LicensePath;  // 如果不使用 Online License,请将这些变量赋值为空字符串
wchar_t* LicensePassword;
// FREngine.dll 的句柄
static HMODULE libraryHandle = 0;
// 全局 FineReader Engine 对象
FREngine::IEnginePtr Engine;
void LoadFREngine()
{
    if( Engine != 0 ) {
    // 已加载
    return;
    }
    // 第一步:加载 FREngine.dll
    if( libraryHandle == 0 ) {
        libraryHandle = LoadLibraryEx( FreDllPath, 0, LOAD_WITH_ALTERED_SEARCH_PATH );
        if( libraryHandle == 0 ) {
            throw L"加载 ABBYY FineReader Engine 时出错";
        }
    }
    // 第二步:获取 Engine 对象
    typedef HRESULT ( STDAPICALLTYPE* InitializeEngineFunc )( BSTR, BSTR, BSTR, BSTR, 
        BSTR, VARIANT_BOOL, FREngine::IEngine** );
    InitializeEngineFunc pInitializeEngine =
    ( InitializeEngineFunc )GetProcAddress( libraryHandle, "InitializeEngine" );
    if( pInitializeEngine == 0 || pInitializeEngine( CustomerProjectId, LicensePath, 
        LicensePassword, L"", L"", VARIANT_FALSE, &Engine ) != S_OK ) {
    UnloadFREngine();
    throw L"加载 ABBYY FineReader Engine 时出错";
    }
}
在 ABBYY FineReader Engine 中,可使用 Engine 对象的 LoadPredefinedProfile 方法,为此场景选择最合适的设置。该方法接收配置文件名称作为输入参数。更多信息,请参见 Working with ProfilesABBYY FineReader Engine 为此场景提供 2 种设置变体:

配置文件名称

说明

TextExtraction_Accuracy

这些设置已针对准确性进行优化:

  • 可检测图像上的所有文本,包括质量较低的小文本区域 (不检测图片和表格) 。
  • 不会对文档的逻辑结构进行完整重建。
此配置文件不适用于将文档转换为 RTF、DOCX 或纯文本 PDF。此类用途请使用文档转换配置文件。

TextExtraction_Speed

这些设置已针对处理速度进行优化:

  • 可检测图像上的所有文本,包括质量较低的小文本区域 (不检测图片和表格) 。
  • 不会对文档的逻辑结构进行完整重建。
  • 加快了文档分析和识别过程。
此配置文件不适用于将文档转换为 RTF、DOCX 或纯文本 PDF。此类用途请使用文档转换配置文件。

C#

// 加载预定义配置文件
engine.LoadPredefinedProfile("TextExtraction_Accuracy");

C++ (COM)

// 加载预定义配置文件
Engine->LoadPredefinedProfile( L"TextExtraction_Accuracy" );
如果您想更改处理设置,请使用相应的参数对象。更多信息,请参见下文的 针对特定任务的附加优化
ABBYY FineReader Engine 提供 FRDocument 对象,用于处理多页文档。要加载单个文档的图像并进行预处理,您应创建 FRDocument 对象并向其中添加图像。您可以采用以下任一方式:

C#

// 从图像文件创建 FRDocument 对象
FREngine.IFRDocument frDocument = engine.CreateFRDocumentFromImage( "C:\\MyImage.tif", null );

C++ (COM)

// 打开图像文件并创建 FRDocument 对象
FREngine::IFRDocumentPtr frDocument = Engine->CreateFRDocumentFromImage( L"C:\\MyImage.tif", 0 );
要识别文档,应使用 FRDocument 对象的分析和识别方法。该对象提供了整套文档分析和识别方法。若要仅通过一次调用就完成文档分析、识别和合成,最便捷的方法是使用 Process 方法。它还能以最高效的方式利用多处理器和多核系统的并行处理能力。不过,您也可以使用 PreprocessAnalyzeRecognizeSynthesize 方法,按顺序执行预处理、分析、识别和合成。

C#

// 分析、识别并合成文档
// 无需额外参数,因为这些参数已由处理配置文件设置
frDocument.Process( null );

C++ (COM)

// 分析、识别并合成文档
// 加载配置文件后,无需向处理方法传递任何附加参数
frDocument->Process( 0 );
在分析过程中,ABBYY FineReader Engine 会选取包含文本、表格、图片等内容的图像块。在识别过程中,包含文本数据的块会填充为识别出的文本。在 ABBYY FineReader Engine 中,Layout 对象用于存储块和识别出的文本。文档处理的主要场景是在 FRDocument 对象中处理 layout,该对象表示正在处理的文档。要访问文档页面的 layout,请使用 IFRPage::Layout 属性。要搜索关键词,您可以通过 Text 对象查看识别出的文本;该对象可通过文本块、表格块或条码块的属性访问。找到的重要数据可根据需要保存或进一步处理。更多详细信息,请参见下方的 针对特定任务的附加优化
此外,您可能希望将提取出的文本保存为便于搜索的格式 (如 TXT) ,或保存为结构化格式 (如 JSON) ,以便后续轻松检索所需信息。使用 FRDocument 对象的 Export 方法,并将相应的 FileExportFormatEnum 常量作为其中一个参数。您可以使用相应的导出对象更改默认导出参数。更多信息,请参见下方的 针对特定任务的附加优化完成对 FRDocument 对象的操作后,请释放该对象使用的所有资源。请使用 IFRDocument::Close 方法。

C#

// 将识别出的文档文本保存为 TXT 格式
frDocument.Export( "C:\\MyText.txt", FREngine.FileExportFormatEnum.FEF_TextUnicodeDefaults, null );
// 释放 FRDocument 对象
frDocument.Close();

C++ (COM)

// 将识别出的文档文本保存为 TXT 格式
frDocument->Export( L"C:\\MyText.txt", FREngine::FEF_TextUnicodeDefaults, 0 );
// 释放 FRDocument 对象
frDocument->Close();
使用完 ABBYY FineReader Engine 后,您需要卸载Engine对象。为此,请使用导出的 DeinitializeEngine 函数。

C#

public class EngineLoader : IDisposable
{
    // 卸载 FineReader Engine
    public void Dispose()
    {
        if (engine == null)
        {
            // Engine 尚未加载
            return;
        }
        engine = null;
        // 在调用 FreeLibrary 之前回收所有对象
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
        int hresult = deinitializeEngine();
 
        hresult = dllCanUnloadNow();
        if (hresult == 0)
        {
            FreeLibrary(dllHandle);
        }
        dllHandle = IntPtr.Zero;
        initializeEngine = null;
        deinitializeEngine = null;
        dllCanUnloadNow = null;
        // 清理后引发异常
        Marshal.ThrowExceptionForHR(hresult);
    }
    // Kernel32.dll 函数
    [DllImport("kernel32.dll")]
    private static extern IntPtr LoadLibraryEx(string dllToLoad, IntPtr reserved, uint flags);
    private const uint LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008;
    [DllImport("kernel32.dll")]
    private static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
    [DllImport("kernel32.dll")]
    private static extern bool FreeLibrary(IntPtr hModule);
    // FREngine.dll 函数
    [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode)]
    private delegate int InitializeEngine( string customerProjectId, string LicensePath, string LicensePassword, , , , ref FREngine.IEngine engine);
    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    private delegate int DeinitializeEngine();
    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    private delegate int DllCanUnloadNow();
    // 私有变量
    private FREngine.IEngine engine = null;
    // FREngine.dll 的句柄
    private IntPtr dllHandle = IntPtr.Zero;
    private InitializeEngine initializeEngine = null;
    private DeinitializeEngine deinitializeEngine = null;
    private DllCanUnloadNow dllCanUnloadNow = null;
}

C++ (COM)

void UnloadFREngine()
{
 if( libraryHandle == 0 ) {
  return;
 }
 // 释放 Engine 对象
 Engine = 0;
 // 取消初始化 FineReader Engine
 typedef HRESULT ( STDAPICALLTYPE* DeinitializeEngineFunc )();
 DeinitializeEngineFunc pDeinitializeEngine =
  ( DeinitializeEngineFunc )GetProcAddress( libraryHandle, "DeinitializeEngine" );
 if( pDeinitializeEngine == 0 || pDeinitializeEngine() != S_OK ) {
  throw L"卸载 ABBYY FineReader Engine 时出错";
 }
 // 现在可以安全释放 FREngine.dll 库
 FreeLibrary( libraryHandle );
 libraryHandle = 0;
}

必需资源

您可以使用 FREngineDistribution.csv 文件,自动生成应用程序运行所需文件的列表。对于按此场景进行处理,请在第 5 列 (RequiredByModule) 中选择以下值: Core Core.Resources Opening Opening, Processing Processing Processing.OCR Processing.OCR, Processing.ICR Processing.OCR.NaturalLanguages Processing.OCR.NaturalLanguages, Processing.ICR.NaturalLanguages 如果您修改了标准场景,请相应调整所需模块。您还需要指定应用程序使用的界面语言、识别语言以及其他附加功能 (例如,如果您需要打开 PDF 文件,则指定 Opening.PDF;如果您需要识别 CJK languages 中的文本,则指定 Processing.OCR.CJK) 。更多详细信息,请参阅 Working with the FREngineDistribution.csv File

针对特定任务的附加优化

  • 扫描 - 仅限 Windows
    • 扫描
      ABBYY FineReader Engine 文档扫描场景说明。
  • 识别
  • 识别手写文本
    TextExtraction*** 配置文件不包含手写体或手写印刷体文本识别。如果您需要识别手写内容,请将 PageAnalysisParams 对象的 DetectHandwritten 属性设置为 TRUE。
  • PageProcessingParams 对象
    此对象可用于自定义分析和识别参数。使用此对象,您可以指定需要检测的图像和文本特征 (反相图像、方向、条码、识别语言、识别误差范围) 。
  • SynthesisParamsForPage 对象
    此对象包含负责在合成期间恢复页面格式的参数。
  • SynthesisParamsForDocument 对象
    此对象可用于自定义文档合成:恢复其结构和格式。
  • MultiProcessingParams 对象 - 仅限 Linux 和 Windows
    处理大量图像时,同时处理会很有帮助。在这种情况下,处理负载会在图像打开和预处理、版面分析以及识别期间分配到各个处理器核心上,从而加快处理速度。
    读取模式 (同时或连续) 通过 MultiProcessingMode 属性设置。RecognitionProcessesCount 属性用于控制可启动的进程数量。
  • 搜索重要信息
    • 使用 Layout 和 Blocks
      关于页面版面、块类型以及如何处理它们。
    • Layout 对象
      此对象的参数可用于访问页面版面以及文档识别后的文本。
    • 处理 Text
      处理已识别的文本、段落、单词和字符。
  • 针对指定数据类型使用特殊参数重新识别文档
  • 保存数据

另请参阅

基本使用场景的实现