Search in sources :

Example 1 with LandmarkType

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

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

PointF (android.graphics.PointF)2 Landmark (com.amplifyframework.predictions.models.Landmark)2 LandmarkType (com.amplifyframework.predictions.models.LandmarkType)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 NonNull (androidx.annotation.NonNull)1 Map (java.util.Map)1 Test (org.junit.Test)1