Search in sources :

Example 1 with RecognizeCelebritiesRequest

use of com.amazonaws.services.rekognition.model.RecognizeCelebritiesRequest in project aws-doc-sdk-examples by awsdocs.

the class RecognizeCelebrities method main.

public static void main(String[] args) {
    // Change photo to the path and filename of your image.
    String photo = "moviestars.jpg";
    AmazonRekognition rekognitionClient = AmazonRekognitionClientBuilder.defaultClient();
    ByteBuffer imageBytes = null;
    try (InputStream inputStream = new FileInputStream(new File(photo))) {
        imageBytes = ByteBuffer.wrap(IOUtils.toByteArray(inputStream));
    } catch (Exception e) {
        System.out.println("Failed to load file " + photo);
        System.exit(1);
    }
    RecognizeCelebritiesRequest request = new RecognizeCelebritiesRequest().withImage(new Image().withBytes(imageBytes));
    System.out.println("Looking for celebrities in image " + photo + "\n");
    RecognizeCelebritiesResult result = rekognitionClient.recognizeCelebrities(request);
    // Display recognized celebrity information
    List<Celebrity> celebs = result.getCelebrityFaces();
    System.out.println(celebs.size() + " celebrity(s) were recognized.\n");
    for (Celebrity celebrity : celebs) {
        System.out.println("Celebrity recognized: " + celebrity.getName());
        System.out.println("Celebrity ID: " + celebrity.getId());
        BoundingBox boundingBox = celebrity.getFace().getBoundingBox();
        System.out.println("position: " + boundingBox.getLeft().toString() + " " + boundingBox.getTop().toString());
        System.out.println("Further information (if available):");
        for (String url : celebrity.getUrls()) {
            System.out.println(url);
        }
        System.out.println();
    }
    System.out.println(result.getUnrecognizedFaces().size() + " face(s) were unrecognized.");
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Image(com.amazonaws.services.rekognition.model.Image) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream) RecognizeCelebritiesResult(com.amazonaws.services.rekognition.model.RecognizeCelebritiesResult) RecognizeCelebritiesRequest(com.amazonaws.services.rekognition.model.RecognizeCelebritiesRequest) Celebrity(com.amazonaws.services.rekognition.model.Celebrity) BoundingBox(com.amazonaws.services.rekognition.model.BoundingBox) AmazonRekognition(com.amazonaws.services.rekognition.AmazonRekognition) File(java.io.File)

Example 2 with RecognizeCelebritiesRequest

use of com.amazonaws.services.rekognition.model.RecognizeCelebritiesRequest 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)

Aggregations

Image (com.amazonaws.services.rekognition.model.Image)2 RecognizeCelebritiesRequest (com.amazonaws.services.rekognition.model.RecognizeCelebritiesRequest)2 RecognizeCelebritiesResult (com.amazonaws.services.rekognition.model.RecognizeCelebritiesResult)2 RectF (android.graphics.RectF)1 AmazonClientException (com.amazonaws.AmazonClientException)1 AmazonRekognition (com.amazonaws.services.rekognition.AmazonRekognition)1 BoundingBox (com.amazonaws.services.rekognition.model.BoundingBox)1 Celebrity (com.amazonaws.services.rekognition.model.Celebrity)1 ComparedFace (com.amazonaws.services.rekognition.model.ComparedFace)1 PredictionsException (com.amplifyframework.predictions.PredictionsException)1 Celebrity (com.amplifyframework.predictions.models.Celebrity)1 CelebrityDetails (com.amplifyframework.predictions.models.CelebrityDetails)1 Landmark (com.amplifyframework.predictions.models.Landmark)1 Pose (com.amplifyframework.predictions.models.Pose)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ByteBuffer (java.nio.ByteBuffer)1