AWS Lambdaでドキュメントを読み取るためにIronOCRを使用

This article was translated from English: Does it need improvement?
Translated
View the article in English

このハウツー記事では、IronOCRを使用したAWS Lambda関数の設定手順をガイドします。 以下のガイドで、IronOCRの設定方法とS3バケットからドキュメントを読み取る方法を学びます。

1. コンテナテンプレートを使用してAWS Lambdaを作成する

IronOCRが適切に機能するためには、特定の依存関係がマシンにインストールされている必要があります。これらの依存関係がインストールされていることを確認するために、Lambda関数はコンテナ化されなければなりません。

Visual Studioを使用すると、コンテナ化されたAWS Lambdaの作成は簡単です。以下の手順に従ってインストールしてください。 AWSツールキット for Visual StudioAWS LambdaのC#テンプレートを選択し、次にコンテナイメージのブループリントを選択し、「完了」を選択します。

2. パッケージの依存関係を追加

以下の内容を使用して、プロジェクトのDockerfileを修正してください: もちろんです!翻訳したいコンテンツを提供してください。それに従って正確な日本語訳を提供いたします。

.NET 5/6 用に dotnet:5 または dotnet:6 に設定します

public.ecr.aws/lambda/dotnet:7 から

作業ディレクトリ /var/task

RUN yum update -y

RUN yum install -y amazon-linux-extras

RUN amazon-linux-extras install epel -y

RUN yum install -y libgdiplus

以下の内容を日本語に翻訳してください:

"bin/Release/lambda-publish" をコピー。 もちろんです!翻訳したいコンテンツを提供してください。それに従って正確な日本語訳を提供いたします。

IronOCRとIronOCR.Linux NuGetパッケージをインストールする

IronOcrIronOcr.Linux パッケージをVisual Studioにインストールするには:

  1. 「プロジェクト」> 「NuGetパッケージの管理...」へ移動

  2. Browse を選択し、次に IronOcrIronOcr.Linux を検索してください。

  3. パッケージを選択してインストールします。

関数ハンドラーコードを修正する

この例では、S3バケットから画像を取得して読み取ります。 それは、SixLaborsImage クラスを使用して画像を IronOCR に読み込みます。 例が正常に動作するためには、S3バケットが設定されている必要があります。 SixLabors.ImageSharp(シックスレイバーズ・イメージシャープ) NuGetパッケージをインストールする必要があります。

:path=/static-assets/ocr/content-code-examples/how-to/iron-ocr-aws-lambda-sample.cs
using Amazon;
using Amazon.Lambda.Core;
using Amazon.S3;
using Amazon.S3.Model;
using SixLabors.ImageSharp;
using IronOcr;

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace IronOcrAWSLambda;

public class Function
{
	private readonly IAmazonS3 _s3Client;
	private readonly string accessKey;
	private readonly string secretKey;

	public Function()
	{
		accessKey = "ACCESS-KEY";
		secretKey = "SECRET-KEY";
		_s3Client = new AmazonS3Client(accessKey, secretKey);
	}
	public async Task<string> FunctionHandler(string input, ILambdaContext context)
	{
		IronOcr.License.LicenseKey = "IRONOCR-LICENSE-KEY";
		string bucketName = "S3-BUCKET-NAME";

		var getObjectRequest = new GetObjectRequest
		{
			BucketName = bucketName,
			Key = input,
		};

		using (GetObjectResponse response = await _s3Client.GetObjectAsync(getObjectRequest))
		{
			// Read the content of the object
			using (Stream responseStream = response.ResponseStream)
			{
				Console.WriteLine("Reading image from S3");
				Image image = Image.Load(responseStream);

				Console.WriteLine("Reading image with IronOCR");
				IronTesseract ironTesseract = new IronTesseract();
				OcrInput ocrInput = new OcrInput(image);
				OcrResult result = ironTesseract.Read(ocrInput);

				return result.Text;
			}
		}
	}
}
Imports Amazon
Imports Amazon.Lambda.Core
Imports Amazon.S3
Imports Amazon.S3.Model
Imports SixLabors.ImageSharp
Imports IronOcr

' Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
<Assembly: LambdaSerializer(GetType(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))>

Namespace IronOcrAWSLambda

	Public Class [Function]
		Private ReadOnly _s3Client As IAmazonS3
		Private ReadOnly accessKey As String
		Private ReadOnly secretKey As String

		Public Sub New()
			accessKey = "ACCESS-KEY"
			secretKey = "SECRET-KEY"
			_s3Client = New AmazonS3Client(accessKey, secretKey)
		End Sub
		Public Async Function FunctionHandler(ByVal input As String, ByVal context As ILambdaContext) As Task(Of String)
			IronOcr.License.LicenseKey = "IRONOCR-LICENSE-KEY"
			Dim bucketName As String = "S3-BUCKET-NAME"

			Dim getObjectRequest As New GetObjectRequest With {
				.BucketName = bucketName,
				.Key = input
			}

			Using response As GetObjectResponse = Await _s3Client.GetObjectAsync(getObjectRequest)
				' Read the content of the object
				Using responseStream As Stream = response.ResponseStream
					Console.WriteLine("Reading image from S3")
					Dim image As Image = System.Drawing.Image.Load(responseStream)

					Console.WriteLine("Reading image with IronOCR")
					Dim ironTesseract As New IronTesseract()
					Dim ocrInput As New OcrInput(image)
					Dim result As OcrResult = ironTesseract.Read(ocrInput)

					Return result.Text
				End Using
			End Using
		End Function
	End Class
End Namespace
VB   C#

メモリとタイムアウトの増加

Lambdaで割り当てるメモリの量は、読み込むドキュメントのサイズおよび一度に読み込む数に基づいて異なります。 基準として、aws-lambda-tools-defaults.jsonでメモリサイズを512 MB、タイムアウトを300秒に設定してください: もちろんです!翻訳したいコンテンツを提供してください。それに従って正確な日本語訳を提供いたします。

「function-memory-size」:512

「function-timeout」: 300 もちろんです!翻訳したいコンテンツを提供してください。それに従って正確な日本語訳を提供いたします。

6. 公開

Visual Studio で公開するには、ソリューションを右クリックして「Publish to AWS Lambda...」を選択し、設定を行います。 Lambdaの公開について詳しくは、 AWSウェブサイト.

7. 試してみてください!

Lambda関数を有効化するには、 Lambdaコンソール またはVisual Studioを通して。