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);
}
}
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;
}
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());
}
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());
}
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();
}
Aggregations