イメージ内のテキストの検出 - Amazon Rekognition

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

イメージ内のテキストの検出

入力イメージとして、イメージのバイト配列 (base64 エンコードされたイメージのバイト) を指定するか、Amazon S3 オブジェクトを指定できます。次の手順では、JPEG イメージまたは PNG イメージを S3 バケットにアップロードし、そのファイル名を指定します。

イメージ内のテキストを検出するには (API)
  1. まだ知識がない場合は、次の前提条件を完了します。

    1. を作成または更新します。AWS Identity and Access Management(IAM) ユーザーAmazonRekognitionFullAccessそしてAmazonS3ReadOnlyAccessアクセス許可。詳細については、「ステップ 1: AWS アカウントをセットアップし、IAM ユーザーを作成します。」を参照してください。

    2. AWS Command Line Interface と AWS SDK をインストールして設定します。詳細については、「ステップ 2: をセットアップするAWS CLIそしてAWSSDK」を参照してください。

  2. テキストが含まれているイメージを S3 バケットにアップロードします。

    手順については、以下を参照してください。Amazon S3 へのオブジェクトのアップロードAmazon Simple Storage Service ユーザーガイド

  3. 以下の例を使用して、DetectText オペレーションを呼び出します。

    Java

    次のコード例では、イメージ内で検出された行と単語を表示します。

    の値を置換するbucketそしてphotoとは、ステップ 2 で使用した S3 バケット名とイメージ名を使用します。

    //Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. //PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-rekognition-developer-guide/blob/master/LICENSE-SAMPLECODE.) package aws.example.rekognition.image; import com.amazonaws.services.rekognition.AmazonRekognition; import com.amazonaws.services.rekognition.AmazonRekognitionClientBuilder; import com.amazonaws.services.rekognition.model.AmazonRekognitionException; import com.amazonaws.services.rekognition.model.Image; import com.amazonaws.services.rekognition.model.S3Object; import com.amazonaws.services.rekognition.model.DetectTextRequest; import com.amazonaws.services.rekognition.model.DetectTextResult; import com.amazonaws.services.rekognition.model.TextDetection; import java.util.List; public class DetectText { public static void main(String[] args) throws Exception { String photo = "inputtext.jpg"; String bucket = "bucket"; AmazonRekognition rekognitionClient = AmazonRekognitionClientBuilder.defaultClient(); DetectTextRequest request = new DetectTextRequest() .withImage(new Image() .withS3Object(new S3Object() .withName(photo) .withBucket(bucket))); try { DetectTextResult result = rekognitionClient.detectText(request); List<TextDetection> textDetections = result.getTextDetections(); System.out.println("Detected lines and words for " + photo); for (TextDetection text: textDetections) { System.out.println("Detected: " + text.getDetectedText()); System.out.println("Confidence: " + text.getConfidence().toString()); System.out.println("Id : " + text.getId()); System.out.println("Parent Id: " + text.getParentId()); System.out.println("Type: " + text.getType()); System.out.println(); } } catch(AmazonRekognitionException e) { e.printStackTrace(); } } }
    Java V2

    このコードはAWSドキュメント SDK の例 GitHub リポジトリ。完全な例を見るここに

    public static void detectTextLabels(RekognitionClient rekClient, String sourceImage) { try { InputStream sourceStream = new FileInputStream(sourceImage); SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream); // Create an Image object for the source image Image souImage = Image.builder() .bytes(sourceBytes) .build(); DetectTextRequest textRequest = DetectTextRequest.builder() .image(souImage) .build(); DetectTextResponse textResponse = rekClient.detectText(textRequest); List<TextDetection> textCollection = textResponse.textDetections(); System.out.println("Detected lines and words"); for (TextDetection text: textCollection) { System.out.println("Detected: " + text.detectedText()); System.out.println("Confidence: " + text.confidence().toString()); System.out.println("Id : " + text.id()); System.out.println("Parent Id: " + text.parentId()); System.out.println("Type: " + text.type()); System.out.println(); } } catch (RekognitionException | FileNotFoundException e) { System.out.println(e.getMessage()); System.exit(1); } }
    AWS CLI

    この AWS CLI コマンドでは、detect-text CLI オペレーションの JSON 出力を表示します。

    の値を置換するBucketそしてNameとは、ステップ 2 で使用した S3 バケット名とイメージ名を使用します。

    aws rekognition detect-text \ --image "S3Object={Bucket=bucketname,Name=input.jpg}"
    Python

    次のコード例では、イメージ内で検出された行と単語を表示します。

    の値を置換するbucketそしてphotoには、ステップ 2 で使用した S3Bucket 名とイメージ名を使用します。

    #Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. #PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-rekognition-developer-guide/blob/master/LICENSE-SAMPLECODE.) import boto3 def detect_text(photo, bucket): client=boto3.client('rekognition') response=client.detect_text(Image={'S3Object':{'Bucket':bucket,'Name':photo}}) textDetections=response['TextDetections'] print ('Detected text\n----------') for text in textDetections: print ('Detected text:' + text['DetectedText']) print ('Confidence: ' + "{:.2f}".format(text['Confidence']) + "%") print ('Id: {}'.format(text['Id'])) if 'ParentId' in text: print ('Parent Id: {}'.format(text['ParentId'])) print ('Type:' + text['Type']) print() return len(textDetections) def main(): bucket='bucket' photo='photo' text_count=detect_text(photo,bucket) print("Text detected: " + str(text_count)) if __name__ == "__main__": main()
    .NET

    次のコード例では、イメージ内で検出された行と単語を表示します。

    の値を置換するbucketそしてphotoとは、ステップ 2 で使用した S3 バケット名とイメージ名を使用します。

    //Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. //PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-rekognition-developer-guide/blob/master/LICENSE-SAMPLECODE.) using System; using Amazon.Rekognition; using Amazon.Rekognition.Model; public class DetectText { public static void Example() { String photo = "input.jpg"; String bucket = "bucket"; AmazonRekognitionClient rekognitionClient = new AmazonRekognitionClient(); DetectTextRequest detectTextRequest = new DetectTextRequest() { Image = new Image() { S3Object = new S3Object() { Name = photo, Bucket = bucket } } }; try { DetectTextResponse detectTextResponse = rekognitionClient.DetectText(detectTextRequest); Console.WriteLine("Detected lines and words for " + photo); foreach (TextDetection text in detectTextResponse.TextDetections) { Console.WriteLine("Detected: " + text.DetectedText); Console.WriteLine("Confidence: " + text.Confidence); Console.WriteLine("Id : " + text.Id); Console.WriteLine("Parent Id: " + text.ParentId); Console.WriteLine("Type: " + text.Type); } } catch (Exception e) { Console.WriteLine(e.Message); } } }
    Node.JS

    次のコード例では、イメージ内で検出された行と単語を表示します。

    の値を置換するbucketそしてphotoには、ステップ 2 で使用した S3Bucket 名とイメージ名を使用します。の値を置き換えるregion.aws 認証情報で見つかったリージョンを使用します。

    var AWS = require('aws-sdk'); const bucket = 'bucket' // the bucketname without s3:// const photo = 'photo' // the name of file const config = new AWS.Config({ accessKeyId: process.env.AWS_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, }) AWS.config.update({region:'region'}); const client = new AWS.Rekognition(); const params = { Image: { S3Object: { Bucket: bucket, Name: photo }, }, } client.detectText(params, function(err, response) { if (err) { console.log(err, err.stack); // handle error if an error occurred } else { console.log(`Detected Text for: ${photo}`) console.log(response) response.TextDetections.forEach(label => { console.log(`Detected Text: ${label.DetectedText}`), console.log(`Type: ${label.Type}`), console.log(`ID: ${label.Id}`), console.log(`Parent ID: ${label.ParentId}`), console.log(`Confidence: ${label.Confidence}`), console.log(`Polygon: `) console.log(label.Geometry.Polygon) } ) } });

DetectText オペレーションのリクエスト

DetectTextオペレーションでは、入力イメージを base64 エンコードされたバイト配列、または Amazon S3 バケットに保存されたイメージとして指定できます。次の JSON リクエストの例では、Amazon S3 バケットからロードしたイメージを表示します。

{ "Image": { "S3Object": { "Bucket": "bucket", "Name": "inputtext.jpg" } } }

フィルター

テキスト領域、サイズ、信頼スコアに基づくフィルタ処理により、テキスト検出出力をさらに柔軟に制御できるようになります。関心領域を使用することで、テキスト検出を関連する領域に簡単に制限できます。たとえば、機械のイメージから部品番号を読み取るとき、この領域はプロフィール写真の右上や基準点からの相対位置などです。単語の境界ボックスサイズフィルタを使用すると、ノイズの多いテキストや無関係な小さな背景テキストを回避できます。最後に、単語信頼性フィルタを使用すると、ぼやけているか汚れているせいで信頼できない結果を削除できます。以下のフィルタを使用できます。

  • MinConfidence単語検出の信頼性レベルを設定します。検出の信頼性がこのレベルより低い単語は、結果から除外されます。値は 0 から 100 の間で指定する必要があります。デフォルトの MinConfidence は 0 です。

  • MinBoundingBoxWidth— 単語境界ボックスの最小幅を設定します。境界ボックスの幅がこの値より小さい単語は、結果から除外されます。値はイメージフレームの幅に対する相対値です。

  • MinBoundingBoxHeight— 単語境界ボックスの最小高さを設定します。境界ボックスの高さがこの値より小さい単語は、結果から除外されます。値はイメージフレームの高さに対する相対値です。

  • RegionsOfInterest— 検出をイメージフレームの特定の領域に制限します。値はフレームの寸法に対する相対値です。領域内に部分的にしか含まれていないテキストの場合、レスポンスは不明となります。

DetectText オペレーションのレスポンス

DetectText オペレーションでは、イメージを分析して配列 (TextDetections) を返します。この配列の各要素 (TextDetection) は、イメージ内で検出された行または単語を表します。DetectText により、要素ごとに以下の情報が返されます。

  • 検出されたテキスト (DetectedText)

  • 単語と行の関係 (IdParentId)

  • イメージ上のテキストの位置 (Geometry)

  • 検出されたテキストおよび境界ボックスの精度に対する Amazon Rekognition の信頼度と境界ボックス (Confidence)

  • 検出されたテキストのタイプ (Type)

検出されたテキスト

TextDetection 要素には、DetectedText フィールドで認識されたテキスト (単語または行) が含まれます。単語とは、スペースで区切られていない、1 個以上のスクリプト文字です。DetectTextはイメージ内の最大 100 個の単語を検出できます。返されたテキストに含まれる文字によっては、単語が認識できない場合があります。たとえば、Cat の代わりに C@t が返される場合があります。TextDetection 要素がテキスト行または単語のいずれであるかを確認するには、Type フィールドを使用します。

eachTextDetection要素には、検出されたテキストおよびそのテキストを囲む境界ボックスの精度に対する Amazon Rekognition の信頼度 (パーセント値) が含まれます。

単語と行の関係

TextDetection 要素ごとに ID フィールド (Id) があります。Id は、行内での単語の位置を示します。要素が単語である場合、親識別子フィールド (ParentId) は単語が検出された行を識別します。行の ParentId は null です。たとえば、例のイメージで「but keep」行の Id 値と ParentId 値は以下のとおりです。

テキスト

ID

親 ID

but keep

3

but

8

3

keep

9

3

画像上のテキストの位置

イメージ上で認識されたテキストの位置を確認するには、DetectText から返される境界ボックス (Geometry) 情報を使用します。Geometry オブジェクトには、検出された行と単語に関する次の 2 種類の境界ボックス情報が含まれます。

  • BoundingBox オブジェクトの軸に揃えられた大まかな四角形のアウトライン

  • Point 配列の複数の X 座標と Y 座標で構成された詳細な多角形

境界ボックスと多角形の座標は、ソースイメージ上のテキストの位置を示します。座標値は、イメージサイズ全体の比率です。詳細については、「BoundingBox」を参照してください。

次の DetectText オペレーションからの JSON レスポンスは、次のイメージで検出された単語と行を示しています。

{ "TextDetections": [ { "Confidence": 90.54900360107422, "DetectedText": "IT'S", "Geometry": { "BoundingBox": { "Height": 0.10317354649305344, "Left": 0.6677391529083252, "Top": 0.17569075524806976, "Width": 0.15113449096679688 }, "Polygon": [ { "X": 0.6677391529083252, "Y": 0.17569075524806976 }, { "X": 0.8188736438751221, "Y": 0.17574213445186615 }, { "X": 0.8188582062721252, "Y": 0.278915673494339 }, { "X": 0.6677237153053284, "Y": 0.2788642942905426 } ] }, "Id": 0, "Type": "LINE" }, { "Confidence": 59.411651611328125, "DetectedText": "I", "Geometry": { "BoundingBox": { "Height": 0.05955825746059418, "Left": 0.2763049304485321, "Top": 0.394121915102005, "Width": 0.026684552431106567 }, "Polygon": [ { "X": 0.2763049304485321, "Y": 0.394121915102005 }, { "X": 0.30298948287963867, "Y": 0.3932435214519501 }, { "X": 0.30385109782218933, "Y": 0.45280176401138306 }, { "X": 0.27716654539108276, "Y": 0.453680157661438 } ] }, "Id": 1, "Type": "LINE" }, { "Confidence": 92.76634979248047, "DetectedText": "MONDAY", "Geometry": { "BoundingBox": { "Height": 0.11997425556182861, "Left": 0.5545867085456848, "Top": 0.34920141100883484, "Width": 0.39841532707214355 }, "Polygon": [ { "X": 0.5545867085456848, "Y": 0.34920141100883484 }, { "X": 0.9530020356178284, "Y": 0.3471102714538574 }, { "X": 0.9532787799835205, "Y": 0.46708452701568604 }, { "X": 0.554863452911377, "Y": 0.46917566657066345 } ] }, "Id": 2, "Type": "LINE" }, { "Confidence": 96.7636489868164, "DetectedText": "but keep", "Geometry": { "BoundingBox": { "Height": 0.0756164938211441, "Left": 0.634815514087677, "Top": 0.5181083083152771, "Width": 0.20877975225448608 }, "Polygon": [ { "X": 0.634815514087677, "Y": 0.5181083083152771 }, { "X": 0.8435952663421631, "Y": 0.52589350938797 }, { "X": 0.8423560857772827, "Y": 0.6015099883079529 }, { "X": 0.6335763335227966, "Y": 0.59372478723526 } ] }, "Id": 3, "Type": "LINE" }, { "Confidence": 99.47185516357422, "DetectedText": "Smiling", "Geometry": { "BoundingBox": { "Height": 0.2814019024372101, "Left": 0.48475268483161926, "Top": 0.6823741793632507, "Width": 0.47539761662483215 }, "Polygon": [ { "X": 0.48475268483161926, "Y": 0.6823741793632507 }, { "X": 0.9601503014564514, "Y": 0.587857186794281 }, { "X": 0.9847385287284851, "Y": 0.8692590594291687 }, { "X": 0.5093409419059753, "Y": 0.9637760519981384 } ] }, "Id": 4, "Type": "LINE" }, { "Confidence": 90.54900360107422, "DetectedText": "IT'S", "Geometry": { "BoundingBox": { "Height": 0.10387301445007324, "Left": 0.6685508489608765, "Top": 0.17597118020057678, "Width": 0.14985692501068115 }, "Polygon": [ { "X": 0.6677391529083252, "Y": 0.17569075524806976 }, { "X": 0.8188736438751221, "Y": 0.17574213445186615 }, { "X": 0.8188582062721252, "Y": 0.278915673494339 }, { "X": 0.6677237153053284, "Y": 0.2788642942905426 } ] }, "Id": 5, "ParentId": 0, "Type": "WORD" }, { "Confidence": 92.76634979248047, "DetectedText": "MONDAY", "Geometry": { "BoundingBox": { "Height": 0.11929994821548462, "Left": 0.5540683269500732, "Top": 0.34858056902885437, "Width": 0.3998897075653076 }, "Polygon": [ { "X": 0.5545867085456848, "Y": 0.34920141100883484 }, { "X": 0.9530020356178284, "Y": 0.3471102714538574 }, { "X": 0.9532787799835205, "Y": 0.46708452701568604 }, { "X": 0.554863452911377, "Y": 0.46917566657066345 } ] }, "Id": 7, "ParentId": 2, "Type": "WORD" }, { "Confidence": 59.411651611328125, "DetectedText": "I", "Geometry": { "BoundingBox": { "Height": 0.05981886386871338, "Left": 0.2779299318790436, "Top": 0.3935416042804718, "Width": 0.02624112367630005 }, "Polygon": [ { "X": 0.2763049304485321, "Y": 0.394121915102005 }, { "X": 0.30298948287963867, "Y": 0.3932435214519501 }, { "X": 0.30385109782218933, "Y": 0.45280176401138306 }, { "X": 0.27716654539108276, "Y": 0.453680157661438 } ] }, "Id": 6, "ParentId": 1, "Type": "WORD" }, { "Confidence": 95.33189392089844, "DetectedText": "but", "Geometry": { "BoundingBox": { "Height": 0.06849122047424316, "Left": 0.6350157260894775, "Top": 0.5214487314224243, "Width": 0.08413040637969971 }, "Polygon": [ { "X": 0.6347596645355225, "Y": 0.5215170383453369 }, { "X": 0.719483494758606, "Y": 0.5212655067443848 }, { "X": 0.7195737957954407, "Y": 0.5904868841171265 }, { "X": 0.6348499655723572, "Y": 0.5907384157180786 } ] }, "Id": 8, "ParentId": 3, "Type": "WORD" }, { "Confidence": 98.1954116821289, "DetectedText": "keep", "Geometry": { "BoundingBox": { "Height": 0.07207882404327393, "Left": 0.7295929789543152, "Top": 0.5265749096870422, "Width": 0.11196041107177734 }, "Polygon": [ { "X": 0.7290706038475037, "Y": 0.5251666903495789 }, { "X": 0.842876672744751, "Y": 0.5268880724906921 }, { "X": 0.8423973917961121, "Y": 0.5989891886711121 }, { "X": 0.7285913228988647, "Y": 0.5972678065299988 } ] }, "Id": 9, "ParentId": 3, "Type": "WORD" }, { "Confidence": 99.47185516357422, "DetectedText": "Smiling", "Geometry": { "BoundingBox": { "Height": 0.3739858865737915, "Left": 0.48920923471450806, "Top": 0.5900818109512329, "Width": 0.5097314119338989 }, "Polygon": [ { "X": 0.48475268483161926, "Y": 0.6823741793632507 }, { "X": 0.9601503014564514, "Y": 0.587857186794281 }, { "X": 0.9847385287284851, "Y": 0.8692590594291687 }, { "X": 0.5093409419059753, "Y": 0.9637760519981384 } ] }, "Id": 10, "ParentId": 4, "Type": "WORD" } ] }