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;
}
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;
}
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())));
}
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;
}
Aggregations