Search in sources :

Example 21 with RekognitionException

use of software.amazon.awssdk.services.rekognition.model.RekognitionException in project aws-doc-sdk-examples by awsdocs.

the class DetectText method detectTextLabels.

// snippet-start:[rekognition.java2.detect_text.main]
public static void detectTextLabels(RekognitionClient rekClient, String sourceImage) {
    try {
        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();
        DetectTextRequest textRequest = DetectTextRequest.builder().image(souImage).build();
        DetectTextResponse textResponse = rekClient.detectText(textRequest);
        List<TextDetection> textCollection = textResponse.textDetections();
        System.out.println("Detected lines and words");
        for (TextDetection text : textCollection) {
            System.out.println("Detected: " + text.detectedText());
            System.out.println("Confidence: " + text.confidence().toString());
            System.out.println("Id : " + text.id());
            System.out.println("Parent Id: " + text.parentId());
            System.out.println("Type: " + text.type());
            System.out.println();
        }
    } catch (RekognitionException | FileNotFoundException e) {
        System.out.println(e.getMessage());
        System.exit(1);
    }
}
Also used : TextDetection(software.amazon.awssdk.services.rekognition.model.TextDetection) SdkBytes(software.amazon.awssdk.core.SdkBytes) DetectTextRequest(software.amazon.awssdk.services.rekognition.model.DetectTextRequest) RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DetectTextResponse(software.amazon.awssdk.services.rekognition.model.DetectTextResponse) FileNotFoundException(java.io.FileNotFoundException) Image(software.amazon.awssdk.services.rekognition.model.Image) FileInputStream(java.io.FileInputStream)

Example 22 with RekognitionException

use of software.amazon.awssdk.services.rekognition.model.RekognitionException in project aws-doc-sdk-examples by awsdocs.

the class ListCollections method listAllCollections.

// snippet-start:[rekognition.java2.list_collections.main]
public static void listAllCollections(RekognitionClient rekClient) {
    try {
        ListCollectionsRequest listCollectionsRequest = ListCollectionsRequest.builder().maxResults(10).build();
        ListCollectionsResponse response = rekClient.listCollections(listCollectionsRequest);
        List<String> collectionIds = response.collectionIds();
        for (String resultId : collectionIds) {
            System.out.println(resultId);
        }
    } catch (RekognitionException e) {
        System.out.println(e.getMessage());
        System.exit(1);
    }
}
Also used : RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) ListCollectionsRequest(software.amazon.awssdk.services.rekognition.model.ListCollectionsRequest) ListCollectionsResponse(software.amazon.awssdk.services.rekognition.model.ListCollectionsResponse)

Example 23 with RekognitionException

use of software.amazon.awssdk.services.rekognition.model.RekognitionException in project aws-doc-sdk-examples by awsdocs.

the class RotateImage method recognizeAllCelebrities.

// snippet-start:[rekognition.java2.recognize_image_orientation.main]
public static void recognizeAllCelebrities(RekognitionClient rekClient, String sourceImage) {
    try {
        BufferedImage image = null;
        InputStream sourceStream = new FileInputStream(sourceImage);
        SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream);
        image = ImageIO.read(sourceBytes.asInputStream());
        int height = image.getHeight();
        int width = image.getWidth();
        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());
            ComparedFace face = celebrity.face();
            ShowBoundingBoxPositions(height, width, face.boundingBox(), result.orientationCorrectionAsString());
        }
    } catch (RekognitionException | FileNotFoundException e) {
        System.out.println(e.getMessage());
        System.exit(1);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) BufferedImage(java.awt.image.BufferedImage) Image(software.amazon.awssdk.services.rekognition.model.Image) ComparedFace(software.amazon.awssdk.services.rekognition.model.ComparedFace) BufferedImage(java.awt.image.BufferedImage) RecognizeCelebritiesResponse(software.amazon.awssdk.services.rekognition.model.RecognizeCelebritiesResponse) SdkBytes(software.amazon.awssdk.core.SdkBytes) RecognizeCelebritiesRequest(software.amazon.awssdk.services.rekognition.model.RecognizeCelebritiesRequest) Celebrity(software.amazon.awssdk.services.rekognition.model.Celebrity)

Example 24 with RekognitionException

use of software.amazon.awssdk.services.rekognition.model.RekognitionException in project aws-doc-sdk-examples by awsdocs.

the class VideoDetect method GetResultsLabels.

// Gets the job results by calling GetLabelDetection
private static void GetResultsLabels(RekognitionClient rekClient) {
    int maxResults = 10;
    String paginationToken = null;
    GetLabelDetectionResponse labelDetectionResult = null;
    try {
        do {
            if (labelDetectionResult != null)
                paginationToken = labelDetectionResult.nextToken();
            GetLabelDetectionRequest labelDetectionRequest = GetLabelDetectionRequest.builder().jobId(startJobId).sortBy(LabelDetectionSortBy.TIMESTAMP).maxResults(maxResults).nextToken(paginationToken).build();
            labelDetectionResult = rekClient.getLabelDetection(labelDetectionRequest);
            VideoMetadata videoMetaData = labelDetectionResult.videoMetadata();
            System.out.println("Format: " + videoMetaData.format());
            System.out.println("Codec: " + videoMetaData.codec());
            System.out.println("Duration: " + videoMetaData.durationMillis());
            System.out.println("FrameRate: " + videoMetaData.frameRate());
            List<LabelDetection> detectedLabels = labelDetectionResult.labels();
            for (LabelDetection detectedLabel : detectedLabels) {
                long seconds = detectedLabel.timestamp();
                Label label = detectedLabel.label();
                System.out.println("Millisecond: " + Long.toString(seconds) + " ");
                System.out.println("   Label:" + label.name());
                System.out.println("   Confidence:" + detectedLabel.label().confidence().toString());
                List<Instance> instances = label.instances();
                System.out.println("   Instances of " + label.name());
                if (instances.isEmpty()) {
                    System.out.println("        " + "None");
                } else {
                    for (Instance instance : instances) {
                        System.out.println("        Confidence: " + instance.confidence().toString());
                        System.out.println("        Bounding box: " + instance.boundingBox().toString());
                    }
                }
                System.out.println("   Parent labels for " + label.name() + ":");
                List<Parent> parents = label.parents();
                if (parents.isEmpty()) {
                    System.out.println("        None");
                } else {
                    for (Parent parent : parents) {
                        System.out.println("   " + parent.name());
                    }
                }
                System.out.println();
            }
        } while (labelDetectionResult != null && labelDetectionResult.nextToken() != null);
    } catch (RekognitionException e) {
        e.getMessage();
        System.exit(1);
    }
}
Also used : Instance(software.amazon.awssdk.services.rekognition.model.Instance) Parent(software.amazon.awssdk.services.rekognition.model.Parent) RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) GetLabelDetectionRequest(software.amazon.awssdk.services.rekognition.model.GetLabelDetectionRequest) Label(software.amazon.awssdk.services.rekognition.model.Label) VideoMetadata(software.amazon.awssdk.services.rekognition.model.VideoMetadata) GetLabelDetectionResponse(software.amazon.awssdk.services.rekognition.model.GetLabelDetectionResponse) LabelDetection(software.amazon.awssdk.services.rekognition.model.LabelDetection)

Example 25 with RekognitionException

use of software.amazon.awssdk.services.rekognition.model.RekognitionException in project aws-doc-sdk-examples by awsdocs.

the class VideoDetectInappropriate method startModerationDetection.

// snippet-start:[rekognition.java2.recognize_video_moderation.main]
public static void startModerationDetection(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();
        StartContentModerationRequest modDetectionRequest = StartContentModerationRequest.builder().jobTag("Moderation").notificationChannel(channel).video(vidOb).build();
        StartContentModerationResponse startModDetectionResult = rekClient.startContentModeration(modDetectionRequest);
        startJobId = startModDetectionResult.jobId();
    } catch (RekognitionException e) {
        System.out.println(e.getMessage());
        System.exit(1);
    }
}
Also used : StartContentModerationRequest(software.amazon.awssdk.services.rekognition.model.StartContentModerationRequest) RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) Video(software.amazon.awssdk.services.rekognition.model.Video) S3Object(software.amazon.awssdk.services.rekognition.model.S3Object) StartContentModerationResponse(software.amazon.awssdk.services.rekognition.model.StartContentModerationResponse)

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