Search in sources :

Example 1 with AgeRange

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

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

the class RekognitionResultTransformersTest method testAgeRangeConversion.

/**
 * Tests that the age range from Rekognition is converted
 * to an equivalent Amplify age range.
 */
@Test
public void testAgeRangeConversion() {
    int low = random.nextInt();
    int high = random.nextInt();
    // high cannot be lower than low
    if (low > high) {
        int temp = high;
        high = low;
        low = temp;
    }
    com.amazonaws.services.rekognition.model.AgeRange rekognitionAgeRange = new com.amazonaws.services.rekognition.model.AgeRange().withHigh(high).withLow(low);
    AgeRange amplifyAgeRange = RekognitionResultTransformers.fromRekognitionAgeRange(rekognitionAgeRange);
    assertEquals(rekognitionAgeRange.getHigh().intValue(), amplifyAgeRange.getHigh());
    assertEquals(rekognitionAgeRange.getLow().intValue(), amplifyAgeRange.getLow());
}
Also used : AgeRange(com.amplifyframework.predictions.models.AgeRange) Point(com.amazonaws.services.rekognition.model.Point) Test(org.junit.Test)

Aggregations

AgeRange (com.amplifyframework.predictions.models.AgeRange)2 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 Point (com.amazonaws.services.rekognition.model.Point)1 PredictionsException (com.amplifyframework.predictions.PredictionsException)1 BinaryFeature (com.amplifyframework.predictions.models.BinaryFeature)1 Emotion (com.amplifyframework.predictions.models.Emotion)1 EmotionType (com.amplifyframework.predictions.models.EmotionType)1 EntityDetails (com.amplifyframework.predictions.models.EntityDetails)1 Gender (com.amplifyframework.predictions.models.Gender)1 Landmark (com.amplifyframework.predictions.models.Landmark)1 Pose (com.amplifyframework.predictions.models.Pose)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1