Search in sources :

Example 1 with BinaryFeature

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

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

the class RekognitionResultTransformersTest method testFaceDetailConversion.

/**
 * Tests that the individual attributes from Rekognition
 * face detail are properly converted to a list of binary
 * features that are compatible with Amplify's entity
 * details.
 */
@Test
public void testFaceDetailConversion() {
    FaceDetail faceDetail = new FaceDetail().withBeard(new Beard().withValue(random.nextBoolean()).withConfidence(random.nextFloat())).withSunglasses(new Sunglasses().withValue(random.nextBoolean()).withConfidence(random.nextFloat())).withSmile(new Smile().withValue(random.nextBoolean()).withConfidence(random.nextFloat())).withEyeglasses(new Eyeglasses().withValue(random.nextBoolean()).withConfidence(random.nextFloat())).withMustache(new Mustache().withValue(random.nextBoolean()).withConfidence(random.nextFloat())).withMouthOpen(new MouthOpen().withValue(random.nextBoolean()).withConfidence(random.nextFloat())).withEyesOpen(new EyeOpen().withValue(random.nextBoolean()).withConfidence(random.nextFloat()));
    List<BinaryFeature> features = RekognitionResultTransformers.fromFaceDetail(faceDetail);
    FeatureAssert.assertMatches(Arrays.asList(faceDetail.getBeard().getValue(), faceDetail.getSunglasses().getValue(), faceDetail.getSmile().getValue(), faceDetail.getEyeglasses().getValue(), faceDetail.getMustache().getValue(), faceDetail.getMouthOpen().getValue(), faceDetail.getEyesOpen().getValue()), features);
}
Also used : Beard(com.amazonaws.services.rekognition.model.Beard) MouthOpen(com.amazonaws.services.rekognition.model.MouthOpen) FaceDetail(com.amazonaws.services.rekognition.model.FaceDetail) EyeOpen(com.amazonaws.services.rekognition.model.EyeOpen) Mustache(com.amazonaws.services.rekognition.model.Mustache) BinaryFeature(com.amplifyframework.predictions.models.BinaryFeature) Smile(com.amazonaws.services.rekognition.model.Smile) Eyeglasses(com.amazonaws.services.rekognition.model.Eyeglasses) Sunglasses(com.amazonaws.services.rekognition.model.Sunglasses) Test(org.junit.Test)

Aggregations

FaceDetail (com.amazonaws.services.rekognition.model.FaceDetail)2 BinaryFeature (com.amplifyframework.predictions.models.BinaryFeature)2 RectF (android.graphics.RectF)1 AmazonClientException (com.amazonaws.AmazonClientException)1 Beard (com.amazonaws.services.rekognition.model.Beard)1 DetectFacesRequest (com.amazonaws.services.rekognition.model.DetectFacesRequest)1 DetectFacesResult (com.amazonaws.services.rekognition.model.DetectFacesResult)1 EyeOpen (com.amazonaws.services.rekognition.model.EyeOpen)1 Eyeglasses (com.amazonaws.services.rekognition.model.Eyeglasses)1 Image (com.amazonaws.services.rekognition.model.Image)1 MouthOpen (com.amazonaws.services.rekognition.model.MouthOpen)1 Mustache (com.amazonaws.services.rekognition.model.Mustache)1 Smile (com.amazonaws.services.rekognition.model.Smile)1 Sunglasses (com.amazonaws.services.rekognition.model.Sunglasses)1 PredictionsException (com.amplifyframework.predictions.PredictionsException)1 AgeRange (com.amplifyframework.predictions.models.AgeRange)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