55
66// snippet-start:[rekognition.java2.compare_faces.main]
77// snippet-start:[rekognition.java2.compare_faces.import]
8+
89import software .amazon .awssdk .regions .Region ;
910import software .amazon .awssdk .services .rekognition .RekognitionClient ;
1011import software .amazon .awssdk .services .rekognition .model .RekognitionException ;
1516import software .amazon .awssdk .services .rekognition .model .ComparedFace ;
1617import software .amazon .awssdk .services .rekognition .model .BoundingBox ;
1718import software .amazon .awssdk .core .SdkBytes ;
19+
1820import java .io .FileInputStream ;
1921import java .io .FileNotFoundException ;
2022import java .io .InputStream ;
2426/**
2527 * Before running this Java V2 code example, set up your development
2628 * environment, including your credentials.
27- *
29+ * <p>
2830 * For more information, see the following documentation topic:
29- *
31+ * <p>
3032 * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
3133 */
3234public class CompareFaces {
3335 public static void main (String [] args ) {
3436 final String usage = """
3537
36- Usage: <pathSource> <pathTarget>
38+ Usage: <pathSource> <pathTarget>
3739
38- Where:
39- pathSource - The path to the source image (for example, C:\\ AWS\\ pic1.png).\s
40- pathTarget - The path to the target image (for example, C:\\ AWS\\ pic2.png).\s
41- """ ;
40+ Where:
41+ pathSource - The path to the source image (for example, C:\\ AWS\\ pic1.png).\s
42+ pathTarget - The path to the target image (for example, C:\\ AWS\\ pic2.png).\s
43+ """ ;
4244
4345 if (args .length != 2 ) {
4446 System .out .println (usage );
@@ -50,15 +52,15 @@ public static void main(String[] args) {
5052 String targetImage = args [1 ];
5153 Region region = Region .US_EAST_1 ;
5254 RekognitionClient rekClient = RekognitionClient .builder ()
53- .region (region )
54- .build ();
55+ .region (region )
56+ .build ();
5557
5658 compareTwoFaces (rekClient , similarityThreshold , sourceImage , targetImage );
5759 rekClient .close ();
5860 }
5961
6062 public static void compareTwoFaces (RekognitionClient rekClient , Float similarityThreshold , String sourceImage ,
61- String targetImage ) {
63+ String targetImage ) {
6264 try {
6365 InputStream sourceStream = new FileInputStream (sourceImage );
6466 InputStream tarStream = new FileInputStream (targetImage );
@@ -67,18 +69,18 @@ public static void compareTwoFaces(RekognitionClient rekClient, Float similarity
6769
6870 // Create an Image object for the source image.
6971 Image souImage = Image .builder ()
70- .bytes (sourceBytes )
71- .build ();
72+ .bytes (sourceBytes )
73+ .build ();
7274
7375 Image tarImage = Image .builder ()
74- .bytes (targetBytes )
75- .build ();
76+ .bytes (targetBytes )
77+ .build ();
7678
7779 CompareFacesRequest facesRequest = CompareFacesRequest .builder ()
78- .sourceImage (souImage )
79- .targetImage (tarImage )
80- .similarityThreshold (similarityThreshold )
81- .build ();
80+ .sourceImage (souImage )
81+ .targetImage (tarImage )
82+ .similarityThreshold (similarityThreshold )
83+ .build ();
8284
8385 // Compare the two images.
8486 CompareFacesResponse compareFacesResult = rekClient .compareFaces (facesRequest );
@@ -87,9 +89,9 @@ public static void compareTwoFaces(RekognitionClient rekClient, Float similarity
8789 ComparedFace face = match .face ();
8890 BoundingBox position = face .boundingBox ();
8991 System .out .println ("Face at " + position .left ().toString ()
90- + " " + position .top ()
91- + " matches with " + face .confidence ().toString ()
92- + "% confidence." );
92+ + " " + position .top ()
93+ + " matches with " + face .confidence ().toString ()
94+ + "% confidence." );
9395
9496 }
9597 List <ComparedFace > uncompared = compareFacesResult .unmatchedFaces ();
@@ -98,8 +100,7 @@ public static void compareTwoFaces(RekognitionClient rekClient, Float similarity
98100 System .out .println ("target image rotation: " + compareFacesResult .targetImageOrientationCorrection ());
99101
100102 } catch (RekognitionException | FileNotFoundException e ) {
101- System .out .println ("Failed to load source image " + sourceImage );
102- System .exit (1 );
103+ e .printStackTrace ();
103104 }
104105 }
105106}
0 commit comments