Search in sources :

Example 6 with RekognitionException

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);
    }
}
Also used : ModerationLabel(software.amazon.awssdk.services.rekognition.model.ModerationLabel) SdkBytes(software.amazon.awssdk.core.SdkBytes) RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) Image(software.amazon.awssdk.services.rekognition.model.Image) DetectModerationLabelsResponse(software.amazon.awssdk.services.rekognition.model.DetectModerationLabelsResponse) FileInputStream(java.io.FileInputStream) DetectModerationLabelsRequest(software.amazon.awssdk.services.rekognition.model.DetectModerationLabelsRequest)

Example 7 with RekognitionException

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);
    }
}
Also used : SdkBytes(software.amazon.awssdk.core.SdkBytes) RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SearchFacesByImageRequest(software.amazon.awssdk.services.rekognition.model.SearchFacesByImageRequest) FileNotFoundException(java.io.FileNotFoundException) SearchFacesByImageResponse(software.amazon.awssdk.services.rekognition.model.SearchFacesByImageResponse) Image(software.amazon.awssdk.services.rekognition.model.Image) File(java.io.File) FaceMatch(software.amazon.awssdk.services.rekognition.model.FaceMatch) FileInputStream(java.io.FileInputStream)

Example 8 with RekognitionException

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();
    }
}
Also used : Message(software.amazon.awssdk.services.sqs.model.Message) ReceiveMessageRequest(software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest) RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) DeleteMessageRequest(software.amazon.awssdk.services.sqs.model.DeleteMessageRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException)

Example 9 with RekognitionException

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);
    }
}
Also used : GetLabelDetectionResponse(software.amazon.awssdk.services.rekognition.model.GetLabelDetectionResponse) RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) Video(software.amazon.awssdk.services.rekognition.model.Video) GetLabelDetectionRequest(software.amazon.awssdk.services.rekognition.model.GetLabelDetectionRequest) StartLabelDetectionRequest(software.amazon.awssdk.services.rekognition.model.StartLabelDetectionRequest) S3Object(software.amazon.awssdk.services.rekognition.model.S3Object) StartLabelDetectionResponse(software.amazon.awssdk.services.rekognition.model.StartLabelDetectionResponse)

Example 10 with RekognitionException

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);
    }
}
Also used : StartTextDetectionRequest(software.amazon.awssdk.services.rekognition.model.StartTextDetectionRequest) StartTextDetectionResponse(software.amazon.awssdk.services.rekognition.model.StartTextDetectionResponse) RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) Video(software.amazon.awssdk.services.rekognition.model.Video) S3Object(software.amazon.awssdk.services.rekognition.model.S3Object)

Aggregations

RekognitionException (software.amazon.awssdk.services.rekognition.model.RekognitionException)33 Image (software.amazon.awssdk.services.rekognition.model.Image)12 SdkBytes (software.amazon.awssdk.core.SdkBytes)10 FileInputStream (java.io.FileInputStream)8 FileNotFoundException (java.io.FileNotFoundException)8 InputStream (java.io.InputStream)8 S3Object (software.amazon.awssdk.services.rekognition.model.S3Object)8 Video (software.amazon.awssdk.services.rekognition.model.Video)6 VideoMetadata (software.amazon.awssdk.services.rekognition.model.VideoMetadata)6 File (java.io.File)2 Celebrity (software.amazon.awssdk.services.rekognition.model.Celebrity)2 ComparedFace (software.amazon.awssdk.services.rekognition.model.ComparedFace)2 DetectLabelsRequest (software.amazon.awssdk.services.rekognition.model.DetectLabelsRequest)2 DetectLabelsResponse (software.amazon.awssdk.services.rekognition.model.DetectLabelsResponse)2 FaceMatch (software.amazon.awssdk.services.rekognition.model.FaceMatch)2 Label (software.amazon.awssdk.services.rekognition.model.Label)2 RecognizeCelebritiesRequest (software.amazon.awssdk.services.rekognition.model.RecognizeCelebritiesRequest)2 RecognizeCelebritiesResponse (software.amazon.awssdk.services.rekognition.model.RecognizeCelebritiesResponse)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1