use of software.amazon.awssdk.services.rekognition.model.RekognitionException 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);
}
}
use of software.amazon.awssdk.services.rekognition.model.RekognitionException in project aws-doc-sdk-examples by awsdocs.
the class DetectLabelsS3 method getLabelsfromImage.
// snippet-start:[rekognition.java2.detect_labels_s3.main]
public static void getLabelsfromImage(RekognitionClient rekClient, String bucket, String image) {
try {
S3Object s3Object = S3Object.builder().bucket(bucket).name(image).build();
Image myImage = Image.builder().s3Object(s3Object).build();
DetectLabelsRequest detectLabelsRequest = DetectLabelsRequest.builder().image(myImage).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 e) {
System.out.println(e.getMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.rekognition.model.RekognitionException in project aws-doc-sdk-examples by awsdocs.
the class DetectCustomLabels method detectImageCustomLabels.
public static void detectImageCustomLabels(RekognitionClient rekClient, String arn, String bucket, String key) {
try {
S3Object s3Object = S3Object.builder().bucket(bucket).name(key).build();
// Create an Image object for the source image
Image s3Image = Image.builder().s3Object(s3Object).build();
DetectCustomLabelsRequest detectCustomLabelsRequest = DetectCustomLabelsRequest.builder().image(s3Image).projectVersionArn(arn).build();
DetectCustomLabelsResponse customLabelsResponse = rekClient.detectCustomLabels(detectCustomLabelsRequest);
List<CustomLabel> customLabels = customLabelsResponse.customLabels();
System.out.println("Detected labels for the given photo");
for (CustomLabel customLabel : customLabels) {
System.out.println(customLabel.name() + ": " + customLabel.confidence().toString());
}
} catch (RekognitionException e) {
System.out.println(e.getMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.rekognition.model.RekognitionException in project aws-doc-sdk-examples by awsdocs.
the class AnalyzePhotos method detectLabels.
// Returns a list of GearItem objects that contains PPE information.
public ArrayList<GearItem> detectLabels(byte[] bytes, String key) {
Region region = Region.US_EAST_2;
RekognitionClient rekClient = RekognitionClient.builder().region(region).build();
ArrayList<GearItem> gearList = new ArrayList<>();
try {
SdkBytes sourceBytes = SdkBytes.fromByteArray(bytes);
// Create an Image object for the source image.
Image souImage = Image.builder().bytes(sourceBytes).build();
ProtectiveEquipmentSummarizationAttributes summarizationAttributes = ProtectiveEquipmentSummarizationAttributes.builder().minConfidence(80F).requiredEquipmentTypesWithStrings("FACE_COVER", "HAND_COVER", "HEAD_COVER").build();
DetectProtectiveEquipmentRequest request = DetectProtectiveEquipmentRequest.builder().image(souImage).summarizationAttributes(summarizationAttributes).build();
DetectProtectiveEquipmentResponse result = rekClient.detectProtectiveEquipment(request);
List<ProtectiveEquipmentPerson> persons = result.persons();
// Create a GearItem object.
GearItem gear;
for (ProtectiveEquipmentPerson person : persons) {
List<ProtectiveEquipmentBodyPart> bodyParts = person.bodyParts();
if (bodyParts.isEmpty()) {
System.out.println("\tNo body parts detected");
} else
for (ProtectiveEquipmentBodyPart bodyPart : bodyParts) {
List<EquipmentDetection> equipmentDetections = bodyPart.equipmentDetections();
if (equipmentDetections.isEmpty()) {
System.out.println("\t\tNo PPE Detected on " + bodyPart.name());
} else {
for (EquipmentDetection item : equipmentDetections) {
gear = new GearItem();
gear.setKey(key);
String itemType = item.type().toString();
String confidence = item.confidence().toString();
String myDesc = "Item: " + item.type() + ". Confidence: " + item.confidence().toString();
String bodyPartDes = "Covers body part: " + item.coversBodyPart().value().toString() + ". Confidence: " + item.coversBodyPart().confidence().toString();
gear.setName(itemType);
gear.setConfidence(confidence);
gear.setItemDescription(myDesc);
gear.setBodyCoverDescription(bodyPartDes);
// push the object.
gearList.add(gear);
}
}
}
}
if (gearList.isEmpty())
return null;
else
return gearList;
} catch (RekognitionException e) {
System.out.println(e.getMessage());
System.exit(1);
}
return null;
}
use of software.amazon.awssdk.services.rekognition.model.RekognitionException in project aws-doc-sdk-examples by awsdocs.
the class RecognizeCelebrities method recognizeAllCelebrities.
// snippet-start:[rekognition.java2.recognize_celebs.main]
public static void recognizeAllCelebrities(RekognitionClient rekClient, String sourceImage) {
try {
InputStream sourceStream = new FileInputStream(sourceImage);
SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream);
Image souImage = Image.builder().bytes(sourceBytes).build();
RecognizeCelebritiesRequest request = RecognizeCelebritiesRequest.builder().image(souImage).build();
RecognizeCelebritiesResponse result = rekClient.recognizeCelebrities(request);
List<Celebrity> celebs = result.celebrityFaces();
System.out.println(celebs.size() + " celebrity(s) were recognized.\n");
for (Celebrity celebrity : celebs) {
System.out.println("Celebrity recognized: " + celebrity.name());
System.out.println("Celebrity ID: " + celebrity.id());
System.out.println("Further information (if available):");
for (String url : celebrity.urls()) {
System.out.println(url);
}
System.out.println();
}
System.out.println(result.unrecognizedFaces().size() + " face(s) were unrecognized.");
} catch (RekognitionException | FileNotFoundException e) {
System.out.println(e.getMessage());
System.exit(1);
}
}
Aggregations