Search in sources :

Example 21 with PredictionsException

use of com.amplifyframework.predictions.PredictionsException in project amplify-android by aws-amplify.

the class AWSRekognitionService method detectEntities.

void detectEntities(@NonNull ByteBuffer imageData, @NonNull Consumer<IdentifyResult> onSuccess, @NonNull Consumer<PredictionsException> onError) {
    final IdentifyEntitiesConfiguration config;
    try {
        config = pluginConfiguration.getIdentifyEntitiesConfiguration();
        if (config.isGeneralEntityDetection()) {
            List<EntityDetails> entities = detectEntities(imageData);
            onSuccess.accept(IdentifyEntitiesResult.fromEntityDetails(entities));
        } else {
            int maxEntities = config.getMaxEntities();
            String collectionId = config.getCollectionId();
            List<EntityMatch> matches = detectEntityMatches(imageData, maxEntities, collectionId);
            onSuccess.accept(IdentifyEntityMatchesResult.fromEntityMatches(matches));
        }
    } catch (PredictionsException exception) {
        onError.accept(exception);
    }
}
Also used : IdentifyEntitiesConfiguration(com.amplifyframework.predictions.aws.configuration.IdentifyEntitiesConfiguration) EntityDetails(com.amplifyframework.predictions.models.EntityDetails) EntityMatch(com.amplifyframework.predictions.models.EntityMatch) PredictionsException(com.amplifyframework.predictions.PredictionsException)

Example 22 with PredictionsException

use of com.amplifyframework.predictions.PredictionsException in project amplify-android by aws-amplify.

the class AWSRekognitionService method detectLabels.

private List<Label> detectLabels(ByteBuffer imageData) throws PredictionsException {
    DetectLabelsRequest request = new DetectLabelsRequest().withImage(new Image().withBytes(imageData));
    // Detect labels in the given image via Amazon Rekognition
    final DetectLabelsResult result;
    try {
        result = rekognition.detectLabels(request);
    } catch (AmazonClientException serviceException) {
        throw new PredictionsException("Amazon Rekognition encountered an error while detecting labels.", serviceException, "See attached service exception for more details.");
    }
    List<Label> labels = new ArrayList<>();
    for (com.amazonaws.services.rekognition.model.Label rekognitionLabel : result.getLabels()) {
        List<String> parents = new ArrayList<>();
        for (Parent parent : rekognitionLabel.getParents()) {
            parents.add(parent.getName());
        }
        List<RectF> boxes = new ArrayList<>();
        for (Instance instance : rekognitionLabel.getInstances()) {
            boxes.add(RekognitionResultTransformers.fromBoundingBox(instance.getBoundingBox()));
        }
        Label amplifyLabel = Label.builder().value(rekognitionLabel.getName()).confidence(rekognitionLabel.getConfidence()).parentLabels(parents).boxes(boxes).build();
        labels.add(amplifyLabel);
    }
    return labels;
}
Also used : Parent(com.amazonaws.services.rekognition.model.Parent) Instance(com.amazonaws.services.rekognition.model.Instance) AmazonClientException(com.amazonaws.AmazonClientException) Label(com.amplifyframework.predictions.models.Label) ModerationLabel(com.amazonaws.services.rekognition.model.ModerationLabel) ArrayList(java.util.ArrayList) Image(com.amazonaws.services.rekognition.model.Image) DetectLabelsResult(com.amazonaws.services.rekognition.model.DetectLabelsResult) RectF(android.graphics.RectF) DetectLabelsRequest(com.amazonaws.services.rekognition.model.DetectLabelsRequest) PredictionsException(com.amplifyframework.predictions.PredictionsException)

Example 23 with PredictionsException

use of com.amplifyframework.predictions.PredictionsException in project amplify-android by aws-amplify.

the class AWSTextractService method analyzeDocument.

private IdentifyDocumentTextResult analyzeDocument(ByteBuffer imageData, List<String> features) throws PredictionsException {
    AnalyzeDocumentRequest request = new AnalyzeDocumentRequest().withDocument(new Document().withBytes(imageData)).withFeatureTypes(features);
    // Analyze document from given image via Amazon Textract
    final AnalyzeDocumentResult result;
    try {
        result = textract.analyzeDocument(request);
    } catch (AmazonClientException serviceException) {
        throw new PredictionsException("AWS Textract encountered an error while analyzing document.", serviceException, "See attached service exception for more details.");
    }
    return processTextractBlocks(result.getBlocks());
}
Also used : AnalyzeDocumentResult(com.amazonaws.services.textract.model.AnalyzeDocumentResult) AmazonClientException(com.amazonaws.AmazonClientException) PredictionsException(com.amplifyframework.predictions.PredictionsException) Document(com.amazonaws.services.textract.model.Document) AnalyzeDocumentRequest(com.amazonaws.services.textract.model.AnalyzeDocumentRequest)

Example 24 with PredictionsException

use of com.amplifyframework.predictions.PredictionsException in project amplify-android by aws-amplify.

the class AWSTextractService method detectDocumentText.

private IdentifyDocumentTextResult detectDocumentText(ByteBuffer imageData) throws PredictionsException {
    DetectDocumentTextRequest request = new DetectDocumentTextRequest().withDocument(new Document().withBytes(imageData));
    // Extract text from given image via Amazon Textract
    final DetectDocumentTextResult result;
    try {
        result = textract.detectDocumentText(request);
    } catch (AmazonClientException serviceException) {
        throw new PredictionsException("AWS Textract encountered an error while detecting document text.", serviceException, "See attached service exception for more details.");
    }
    return processTextractBlocks(result.getBlocks());
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) DetectDocumentTextResult(com.amazonaws.services.textract.model.DetectDocumentTextResult) PredictionsException(com.amplifyframework.predictions.PredictionsException) DetectDocumentTextRequest(com.amazonaws.services.textract.model.DetectDocumentTextRequest) Document(com.amazonaws.services.textract.model.Document)

Example 25 with PredictionsException

use of com.amplifyframework.predictions.PredictionsException in project amplify-android by aws-amplify.

the class AWSTranslateService method fetchTranslation.

private TranslateTextResult fetchTranslation(String text, LanguageType sourceLanguage, LanguageType targetLanguage) throws PredictionsException {
    // Throw if default language is not configured
    LanguageType source = !LanguageType.UNKNOWN.equals(sourceLanguage) ? sourceLanguage : pluginConfiguration.getTranslateTextConfiguration().getSourceLanguage();
    LanguageType target = !LanguageType.UNKNOWN.equals(targetLanguage) ? targetLanguage : pluginConfiguration.getTranslateTextConfiguration().getTargetLanguage();
    TranslateTextRequest request = new TranslateTextRequest().withText(text).withSourceLanguageCode(source.getLanguageCode()).withTargetLanguageCode(target.getLanguageCode());
    // Translate given text via AWS Translate
    final com.amazonaws.services.translate.model.TranslateTextResult result;
    try {
        result = translate.translateText(request);
    } catch (AmazonClientException serviceException) {
        throw new PredictionsException("AWS Translate encountered an error while translating text.", serviceException, "See attached service exception for more details.");
    }
    String translation = result.getTranslatedText();
    String targetCode = result.getTargetLanguageCode();
    LanguageType language = LanguageType.from(targetCode);
    return TranslateTextResult.builder().translatedText(translation).targetLanguage(language).build();
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) TranslateTextRequest(com.amazonaws.services.translate.model.TranslateTextRequest) PredictionsException(com.amplifyframework.predictions.PredictionsException) LanguageType(com.amplifyframework.predictions.models.LanguageType)

Aggregations

PredictionsException (com.amplifyframework.predictions.PredictionsException)32 AmazonClientException (com.amazonaws.AmazonClientException)15 ArrayList (java.util.ArrayList)9 Image (com.amazonaws.services.rekognition.model.Image)6 Test (org.junit.Test)5 RectF (android.graphics.RectF)4 IdentifyEntitiesConfiguration (com.amplifyframework.predictions.aws.configuration.IdentifyEntitiesConfiguration)3 Sentiment (com.amplifyframework.predictions.models.Sentiment)3 InputStream (java.io.InputStream)3 DominantLanguage (com.amazonaws.services.comprehend.model.DominantLanguage)2 ComparedFace (com.amazonaws.services.rekognition.model.ComparedFace)2 ModerationLabel (com.amazonaws.services.rekognition.model.ModerationLabel)2 Document (com.amazonaws.services.textract.model.Document)2 SpeechGeneratorConfiguration (com.amplifyframework.predictions.aws.configuration.SpeechGeneratorConfiguration)2 CelebrityDetails (com.amplifyframework.predictions.models.CelebrityDetails)2 KeyPhrase (com.amplifyframework.predictions.models.KeyPhrase)2 Landmark (com.amplifyframework.predictions.models.Landmark)2 LanguageType (com.amplifyframework.predictions.models.LanguageType)2 Pose (com.amplifyframework.predictions.models.Pose)2 Syntax (com.amplifyframework.predictions.models.Syntax)2