use of software.amazon.awssdk.services.rekognition.model.Image in project aws-doc-sdk-examples by awsdocs.
the class DetectLabels method detectImageLabels.
// snippet-start:[rekognition.java2.detect_labels.main]
public static void detectImageLabels(RekognitionClient rekClient, String sourceImage) {
try {
InputStream sourceStream = new URL("https://images.unsplash.com/photo-1557456170-0cf4f4d0d362?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8bGFrZXxlbnwwfHwwfHw%3D&ixlib=rb-1.2.1&w=1000&q=80").openStream();
// InputStream sourceStream = new FileInputStream(sourceImage);
SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream);
// Create an Image object for the source image.
Image souImage = Image.builder().bytes(sourceBytes).build();
DetectLabelsRequest detectLabelsRequest = DetectLabelsRequest.builder().image(souImage).maxLabels(10).build();
DetectLabelsResponse labelsResponse = rekClient.detectLabels(detectLabelsRequest);
List<Label> labels = labelsResponse.labels();
System.out.println("Detected labels for the given photo");
for (Label label : labels) {
System.out.println(label.name() + ": " + label.confidence().toString());
}
} catch (RekognitionException | FileNotFoundException | MalformedURLException e) {
System.out.println(e.getMessage());
System.exit(1);
} catch (IOException e) {
e.printStackTrace();
}
}
use of software.amazon.awssdk.services.rekognition.model.Image in project aws-doc-sdk-examples by awsdocs.
the class DetectModerationLabels method detectModLabels.
// snippet-start:[rekognition.java2.detect_mod_labels.main]
public static void detectModLabels(RekognitionClient rekClient, String sourceImage) {
try {
InputStream sourceStream = new FileInputStream(sourceImage);
SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream);
Image souImage = Image.builder().bytes(sourceBytes).build();
DetectModerationLabelsRequest moderationLabelsRequest = DetectModerationLabelsRequest.builder().image(souImage).minConfidence(60F).build();
DetectModerationLabelsResponse moderationLabelsResponse = rekClient.detectModerationLabels(moderationLabelsRequest);
// Display the results
List<ModerationLabel> labels = moderationLabelsResponse.moderationLabels();
System.out.println("Detected labels for image");
for (ModerationLabel label : labels) {
System.out.println("Label: " + label.name() + "\n Confidence: " + label.confidence().toString() + "%" + "\n Parent:" + label.parentName());
}
} catch (RekognitionException | FileNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
}
use of software.amazon.awssdk.services.rekognition.model.Image in project aws-doc-sdk-examples by awsdocs.
the class PPEBoundingBoxFrame method displayGear.
// snippet-start:[rekognition.java2.display_mask.main]
public static void displayGear(S3Client s3, RekognitionClient rekClient, String sourceImage, String bucketName) {
float confidence = 80;
byte[] data = getObjectBytes(s3, bucketName, sourceImage);
InputStream is = new ByteArrayInputStream(data);
try {
ProtectiveEquipmentSummarizationAttributes summarizationAttributes = ProtectiveEquipmentSummarizationAttributes.builder().minConfidence(70F).requiredEquipmentTypesWithStrings("FACE_COVER").build();
SdkBytes sourceBytes = SdkBytes.fromInputStream(is);
image = ImageIO.read(sourceBytes.asInputStream());
// Create an Image object for the source image.
software.amazon.awssdk.services.rekognition.model.Image souImage = Image.builder().bytes(sourceBytes).build();
DetectProtectiveEquipmentRequest request = DetectProtectiveEquipmentRequest.builder().image(souImage).summarizationAttributes(summarizationAttributes).build();
DetectProtectiveEquipmentResponse result = rekClient.detectProtectiveEquipment(request);
// Create frame and panel.
JFrame frame = new JFrame("Detect PPE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PPEBoundingBoxFrame panel = new PPEBoundingBoxFrame(result, image, confidence);
panel.setPreferredSize(new Dimension(image.getWidth() / scale, image.getHeight() / scale));
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
} catch (RekognitionException e) {
e.printStackTrace();
System.exit(1);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
use of software.amazon.awssdk.services.rekognition.model.Image in project aws-doc-sdk-examples by awsdocs.
the class SearchFaceMatchingImageCollection method searchFaceInCollection.
// snippet-start:[rekognition.java2.search_faces_collection.main]
public static void searchFaceInCollection(RekognitionClient rekClient, String collectionId, String sourceImage) {
try {
InputStream sourceStream = new FileInputStream(new File(sourceImage));
SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream);
Image souImage = Image.builder().bytes(sourceBytes).build();
SearchFacesByImageRequest facesByImageRequest = SearchFacesByImageRequest.builder().image(souImage).maxFaces(10).faceMatchThreshold(70F).collectionId(collectionId).build();
SearchFacesByImageResponse imageResponse = rekClient.searchFacesByImage(facesByImageRequest);
// Display the results.
System.out.println("Faces matching in the collection");
List<FaceMatch> faceImageMatches = imageResponse.faceMatches();
for (FaceMatch face : faceImageMatches) {
System.out.println("The similarity level is " + face.similarity());
System.out.println();
}
} catch (RekognitionException | FileNotFoundException e) {
System.out.println(e.getMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.rekognition.model.Image in project aws-doc-sdk-examples by awsdocs.
the class DetectFaces method detectFacesinImage.
// snippet-start:[rekognition.java2.detect_faces.main]
public static void detectFacesinImage(RekognitionClient rekClient, String sourceImage) {
try {
InputStream sourceStream = new FileInputStream(new File(sourceImage));
SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream);
// Create an Image object for the source image.
Image souImage = Image.builder().bytes(sourceBytes).build();
DetectFacesRequest facesRequest = DetectFacesRequest.builder().attributes(Attribute.ALL).image(souImage).build();
DetectFacesResponse facesResponse = rekClient.detectFaces(facesRequest);
List<FaceDetail> faceDetails = facesResponse.faceDetails();
for (FaceDetail face : faceDetails) {
AgeRange ageRange = face.ageRange();
System.out.println("The detected face is estimated to be between " + ageRange.low().toString() + " and " + ageRange.high().toString() + " years old.");
System.out.println("There is a smile : " + face.smile().value().toString());
}
} catch (RekognitionException | FileNotFoundException e) {
System.out.println(e.getMessage());
System.exit(1);
}
}
Aggregations