Search in sources :

Example 1 with DetectTextResult

use of com.amazonaws.services.rekognition.model.DetectTextResult in project aws-doc-sdk-examples by awsdocs.

the class DetectText method main.

public static void main(String[] args) throws Exception {
    // Change the value of bucket to the S3 bucket that contains your image file.
    // Change the value of photo to your image file name.
    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();
    }
}
Also used : TextDetection(com.amazonaws.services.rekognition.model.TextDetection) DetectTextRequest(com.amazonaws.services.rekognition.model.DetectTextRequest) AmazonRekognitionException(com.amazonaws.services.rekognition.model.AmazonRekognitionException) AmazonRekognition(com.amazonaws.services.rekognition.AmazonRekognition) S3Object(com.amazonaws.services.rekognition.model.S3Object) Image(com.amazonaws.services.rekognition.model.Image) DetectTextResult(com.amazonaws.services.rekognition.model.DetectTextResult)

Example 2 with DetectTextResult

use of com.amazonaws.services.rekognition.model.DetectTextResult in project amplify-android by aws-amplify.

the class AWSRekognitionService method detectPlainText.

private IdentifyTextResult detectPlainText(ByteBuffer imageData) throws PredictionsException {
    DetectTextRequest request = new DetectTextRequest().withImage(new Image().withBytes(imageData));
    // Read text in the given image via Amazon Rekognition
    final DetectTextResult result;
    try {
        result = rekognition.detectText(request);
    } catch (AmazonClientException serviceException) {
        throw new PredictionsException("Amazon Rekognition encountered an error while detecting text.", serviceException, "See attached service exception for more details.");
    }
    StringBuilder fullTextBuilder = new StringBuilder();
    List<String> rawLineText = new ArrayList<>();
    List<IdentifiedText> words = new ArrayList<>();
    List<IdentifiedText> lines = new ArrayList<>();
    for (TextDetection detection : result.getTextDetections()) {
        TextTypes type = TextTypes.fromValue(detection.getType());
        switch(type) {
            case LINE:
                rawLineText.add(detection.getDetectedText());
                lines.add(RekognitionResultTransformers.fromTextDetection(detection));
                continue;
            case WORD:
                fullTextBuilder.append(detection.getDetectedText()).append(" ");
                words.add(RekognitionResultTransformers.fromTextDetection(detection));
                continue;
            default:
        }
    }
    return IdentifyTextResult.builder().fullText(fullTextBuilder.toString().trim()).rawLineText(rawLineText).lines(lines).words(words).build();
}
Also used : IdentifiedText(com.amplifyframework.predictions.models.IdentifiedText) AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) Image(com.amazonaws.services.rekognition.model.Image) DetectTextResult(com.amazonaws.services.rekognition.model.DetectTextResult) TextTypes(com.amazonaws.services.rekognition.model.TextTypes) TextDetection(com.amazonaws.services.rekognition.model.TextDetection) DetectTextRequest(com.amazonaws.services.rekognition.model.DetectTextRequest) PredictionsException(com.amplifyframework.predictions.PredictionsException)

Aggregations

DetectTextRequest (com.amazonaws.services.rekognition.model.DetectTextRequest)2 DetectTextResult (com.amazonaws.services.rekognition.model.DetectTextResult)2 Image (com.amazonaws.services.rekognition.model.Image)2 TextDetection (com.amazonaws.services.rekognition.model.TextDetection)2 AmazonClientException (com.amazonaws.AmazonClientException)1 AmazonRekognition (com.amazonaws.services.rekognition.AmazonRekognition)1 AmazonRekognitionException (com.amazonaws.services.rekognition.model.AmazonRekognitionException)1 S3Object (com.amazonaws.services.rekognition.model.S3Object)1 TextTypes (com.amazonaws.services.rekognition.model.TextTypes)1 PredictionsException (com.amplifyframework.predictions.PredictionsException)1 IdentifiedText (com.amplifyframework.predictions.models.IdentifiedText)1 ArrayList (java.util.ArrayList)1