use of com.amazonaws.services.rekognition.model.Image in project amplify-android by aws-amplify.
the class AWSRekognitionService method detectModerationLabels.
private List<Label> detectModerationLabels(ByteBuffer imageData) throws PredictionsException {
DetectModerationLabelsRequest request = new DetectModerationLabelsRequest().withImage(new Image().withBytes(imageData));
// Detect moderation labels in the given image via Amazon Rekognition
final DetectModerationLabelsResult result;
try {
result = rekognition.detectModerationLabels(request);
} catch (AmazonClientException serviceException) {
throw new PredictionsException("Amazon Rekognition encountered an error while detecting moderation labels.", serviceException, "See attached service exception for more details.");
}
List<Label> labels = new ArrayList<>();
for (ModerationLabel moderationLabel : result.getModerationLabels()) {
Label label = Label.builder().value(moderationLabel.getName()).confidence(moderationLabel.getConfidence()).parentLabels(Collections.singletonList(moderationLabel.getParentName())).build();
labels.add(label);
}
return labels;
}
use of com.amazonaws.services.rekognition.model.Image in project amplify-android by aws-amplify.
the class AWSRekognitionService method detectEntityMatches.
private List<EntityMatch> detectEntityMatches(ByteBuffer imageData, int maxEntities, String collectionId) throws PredictionsException {
SearchFacesByImageRequest request = new SearchFacesByImageRequest().withImage(new Image().withBytes(imageData)).withMaxFaces(maxEntities).withCollectionId(collectionId);
// Detect entities in the given image by matching against a collection of images
final SearchFacesByImageResult result;
try {
result = rekognition.searchFacesByImage(request);
} catch (AmazonClientException serviceException) {
throw new PredictionsException("Amazon Rekognition encountered an error while searching for known faces.", serviceException, "See attached service exception for more details.");
}
List<EntityMatch> matches = new ArrayList<>();
for (FaceMatch rekognitionMatch : result.getFaceMatches()) {
Face face = rekognitionMatch.getFace();
RectF box = RekognitionResultTransformers.fromBoundingBox(face.getBoundingBox());
EntityMatch amplifyMatch = EntityMatch.builder().externalImageId(face.getExternalImageId()).confidence(rekognitionMatch.getSimilarity()).box(box).build();
matches.add(amplifyMatch);
}
return matches;
}
use of com.amazonaws.services.rekognition.model.Image 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.amazonaws.services.rekognition.model.Image 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.amazonaws.services.rekognition.model.Image in project aws-doc-sdk-examples by awsdocs.
the class CompareFaces method main.
public static void main(String[] args) throws Exception {
Float similarityThreshold = 70F;
// Replace sourceFile and targetFile with the image files you want to compare.
String sourceImage = "source.jpg";
String targetImage = "target.jpg";
ByteBuffer sourceImageBytes = null;
ByteBuffer targetImageBytes = null;
AmazonRekognition rekognitionClient = AmazonRekognitionClientBuilder.defaultClient();
// Load source and target images and create input parameters
try (InputStream inputStream = new FileInputStream(new File(sourceImage))) {
sourceImageBytes = ByteBuffer.wrap(IOUtils.toByteArray(inputStream));
} catch (Exception e) {
System.out.println("Failed to load source image " + sourceImage);
System.exit(1);
}
try (InputStream inputStream = new FileInputStream(new File(targetImage))) {
targetImageBytes = ByteBuffer.wrap(IOUtils.toByteArray(inputStream));
} catch (Exception e) {
System.out.println("Failed to load target images: " + targetImage);
System.exit(1);
}
Image source = new Image().withBytes(sourceImageBytes);
Image target = new Image().withBytes(targetImageBytes);
CompareFacesRequest request = new CompareFacesRequest().withSourceImage(source).withTargetImage(target).withSimilarityThreshold(similarityThreshold);
// Call operation
CompareFacesResult compareFacesResult = rekognitionClient.compareFaces(request);
// Display results
List<CompareFacesMatch> faceDetails = compareFacesResult.getFaceMatches();
for (CompareFacesMatch match : faceDetails) {
ComparedFace face = match.getFace();
BoundingBox position = face.getBoundingBox();
System.out.println("Face at " + position.getLeft().toString() + " " + position.getTop() + " matches with " + face.getConfidence().toString() + "% confidence.");
}
List<ComparedFace> uncompared = compareFacesResult.getUnmatchedFaces();
System.out.println("There was " + uncompared.size() + " face(s) that did not match");
System.out.println("Source image rotation: " + compareFacesResult.getSourceImageOrientationCorrection());
System.out.println("target image rotation: " + compareFacesResult.getTargetImageOrientationCorrection());
}
Aggregations