ドキュメント処理の自動化は、法的および金融サービスから医療や製造に至るまで、さまざまな産業における生産性と正確性を大幅に向上させることができます。このタスクのための強力なツールは Aspose.OCR for .NET で、開発者が高精度でスキャンされた文書や画像からテキストを抽出することを可能にします。

完全例

以下は、複数のディレクトリの画像で OCR を実行するために .NET の Aspose.OCR を使用する方法を示す完全な例です。

ステップ・ステップ・ガイド

ステップ1:OCRエンジンを起動する

OCR エンジンを作成および設定します. 望ましい言語(この例の英語)を設定してください。

// Step 1: Initialize the OCR Engine
using Aspose.Ocr;

using (Ocr ocrEngine = new Ocr())
{
    // Set language and other configurations if needed
    ocrEngine.Language = Language.English;

    // (Continue with steps below inside this using block)
}

ステップ2:画像をアップロード

入力/出力ディレクトリを定義し、出口フォルダーが存在することを確認し,画像ファイルをリストします。

// Step 2: Load Images for Processing
string inputDirectory = @"path\to\input\images";
string outputDirectory = @"path\to\output\text";

if (!Directory.Exists(outputDirectory))
{
    Directory.CreateDirectory(outputDirectory);
}

// Get all files from the input directory (same pattern as the gist)
// TIP: to restrict to specific formats, replace "*.*" with "*.png" or "*.jpg"
string[] imageFiles = Directory.GetFiles(
    inputDirectory,
    "*.*",
    SearchOption.TopDirectoryOnly
);

ステップ3:各画像でOCRを実行する

ファイルを振り回し、テキストを認識する RecognizeImage(string path).

// Step 3: Perform OCR on Each Image
foreach (string imageFile in imageFiles)
{
    try
    {
        // Recognize text from the image (exactly as in the gist)
        string recognizedText = ocrEngine.RecognizeImage(imageFile);

        // Proceed to Step 4: save text to disk...
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error processing {imageFile}: {ex.Message}");
    }
}

ステップ4:ファイルに抽出されたテキストを保存する

相応の作成 .txt すべての処理された画像のためのファイル。

// Step 4: Save Extracted Text to Files
string outputFilePath = Path.Combine(
    outputDirectory,
    Path.GetFileNameWithoutExtension(imageFile) + ".txt"
);

File.WriteAllText(outputFilePath, recognizedText);

Console.WriteLine($"Processed: {imageFile} -> {outputFilePath}");

タイトル&Tweaks

  • フィルターフォーマット: パターンを使用する "*.png" または "*.jpg" 非画像ファイルを削除します。
    • リクルス・サブフォーダー*: 変更 SearchOption.TopDirectoryOnlySearchOption.AllDirectories.
  • 船の空き出力:もし string.IsNullOrWhiteSpace(recognizedText)ログして続ける。
  • パラレルバッチ:使用 Parallel.ForEach(imageFiles, file => { ... }) より速い走行(I/Oとライセンス)のために。

これらのステップに従って、Aspose.OCR for .NET を使用して OCR バッチを自動化し、ダウンストリーム処理のためのクリーンテキストファイルを輸出できます。

More in this category