use of com.amplifyframework.predictions.models.EntityDetails 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.EntityDetails in project amplify-android by aws-amplify.
the class AWSRekognitionService method detectEntities.
void detectEntities(@NonNull ByteBuffer imageData, @NonNull Consumer<IdentifyResult> onSuccess, @NonNull Consumer<PredictionsException> onError) {
final IdentifyEntitiesConfiguration config;
try {
config = pluginConfiguration.getIdentifyEntitiesConfiguration();
if (config.isGeneralEntityDetection()) {
List<EntityDetails> entities = detectEntities(imageData);
onSuccess.accept(IdentifyEntitiesResult.fromEntityDetails(entities));
} else {
int maxEntities = config.getMaxEntities();
String collectionId = config.getCollectionId();
List<EntityMatch> matches = detectEntityMatches(imageData, maxEntities, collectionId);
onSuccess.accept(IdentifyEntityMatchesResult.fromEntityMatches(matches));
}
} catch (PredictionsException exception) {
onError.accept(exception);
}
}
use of com.amplifyframework.predictions.models.EntityDetails in project amplify-android by aws-amplify.
the class AWSPredictionsIdentifyEntitiesTest method testIdentifyEntities.
/**
* Assert general entity detection works.
* @throws Exception if prediction fails
*/
@Test
// Jeff Bezos' current age
@SuppressWarnings("MagicNumber")
public void testIdentifyEntities() throws Exception {
final Bitmap image = Assets.readAsBitmap("jeff_bezos.jpg");
// Identify the entity inside given image and assert non-null result.
IdentifyEntitiesResult result = (IdentifyEntitiesResult) predictions.identify(TYPE, image);
assertNotNull(result);
// Assert that at least one entity is detected
assertFalse(Empty.check(result.getEntities()));
EntityDetails entity = result.getEntities().get(0);
// Assert features from detected entity
FeatureAssert.assertMatches(GenderBinaryType.MALE, entity.getGender());
FeatureAssert.assertMatches(EmotionType.HAPPY, Collections.max(entity.getEmotions()));
assertNotNull(entity.getAgeRange());
int jeffAge = 56;
// When this test was created, Rekognition returned an age range containing Jeff's age. The algorithm
// recently changed to predict an age range of 37-55. The goal of this test is to verify a sane response,
// not verify the accurate of Rekognition, so we will add a 20 year buffer to the age range (e.g. if age range
// is 37 to 55, the test will pass because 56 is still between 37-20 (17) and 55+20 (75)).
int buffer = 20;
assertTrue(jeffAge <= entity.getAgeRange().getHigh() + buffer);
assertTrue(jeffAge >= entity.getAgeRange().getLow() - buffer);
}
Aggregations