Search in sources :

Example 1 with Landmark

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

the class AWSRekognitionService method detectCelebrities.

private List<CelebrityDetails> detectCelebrities(ByteBuffer imageData) throws PredictionsException {
    RecognizeCelebritiesRequest request = new RecognizeCelebritiesRequest().withImage(new Image().withBytes(imageData));
    // Recognize celebrities in the given image via Amazon Rekognition
    final RecognizeCelebritiesResult result;
    try {
        result = rekognition.recognizeCelebrities(request);
    } catch (AmazonClientException serviceException) {
        throw new PredictionsException("Amazon Rekognition encountered an error while recognizing celebrities.", serviceException, "See attached service exception for more details.");
    }
    List<CelebrityDetails> celebrities = new ArrayList<>();
    for (com.amazonaws.services.rekognition.model.Celebrity rekognitionCelebrity : result.getCelebrityFaces()) {
        Celebrity amplifyCelebrity = Celebrity.builder().id(rekognitionCelebrity.getId()).value(rekognitionCelebrity.getName()).confidence(rekognitionCelebrity.getMatchConfidence()).build();
        // Get face-specific celebrity details from the result
        ComparedFace face = rekognitionCelebrity.getFace();
        RectF box = RekognitionResultTransformers.fromBoundingBox(face.getBoundingBox());
        Pose pose = RekognitionResultTransformers.fromRekognitionPose(face.getPose());
        List<Landmark> landmarks = RekognitionResultTransformers.fromLandmarks(face.getLandmarks());
        // Get URL links that are relevant to celebrities
        List<URL> urls = new ArrayList<>();
        for (String url : rekognitionCelebrity.getUrls()) {
            try {
                urls.add(new URL(url));
            } catch (MalformedURLException badUrl) {
            // Ignore bad URL
            }
        }
        CelebrityDetails details = CelebrityDetails.builder().celebrity(amplifyCelebrity).box(box).pose(pose).landmarks(landmarks).urls(urls).build();
        celebrities.add(details);
    }
    return celebrities;
}
Also used : MalformedURLException(java.net.MalformedURLException) CelebrityDetails(com.amplifyframework.predictions.models.CelebrityDetails) AmazonClientException(com.amazonaws.AmazonClientException) Landmark(com.amplifyframework.predictions.models.Landmark) ArrayList(java.util.ArrayList) Image(com.amazonaws.services.rekognition.model.Image) ComparedFace(com.amazonaws.services.rekognition.model.ComparedFace) URL(java.net.URL) RectF(android.graphics.RectF) RecognizeCelebritiesResult(com.amazonaws.services.rekognition.model.RecognizeCelebritiesResult) RecognizeCelebritiesRequest(com.amazonaws.services.rekognition.model.RecognizeCelebritiesRequest) Celebrity(com.amplifyframework.predictions.models.Celebrity) Pose(com.amplifyframework.predictions.models.Pose) PredictionsException(com.amplifyframework.predictions.PredictionsException)

Example 2 with Landmark

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

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

the class RekognitionResultTransformersTest method testLandmarksConversion.

/**
 * Tests that the landmarks from Rekognition are converted
 * from a list of individual points to a list of Amplify
 * landmarks that are mapped by their types.
 */
@Test
public void testLandmarksConversion() {
    com.amazonaws.services.rekognition.model.Landmark leftEyeDown = new com.amazonaws.services.rekognition.model.Landmark().withType(com.amazonaws.services.rekognition.model.LandmarkType.LeftEyeDown).withX(random.nextFloat()).withY(random.nextFloat());
    com.amazonaws.services.rekognition.model.Landmark leftEyeRight = new com.amazonaws.services.rekognition.model.Landmark().withType(com.amazonaws.services.rekognition.model.LandmarkType.LeftEyeRight).withX(random.nextFloat()).withY(random.nextFloat());
    com.amazonaws.services.rekognition.model.Landmark mouthDown = new com.amazonaws.services.rekognition.model.Landmark().withType(com.amazonaws.services.rekognition.model.LandmarkType.MouthDown).withX(random.nextFloat()).withY(random.nextFloat());
    List<com.amazonaws.services.rekognition.model.Landmark> rekognitionLandmarks = Arrays.asList(leftEyeDown, leftEyeRight, mouthDown);
    List<Landmark> amplifyLandmarks = RekognitionResultTransformers.fromLandmarks(rekognitionLandmarks);
    Map<LandmarkType, List<PointF>> map = new HashMap<>();
    for (Landmark landmark : amplifyLandmarks) {
        map.put(landmark.getType(), landmark.getPoints());
    }
    assertEquals(map.keySet(), new HashSet<>(Arrays.asList(LandmarkType.ALL_POINTS, LandmarkType.LEFT_EYE, LandmarkType.OUTER_LIPS)));
    assertTrue(map.get(LandmarkType.ALL_POINTS).containsAll(Arrays.asList(new PointF(leftEyeDown.getX(), leftEyeDown.getY()), new PointF(leftEyeRight.getX(), leftEyeRight.getY()), new PointF(mouthDown.getX(), mouthDown.getY()))));
    assertTrue(map.get(LandmarkType.LEFT_EYE).containsAll(Arrays.asList(new PointF(leftEyeDown.getX(), leftEyeDown.getY()), new PointF(leftEyeRight.getX(), leftEyeRight.getY()))));
    assertTrue(map.get(LandmarkType.OUTER_LIPS).contains(new PointF(mouthDown.getX(), mouthDown.getY())));
}
Also used : HashMap(java.util.HashMap) Landmark(com.amplifyframework.predictions.models.Landmark) PointF(android.graphics.PointF) LandmarkType(com.amplifyframework.predictions.models.LandmarkType) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 4 with Landmark

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

the class RekognitionResultTransformers method fromLandmarks.

/**
 * Converts a list of {@link com.amazonaws.services.rekognition.model.Landmark}
 * from Amazon Rekognition into Amplify-compatible list of
 * {@link Landmark} objects.
 * @param landmarks the list of Amazon Rekognition landmark objects
 * @return the list of Amplify Predictions landmark objects
 */
@NonNull
public static List<Landmark> fromLandmarks(@Nullable List<com.amazonaws.services.rekognition.model.Landmark> landmarks) {
    List<Landmark> amplifyLandmarks = new ArrayList<>();
    if (Empty.check(landmarks)) {
        return amplifyLandmarks;
    }
    List<PointF> allPoints = new ArrayList<>();
    Map<LandmarkType, List<PointF>> landmarkMap = new HashMap<>();
    // Pre-process all of the landmarks into a map of type -> matching points
    for (com.amazonaws.services.rekognition.model.Landmark landmark : landmarks) {
        LandmarkType type = LandmarkTypeAdapter.fromRekognition(landmark.getType());
        PointF point = new PointF(landmark.getX(), landmark.getY());
        List<PointF> points = landmarkMap.get(type);
        if (points == null) {
            points = new ArrayList<>();
            landmarkMap.put(type, points);
        }
        points.add(point);
        allPoints.add(point);
    }
    // Construct Amplify landmarks for each entry in the map + ALL_POINTS
    for (Map.Entry<LandmarkType, List<PointF>> entry : landmarkMap.entrySet()) {
        Landmark landmark = new Landmark(entry.getKey(), entry.getValue());
        amplifyLandmarks.add(landmark);
    }
    Landmark allPointsLandmark = new Landmark(LandmarkType.ALL_POINTS, allPoints);
    amplifyLandmarks.add(allPointsLandmark);
    return amplifyLandmarks;
}
Also used : HashMap(java.util.HashMap) Landmark(com.amplifyframework.predictions.models.Landmark) PointF(android.graphics.PointF) ArrayList(java.util.ArrayList) LandmarkType(com.amplifyframework.predictions.models.LandmarkType) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) NonNull(androidx.annotation.NonNull)

Aggregations

Landmark (com.amplifyframework.predictions.models.Landmark)4 ArrayList (java.util.ArrayList)4 PointF (android.graphics.PointF)2 RectF (android.graphics.RectF)2 AmazonClientException (com.amazonaws.AmazonClientException)2 Image (com.amazonaws.services.rekognition.model.Image)2 PredictionsException (com.amplifyframework.predictions.PredictionsException)2 LandmarkType (com.amplifyframework.predictions.models.LandmarkType)2 Pose (com.amplifyframework.predictions.models.Pose)2 HashMap (java.util.HashMap)2 List (java.util.List)2 NonNull (androidx.annotation.NonNull)1 ComparedFace (com.amazonaws.services.rekognition.model.ComparedFace)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 RecognizeCelebritiesRequest (com.amazonaws.services.rekognition.model.RecognizeCelebritiesRequest)1 RecognizeCelebritiesResult (com.amazonaws.services.rekognition.model.RecognizeCelebritiesResult)1 AgeRange (com.amplifyframework.predictions.models.AgeRange)1 BinaryFeature (com.amplifyframework.predictions.models.BinaryFeature)1