または DetectText で を使用する AWS SDK CLI - AWS SDK CLI コードの例

AWS Doc SDK Examples GitHub リポジトリには他にも AWS SDK例があります。

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

または DetectText で を使用する AWS SDK CLI

以下のコード例は、DetectText の使用方法を示しています。

詳細については、「イメージ内のテキストを検出する」を参照してください。

.NET
AWS SDK for .NET
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

using System; using System.Threading.Tasks; using Amazon.Rekognition; using Amazon.Rekognition.Model; /// <summary> /// Uses the Amazon Rekognition Service to detect text in an image. The /// example was created using the AWS SDK for .NET version 3.7 and .NET /// Core 5.0. /// </summary> public class DetectText { public static async Task Main() { string photo = "Dad_photographer.jpg"; // "input.jpg"; string bucket = "igsmiths3photos"; // "bucket"; var rekognitionClient = new AmazonRekognitionClient(); var detectTextRequest = new DetectTextRequest() { Image = new Image() { S3Object = new S3Object() { Name = photo, Bucket = bucket, }, }, }; try { DetectTextResponse detectTextResponse = await rekognitionClient.DetectTextAsync(detectTextRequest); Console.WriteLine($"Detected lines and words for {photo}"); detectTextResponse.TextDetections.ForEach(text => { 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); } } }
  • API 詳細については、「 リファレンスDetectText」の「」を参照してください。 AWS SDK for .NET API

CLI
AWS CLI

画像内のテキストを検出するには

次の detect-text コマンドは、指定された画像内のテキストを検出します。

aws rekognition detect-text \ --image '{"S3Object":{"Bucket":"MyImageS3Bucket","Name":"ExamplePicture.jpg"}}'

出力:

{ "TextDetections": [ { "Geometry": { "BoundingBox": { "Width": 0.24624845385551453, "Top": 0.28288066387176514, "Left": 0.391388863325119, "Height": 0.022687450051307678 }, "Polygon": [ { "Y": 0.28288066387176514, "X": 0.391388863325119 }, { "Y": 0.2826388478279114, "X": 0.6376373171806335 }, { "Y": 0.30532628297805786, "X": 0.637677013874054 }, { "Y": 0.305568128824234, "X": 0.39142853021621704 } ] }, "Confidence": 94.35709381103516, "DetectedText": "ESTD 1882", "Type": "LINE", "Id": 0 }, { "Geometry": { "BoundingBox": { "Width": 0.33933889865875244, "Top": 0.32603850960731506, "Left": 0.34534579515457153, "Height": 0.07126858830451965 }, "Polygon": [ { "Y": 0.32603850960731506, "X": 0.34534579515457153 }, { "Y": 0.32633158564567566, "X": 0.684684693813324 }, { "Y": 0.3976001739501953, "X": 0.684575080871582 }, { "Y": 0.3973070979118347, "X": 0.345236212015152 } ] }, "Confidence": 99.95779418945312, "DetectedText": "BRAINS", "Type": "LINE", "Id": 1 }, { "Confidence": 97.22098541259766, "Geometry": { "BoundingBox": { "Width": 0.061079490929841995, "Top": 0.2843210697174072, "Left": 0.391391396522522, "Height": 0.021029088646173477 }, "Polygon": [ { "Y": 0.2843210697174072, "X": 0.391391396522522 }, { "Y": 0.2828207015991211, "X": 0.4524524509906769 }, { "Y": 0.3038259446620941, "X": 0.4534534513950348 }, { "Y": 0.30532634258270264, "X": 0.3923923969268799 } ] }, "DetectedText": "ESTD", "ParentId": 0, "Type": "WORD", "Id": 2 }, { "Confidence": 91.49320983886719, "Geometry": { "BoundingBox": { "Width": 0.07007007300853729, "Top": 0.2828207015991211, "Left": 0.5675675868988037, "Height": 0.02250562608242035 }, "Polygon": [ { "Y": 0.2828207015991211, "X": 0.5675675868988037 }, { "Y": 0.2828207015991211, "X": 0.6376376152038574 }, { "Y": 0.30532634258270264, "X": 0.6376376152038574 }, { "Y": 0.30532634258270264, "X": 0.5675675868988037 } ] }, "DetectedText": "1882", "ParentId": 0, "Type": "WORD", "Id": 3 }, { "Confidence": 99.95779418945312, "Geometry": { "BoundingBox": { "Width": 0.33933934569358826, "Top": 0.32633158564567566, "Left": 0.3453453481197357, "Height": 0.07127484679222107 }, "Polygon": [ { "Y": 0.32633158564567566, "X": 0.3453453481197357 }, { "Y": 0.32633158564567566, "X": 0.684684693813324 }, { "Y": 0.39759939908981323, "X": 0.6836836934089661 }, { "Y": 0.39684921503067017, "X": 0.3453453481197357 } ] }, "DetectedText": "BRAINS", "ParentId": 1, "Type": "WORD", "Id": 4 } ] }
  • API 詳細については、「 コマンドリファレンスDetectText」の「」を参照してください。 AWS CLI

Java
SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

import software.amazon.awssdk.core.SdkBytes; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.rekognition.RekognitionClient; import software.amazon.awssdk.services.rekognition.model.DetectTextRequest; import software.amazon.awssdk.services.rekognition.model.Image; import software.amazon.awssdk.services.rekognition.model.DetectTextResponse; import software.amazon.awssdk.services.rekognition.model.TextDetection; import software.amazon.awssdk.services.rekognition.model.RekognitionException; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DetectText { public static void main(String[] args) { final String usage = """ Usage: <sourceImage> Where: sourceImage - The path to the image that contains text (for example, C:\\AWS\\pic1.png).\s """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String sourceImage = args[0]; Region region = Region.US_EAST_1; RekognitionClient rekClient = RekognitionClient.builder() .region(region) .build(); detectTextLabels(rekClient, sourceImage); rekClient.close(); } public static void detectTextLabels(RekognitionClient rekClient, String sourceImage) { try { InputStream sourceStream = new FileInputStream(sourceImage); SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream); 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); } } }
  • API 詳細については、「 リファレンスDetectText」の「」を参照してください。 AWS SDK for Java 2.x API

Kotlin
SDK Kotlin 用
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

suspend fun detectTextLabels(sourceImage: String?) { val souImage = Image { bytes = (File(sourceImage).readBytes()) } val request = DetectTextRequest { image = souImage } RekognitionClient { region = "us-east-1" }.use { rekClient -> val response = rekClient.detectText(request) response.textDetections?.forEach { text -> println("Detected: ${text.detectedText}") println("Confidence: ${text.confidence}") println("Id: ${text.id}") println("Parent Id: ${text.parentId}") println("Type: ${text.type}") } } }
  • API 詳細については、Kotlin リファレンス DetectTextの「」の「」を参照してください。 AWS SDK API

Python
SDK for Python (Boto3)
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

class RekognitionImage: """ Encapsulates an Amazon Rekognition image. This class is a thin wrapper around parts of the Boto3 Amazon Rekognition API. """ def __init__(self, image, image_name, rekognition_client): """ Initializes the image object. :param image: Data that defines the image, either the image bytes or an Amazon S3 bucket and object key. :param image_name: The name of the image. :param rekognition_client: A Boto3 Rekognition client. """ self.image = image self.image_name = image_name self.rekognition_client = rekognition_client def detect_text(self): """ Detects text in the image. :return The list of text elements found in the image. """ try: response = self.rekognition_client.detect_text(Image=self.image) texts = [RekognitionText(text) for text in response["TextDetections"]] logger.info("Found %s texts in %s.", len(texts), self.image_name) except ClientError: logger.exception("Couldn't detect text in %s.", self.image_name) raise else: return texts
  • API 詳細については、「 for Python (Boto3) リファレンスDetectText」の「」を参照してください。 AWS SDK API