Search in sources :

Example 16 with PredictionsException

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

the class AWSComprehendService method fetchKeyPhrases.

private List<KeyPhrase> fetchKeyPhrases(String text, LanguageType language) throws PredictionsException {
    // Skip if configuration specifies NOT key phrase
    if (!isResourceConfigured(InterpretTextConfiguration.InterpretType.KEY_PHRASES)) {
        return null;
    }
    DetectKeyPhrasesRequest request = new DetectKeyPhrasesRequest().withText(text).withLanguageCode(language.getLanguageCode());
    // Detect key phrases from given text via AWS Comprehend
    final DetectKeyPhrasesResult result;
    try {
        result = comprehend.detectKeyPhrases(request);
    } catch (AmazonClientException serviceException) {
        throw new PredictionsException("AWS Comprehend encountered an error while detecting key phrases.", serviceException, "See attached service exception for more details.");
    }
    // Convert AWS Comprehend's detection result to Amplify-compatible format
    List<KeyPhrase> keyPhrases = new ArrayList<>();
    for (com.amazonaws.services.comprehend.model.KeyPhrase comprehendKeyPhrase : result.getKeyPhrases()) {
        KeyPhrase amplifyKeyPhrase = KeyPhrase.builder().value(comprehendKeyPhrase.getText()).confidence(comprehendKeyPhrase.getScore() * PERCENT).targetText(comprehendKeyPhrase.getText()).startIndex(comprehendKeyPhrase.getBeginOffset()).build();
        keyPhrases.add(amplifyKeyPhrase);
    }
    return keyPhrases;
}
Also used : DetectKeyPhrasesRequest(com.amazonaws.services.comprehend.model.DetectKeyPhrasesRequest) DetectKeyPhrasesResult(com.amazonaws.services.comprehend.model.DetectKeyPhrasesResult) AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) PredictionsException(com.amplifyframework.predictions.PredictionsException) KeyPhrase(com.amplifyframework.predictions.models.KeyPhrase)

Example 17 with PredictionsException

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

the class AWSComprehendService method fetchSyntax.

private List<Syntax> fetchSyntax(String text, LanguageType language) throws PredictionsException {
    // Skip if configuration specifies NOT syntax
    if (!isResourceConfigured(InterpretTextConfiguration.InterpretType.SYNTAX)) {
        return null;
    }
    DetectSyntaxRequest request = new DetectSyntaxRequest().withText(text).withLanguageCode(language.getLanguageCode());
    // Detect syntax from given text via AWS Comprehend
    final DetectSyntaxResult result;
    try {
        result = comprehend.detectSyntax(request);
    } catch (AmazonClientException serviceException) {
        throw new PredictionsException("AWS Comprehend encountered an error while detecting syntax.", serviceException, "See attached service exception for more details.");
    }
    // Convert AWS Comprehend's detection result to Amplify-compatible format
    List<Syntax> syntaxTokens = new ArrayList<>();
    for (com.amazonaws.services.comprehend.model.SyntaxToken comprehendSyntax : result.getSyntaxTokens()) {
        PartOfSpeechTag comprehendPartOfSpeech = comprehendSyntax.getPartOfSpeech();
        SpeechType partOfSpeech = SpeechTypeAdapter.fromComprehend(comprehendPartOfSpeech.getTag());
        Syntax amplifySyntax = Syntax.builder().id(comprehendSyntax.getTokenId().toString()).value(partOfSpeech).confidence(comprehendPartOfSpeech.getScore() * PERCENT).targetText(comprehendSyntax.getText()).startIndex(comprehendSyntax.getBeginOffset()).build();
        syntaxTokens.add(amplifySyntax);
    }
    return syntaxTokens;
}
Also used : PartOfSpeechTag(com.amazonaws.services.comprehend.model.PartOfSpeechTag) SpeechType(com.amplifyframework.predictions.models.SpeechType) DetectSyntaxRequest(com.amazonaws.services.comprehend.model.DetectSyntaxRequest) DetectSyntaxResult(com.amazonaws.services.comprehend.model.DetectSyntaxResult) AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) PredictionsException(com.amplifyframework.predictions.PredictionsException) Syntax(com.amplifyframework.predictions.models.Syntax)

Example 18 with PredictionsException

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

the class AWSPollyService method synthesizeSpeech.

private InputStream synthesizeSpeech(String text, AWSVoiceType voiceType) throws PredictionsException {
    final String languageCode;
    final String voiceId;
    if (AWSVoiceType.UNKNOWN.equals(voiceType)) {
        // Obtain voice + language from plugin configuration by default
        SpeechGeneratorConfiguration config = pluginConfiguration.getSpeechGeneratorConfiguration();
        languageCode = config.getLanguage();
        voiceId = config.getVoice();
    } else {
        // Override configuration defaults if explicitly specified in the options
        languageCode = voiceType.getLanguageCode();
        voiceId = voiceType.getName();
    }
    SynthesizeSpeechRequest request = new SynthesizeSpeechRequest().withText(text).withTextType(TextType.Text).withLanguageCode(languageCode).withVoiceId(voiceId).withOutputFormat(OutputFormat.Mp3).withSampleRate(Integer.toString(MP3_SAMPLE_RATE));
    // Synthesize speech from given text via Amazon Polly
    final SynthesizeSpeechResult result;
    try {
        result = polly.synthesizeSpeech(request);
    } catch (AmazonClientException serviceException) {
        throw new PredictionsException("AWS Polly encountered an error while synthesizing speech.", serviceException, "See attached service exception for more details.");
    }
    return result.getAudioStream();
}
Also used : SynthesizeSpeechRequest(com.amazonaws.services.polly.model.SynthesizeSpeechRequest) SpeechGeneratorConfiguration(com.amplifyframework.predictions.aws.configuration.SpeechGeneratorConfiguration) AmazonClientException(com.amazonaws.AmazonClientException) PredictionsException(com.amplifyframework.predictions.PredictionsException) SynthesizeSpeechResult(com.amazonaws.services.polly.model.SynthesizeSpeechResult)

Example 19 with PredictionsException

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

the class AWSPredictionsService method detectLabels.

/**
 * Delegate to {@link AWSRekognitionService} to detect labels.
 * @param type the type of labels to detect
 * @param imageData the image data
 * @param onSuccess triggered upon successful result
 * @param onError triggered upon encountering error
 */
public void detectLabels(@NonNull IdentifyAction type, @NonNull ByteBuffer imageData, @NonNull Consumer<IdentifyResult> onSuccess, @NonNull Consumer<PredictionsException> onError) {
    final LabelType labelType;
    try {
        labelType = getLabelType(type);
    } catch (PredictionsException error) {
        onError.accept(error);
        return;
    }
    rekognitionService.detectLabels(labelType, imageData, onSuccess, onError);
}
Also used : LabelType(com.amplifyframework.predictions.models.LabelType) PredictionsException(com.amplifyframework.predictions.PredictionsException)

Example 20 with PredictionsException

use of com.amplifyframework.predictions.PredictionsException 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

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