use of software.amazon.awssdk.services.rekognition.model.RekognitionException 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.RekognitionException 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.RekognitionException in project aws-doc-sdk-examples by awsdocs.
the class VideoDetect method getLabelJob.
public static void getLabelJob(RekognitionClient rekClient, SqsClient sqs, String queueUrl) {
List<Message> messages = null;
ReceiveMessageRequest messageRequest = ReceiveMessageRequest.builder().queueUrl(queueUrl).build();
try {
messages = sqs.receiveMessage(messageRequest).messages();
if (!messages.isEmpty()) {
for (Message message : messages) {
String notification = message.body();
// Get the status and job id from the notification
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonMessageTree = mapper.readTree(notification);
JsonNode messageBodyText = jsonMessageTree.get("Message");
ObjectMapper operationResultMapper = new ObjectMapper();
JsonNode jsonResultTree = operationResultMapper.readTree(messageBodyText.textValue());
JsonNode operationJobId = jsonResultTree.get("JobId");
JsonNode operationStatus = jsonResultTree.get("Status");
System.out.println("Job found in JSON is " + operationJobId);
DeleteMessageRequest deleteMessageRequest = DeleteMessageRequest.builder().queueUrl(queueUrl).build();
String jobId = operationJobId.textValue();
if (startJobId.compareTo(jobId) == 0) {
System.out.println("Job id: " + operationJobId);
System.out.println("Status : " + operationStatus.toString());
if (operationStatus.asText().equals("SUCCEEDED"))
GetResultsLabels(rekClient);
else
System.out.println("Video analysis failed");
sqs.deleteMessage(deleteMessageRequest);
} else {
System.out.println("Job received was not job " + startJobId);
sqs.deleteMessage(deleteMessageRequest);
}
}
}
} catch (RekognitionException e) {
e.getMessage();
System.exit(1);
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (JsonProcessingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
use of software.amazon.awssdk.services.rekognition.model.RekognitionException in project aws-doc-sdk-examples by awsdocs.
the class VideoDetect method startLabels.
// snippet-start:[rekognition.java2.recognize_video_detect.main]
public static void startLabels(RekognitionClient rekClient, NotificationChannel channel, String bucket, String video) {
try {
S3Object s3Obj = S3Object.builder().bucket(bucket).name(video).build();
Video vidOb = Video.builder().s3Object(s3Obj).build();
StartLabelDetectionRequest labelDetectionRequest = StartLabelDetectionRequest.builder().jobTag("DetectingLabels").notificationChannel(channel).video(vidOb).minConfidence(50F).build();
StartLabelDetectionResponse labelDetectionResponse = rekClient.startLabelDetection(labelDetectionRequest);
startJobId = labelDetectionResponse.jobId();
boolean ans = true;
String status = "";
int yy = 0;
while (ans) {
GetLabelDetectionRequest detectionRequest = GetLabelDetectionRequest.builder().jobId(startJobId).maxResults(10).build();
GetLabelDetectionResponse result = rekClient.getLabelDetection(detectionRequest);
status = result.jobStatusAsString();
if (status.compareTo("SUCCEEDED") == 0)
ans = false;
else
System.out.println(yy + " status is: " + status);
Thread.sleep(1000);
yy++;
}
System.out.println(startJobId + " status is: " + status);
} catch (RekognitionException | InterruptedException e) {
e.getMessage();
System.exit(1);
}
}
use of software.amazon.awssdk.services.rekognition.model.RekognitionException in project aws-doc-sdk-examples by awsdocs.
the class VideoDetectText method startTextLabels.
// snippet-start:[rekognition.java2.recognize_video_text.main]
public static void startTextLabels(RekognitionClient rekClient, NotificationChannel channel, String bucket, String video) {
try {
S3Object s3Obj = S3Object.builder().bucket(bucket).name(video).build();
Video vidOb = Video.builder().s3Object(s3Obj).build();
StartTextDetectionRequest labelDetectionRequest = StartTextDetectionRequest.builder().jobTag("DetectingLabels").notificationChannel(channel).video(vidOb).build();
StartTextDetectionResponse labelDetectionResponse = rekClient.startTextDetection(labelDetectionRequest);
startJobId = labelDetectionResponse.jobId();
} catch (RekognitionException e) {
System.out.println(e.getMessage());
System.exit(1);
}
}
Aggregations