Search in sources :

Example 1 with TextTypes

use of com.amazonaws.services.rekognition.model.TextTypes 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

AmazonClientException (com.amazonaws.AmazonClientException)1 DetectTextRequest (com.amazonaws.services.rekognition.model.DetectTextRequest)1 DetectTextResult (com.amazonaws.services.rekognition.model.DetectTextResult)1 Image (com.amazonaws.services.rekognition.model.Image)1 TextDetection (com.amazonaws.services.rekognition.model.TextDetection)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