Search in sources :

Example 1 with EntityDetails

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

the class AWSRekognitionService method detectEntities.

private List<EntityDetails> detectEntities(ByteBuffer imageData) throws PredictionsException {
    DetectFacesRequest request = new DetectFacesRequest().withImage(new Image().withBytes(imageData)).withAttributes(Attribute.ALL.toString());
    // Detect entities in the given image via Amazon Rekognition
    final DetectFacesResult result;
    try {
        result = rekognition.detectFaces(request);
    } catch (AmazonClientException serviceException) {
        throw new PredictionsException("Amazon Rekognition encountered an error while detecting faces.", serviceException, "See attached service exception for more details.");
    }
    List<EntityDetails> entities = new ArrayList<>();
    for (FaceDetail face : result.getFaceDetails()) {
        // Extract details from face detection
        RectF box = RekognitionResultTransformers.fromBoundingBox(face.getBoundingBox());
        AgeRange ageRange = RekognitionResultTransformers.fromRekognitionAgeRange(face.getAgeRange());
        Pose pose = RekognitionResultTransformers.fromRekognitionPose(face.getPose());
        List<Landmark> landmarks = RekognitionResultTransformers.fromLandmarks(face.getLandmarks());
        List<BinaryFeature> features = RekognitionResultTransformers.fromFaceDetail(face);
        // Gender detection
        com.amazonaws.services.rekognition.model.Gender rekognitionGender = face.getGender();
        Gender amplifyGender = Gender.builder().value(GenderBinaryTypeAdapter.fromRekognition(rekognitionGender.getValue())).confidence(rekognitionGender.getConfidence()).build();
        // Emotion detection
        List<Emotion> emotions = new ArrayList<>();
        for (com.amazonaws.services.rekognition.model.Emotion rekognitionEmotion : face.getEmotions()) {
            EmotionType emotion = EmotionTypeAdapter.fromRekognition(rekognitionEmotion.getType());
            Emotion amplifyEmotion = Emotion.builder().value(emotion).confidence(rekognitionEmotion.getConfidence()).build();
            emotions.add(amplifyEmotion);
        }
        Collections.sort(emotions, Collections.reverseOrder());
        EntityDetails entity = EntityDetails.builder().box(box).ageRange(ageRange).pose(pose).gender(amplifyGender).landmarks(landmarks).emotions(emotions).features(features).build();
        entities.add(entity);
    }
    return entities;
}
Also used : AgeRange(com.amplifyframework.predictions.models.AgeRange) AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) Gender(com.amplifyframework.predictions.models.Gender) Image(com.amazonaws.services.rekognition.model.Image) FaceDetail(com.amazonaws.services.rekognition.model.FaceDetail) EntityDetails(com.amplifyframework.predictions.models.EntityDetails) Pose(com.amplifyframework.predictions.models.Pose) PredictionsException(com.amplifyframework.predictions.PredictionsException) Emotion(com.amplifyframework.predictions.models.Emotion) Landmark(com.amplifyframework.predictions.models.Landmark) BinaryFeature(com.amplifyframework.predictions.models.BinaryFeature) RectF(android.graphics.RectF) EmotionType(com.amplifyframework.predictions.models.EmotionType) DetectFacesResult(com.amazonaws.services.rekognition.model.DetectFacesResult) DetectFacesRequest(com.amazonaws.services.rekognition.model.DetectFacesRequest)

Example 2 with EntityDetails

use of com.amplifyframework.predictions.models.EntityDetails 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 3 with EntityDetails

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

the class AWSPredictionsIdentifyEntitiesTest method testIdentifyEntities.

/**
 * Assert general entity detection works.
 * @throws Exception if prediction fails
 */
@Test
// Jeff Bezos' current age
@SuppressWarnings("MagicNumber")
public void testIdentifyEntities() throws Exception {
    final Bitmap image = Assets.readAsBitmap("jeff_bezos.jpg");
    // Identify the entity inside given image and assert non-null result.
    IdentifyEntitiesResult result = (IdentifyEntitiesResult) predictions.identify(TYPE, image);
    assertNotNull(result);
    // Assert that at least one entity is detected
    assertFalse(Empty.check(result.getEntities()));
    EntityDetails entity = result.getEntities().get(0);
    // Assert features from detected entity
    FeatureAssert.assertMatches(GenderBinaryType.MALE, entity.getGender());
    FeatureAssert.assertMatches(EmotionType.HAPPY, Collections.max(entity.getEmotions()));
    assertNotNull(entity.getAgeRange());
    int jeffAge = 56;
    // When this test was created, Rekognition returned an age range containing Jeff's age.  The algorithm
    // recently changed to predict an age range of 37-55.  The goal of this test is to verify a sane response,
    // not verify the accurate of Rekognition, so we will add a 20 year buffer to the age range (e.g. if age range
    // is 37 to 55, the test will pass because 56 is still between 37-20 (17) and 55+20 (75)).
    int buffer = 20;
    assertTrue(jeffAge <= entity.getAgeRange().getHigh() + buffer);
    assertTrue(jeffAge >= entity.getAgeRange().getLow() - buffer);
}
Also used : IdentifyEntitiesResult(com.amplifyframework.predictions.result.IdentifyEntitiesResult) Bitmap(android.graphics.Bitmap) EntityDetails(com.amplifyframework.predictions.models.EntityDetails) Test(org.junit.Test)

Aggregations

EntityDetails (com.amplifyframework.predictions.models.EntityDetails)3 PredictionsException (com.amplifyframework.predictions.PredictionsException)2 Bitmap (android.graphics.Bitmap)1 RectF (android.graphics.RectF)1 AmazonClientException (com.amazonaws.AmazonClientException)1 DetectFacesRequest (com.amazonaws.services.rekognition.model.DetectFacesRequest)1 DetectFacesResult (com.amazonaws.services.rekognition.model.DetectFacesResult)1 FaceDetail (com.amazonaws.services.rekognition.model.FaceDetail)1 Image (com.amazonaws.services.rekognition.model.Image)1 IdentifyEntitiesConfiguration (com.amplifyframework.predictions.aws.configuration.IdentifyEntitiesConfiguration)1 AgeRange (com.amplifyframework.predictions.models.AgeRange)1 BinaryFeature (com.amplifyframework.predictions.models.BinaryFeature)1 Emotion (com.amplifyframework.predictions.models.Emotion)1 EmotionType (com.amplifyframework.predictions.models.EmotionType)1 EntityMatch (com.amplifyframework.predictions.models.EntityMatch)1 Gender (com.amplifyframework.predictions.models.Gender)1 Landmark (com.amplifyframework.predictions.models.Landmark)1 Pose (com.amplifyframework.predictions.models.Pose)1 IdentifyEntitiesResult (com.amplifyframework.predictions.result.IdentifyEntitiesResult)1 ArrayList (java.util.ArrayList)1