Search in sources :

Example 1 with Label

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

the class AWSRekognitionService method detectModerationLabels.

private List<Label> detectModerationLabels(ByteBuffer imageData) throws PredictionsException {
    DetectModerationLabelsRequest request = new DetectModerationLabelsRequest().withImage(new Image().withBytes(imageData));
    // Detect moderation labels in the given image via Amazon Rekognition
    final DetectModerationLabelsResult result;
    try {
        result = rekognition.detectModerationLabels(request);
    } catch (AmazonClientException serviceException) {
        throw new PredictionsException("Amazon Rekognition encountered an error while detecting moderation labels.", serviceException, "See attached service exception for more details.");
    }
    List<Label> labels = new ArrayList<>();
    for (ModerationLabel moderationLabel : result.getModerationLabels()) {
        Label label = Label.builder().value(moderationLabel.getName()).confidence(moderationLabel.getConfidence()).parentLabels(Collections.singletonList(moderationLabel.getParentName())).build();
        labels.add(label);
    }
    return labels;
}
Also used : ModerationLabel(com.amazonaws.services.rekognition.model.ModerationLabel) AmazonClientException(com.amazonaws.AmazonClientException) Label(com.amplifyframework.predictions.models.Label) ModerationLabel(com.amazonaws.services.rekognition.model.ModerationLabel) ArrayList(java.util.ArrayList) PredictionsException(com.amplifyframework.predictions.PredictionsException) Image(com.amazonaws.services.rekognition.model.Image) DetectModerationLabelsResult(com.amazonaws.services.rekognition.model.DetectModerationLabelsResult) DetectModerationLabelsRequest(com.amazonaws.services.rekognition.model.DetectModerationLabelsRequest)

Example 2 with Label

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

the class AWSPredictionsIdentifyLabelsTest method testIdentifyLabels.

/**
 * Assert label detection works.
 * @throws Exception if prediction fails
 */
@Test
public void testIdentifyLabels() throws Exception {
    final Bitmap image = Assets.readAsBitmap("jeff_bezos.jpg");
    // Identify the labels inside given image and assert non-null result.
    IdentifyLabelsResult result = (IdentifyLabelsResult) predictions.identify(LabelType.ALL, image);
    assertNotNull(result);
    // Assert that Jeff's portrait doesn't flag moderation :)
    assertFalse(result.isUnsafeContent());
    // Assert at least one label is detected as "Person"
    assertFalse(Empty.check(result.getLabels()));
    assertTrue(Observable.fromIterable(result.getLabels()).map(Label::getName).toList().blockingGet().contains("Person"));
}
Also used : Bitmap(android.graphics.Bitmap) Label(com.amplifyframework.predictions.models.Label) IdentifyLabelsResult(com.amplifyframework.predictions.result.IdentifyLabelsResult) Test(org.junit.Test)

Example 3 with Label

use of com.amplifyframework.predictions.models.Label 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)

Aggregations

Label (com.amplifyframework.predictions.models.Label)3 AmazonClientException (com.amazonaws.AmazonClientException)2 Image (com.amazonaws.services.rekognition.model.Image)2 ModerationLabel (com.amazonaws.services.rekognition.model.ModerationLabel)2 PredictionsException (com.amplifyframework.predictions.PredictionsException)2 ArrayList (java.util.ArrayList)2 Bitmap (android.graphics.Bitmap)1 RectF (android.graphics.RectF)1 DetectLabelsRequest (com.amazonaws.services.rekognition.model.DetectLabelsRequest)1 DetectLabelsResult (com.amazonaws.services.rekognition.model.DetectLabelsResult)1 DetectModerationLabelsRequest (com.amazonaws.services.rekognition.model.DetectModerationLabelsRequest)1 DetectModerationLabelsResult (com.amazonaws.services.rekognition.model.DetectModerationLabelsResult)1 Instance (com.amazonaws.services.rekognition.model.Instance)1 Parent (com.amazonaws.services.rekognition.model.Parent)1 IdentifyLabelsResult (com.amplifyframework.predictions.result.IdentifyLabelsResult)1 Test (org.junit.Test)1