Use DetectText with an AWS SDK or CLI - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Use DetectText with an AWS SDK or CLI

The following code examples show how to use DetectText.

For more information, see Detecting text in an image.

.NET
AWS SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

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 = "amzn-s3-demo-bucket"; // "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); } } }
  • For API details, see DetectText in AWS SDK for .NET API Reference.

CLI
AWS CLI

To detect text in an image

The following detect-text command detects text in the specified image.

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

Output:

{ "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 } ] }
  • For API details, see DetectText in AWS CLI Command Reference.

Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

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); } } }
  • For API details, see DetectText in AWS SDK for Java 2.x API Reference.

Kotlin
SDK for Kotlin
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

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}") } } }
  • For API details, see DetectText in AWS SDK for Kotlin API reference.

Python
SDK for Python (Boto3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

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
  • For API details, see DetectText in AWS SDK for Python (Boto3) API Reference.