2019-08-27 16:25:01 -07:00
// snippet-sourcedescription:[rekognition-image-java-recognize-celebrities.java demonstrates how to recognize celebrities in an image loaded from a local file system.]
// snippet-service:[rekognition]
2019-09-05 19:52:34 -07:00
// snippet-keyword:[Java]
// snippet-sourcesyntax:[java]
2019-08-27 16:25:01 -07:00
// snippet-keyword:[Amazon Rekognition]
// snippet-keyword:[Code Sample]
// snippet-keyword:[RecognizeCelebrities]
// snippet-keyword:[Local]
// snippet-keyword:[Celebrity]
// snippet-keyword:[Image]
// snippet-sourcetype:[full-example]
// snippet-sourcedate:[2019-01-18]
// snippet-sourceauthor:[reesch(AWS)]
// snippet-start:[rekognition.java.rekognition-image-java-recognize-celebrities.complete]
/**
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* This file is licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License. A copy of
* the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package aws.example.rekognition.image ;
import com.amazonaws.services.rekognition.AmazonRekognition ;
import com.amazonaws.services.rekognition.AmazonRekognitionClientBuilder ;
import com.amazonaws.services.rekognition.model.Image ;
import com.amazonaws.services.rekognition.model.BoundingBox ;
import com.amazonaws.services.rekognition.model.Celebrity ;
import com.amazonaws.services.rekognition.model.RecognizeCelebritiesRequest ;
import com.amazonaws.services.rekognition.model.RecognizeCelebritiesResult ;
import java.io.File ;
import java.io.FileInputStream ;
import java.io.InputStream ;
import java.nio.ByteBuffer ;
import com.amazonaws.util.IOUtils ;
import java.util.List ;
public class RecognizeCelebrities {
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. " ) ;
}
}
2019-01-17 17:03:17 -08:00
// snippet-end:[rekognition.java.rekognition-image-java-recognize-celebrities.complete]