Search in sources :

Example 1 with RekognitionException

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

the class ListFacesInCollection method listFacesCollection.

// snippet-start:[rekognition.java2.list_faces_collection.main]
public static void listFacesCollection(RekognitionClient rekClient, String collectionId) {
    try {
        ListFacesRequest facesRequest = ListFacesRequest.builder().collectionId(collectionId).maxResults(10).build();
        ListFacesResponse facesResponse = rekClient.listFaces(facesRequest);
        // For each face in the collection, print out the confidence level and face id value.
        List<Face> faces = facesResponse.faces();
        for (Face face : faces) {
            System.out.println("Confidence level there is a face: " + face.confidence());
            System.out.println("The face Id value is " + face.faceId());
        }
    } catch (RekognitionException e) {
        System.out.println(e.getMessage());
        System.exit(1);
    }
}
Also used : RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) ListFacesResponse(software.amazon.awssdk.services.rekognition.model.ListFacesResponse) ListFacesRequest(software.amazon.awssdk.services.rekognition.model.ListFacesRequest) Face(software.amazon.awssdk.services.rekognition.model.Face)

Example 2 with RekognitionException

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

the class VideoCelebrityDetection method GetCelebrityDetectionResults.

public static void GetCelebrityDetectionResults(RekognitionClient rekClient) {
    try {
        String paginationToken = null;
        GetCelebrityRecognitionResponse recognitionResponse = null;
        Boolean finished = false;
        String status = "";
        int yy = 0;
        do {
            if (recognitionResponse != null)
                paginationToken = recognitionResponse.nextToken();
            GetCelebrityRecognitionRequest recognitionRequest = GetCelebrityRecognitionRequest.builder().jobId(startJobId).nextToken(paginationToken).sortBy(CelebrityRecognitionSortBy.TIMESTAMP).maxResults(10).build();
            // Wait until the job succeeds
            while (!finished) {
                recognitionResponse = rekClient.getCelebrityRecognition(recognitionRequest);
                status = recognitionResponse.jobStatusAsString();
                if (status.compareTo("SUCCEEDED") == 0)
                    finished = true;
                else {
                    System.out.println(yy + " status is: " + status);
                    Thread.sleep(1000);
                }
                yy++;
            }
            finished = false;
            // Proceed when the job is done - otherwise VideoMetadata is null
            VideoMetadata videoMetaData = recognitionResponse.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());
            System.out.println("Job");
            List<CelebrityRecognition> celebs = recognitionResponse.celebrities();
            for (CelebrityRecognition celeb : celebs) {
                long seconds = celeb.timestamp() / 1000;
                System.out.print("Sec: " + Long.toString(seconds) + " ");
                CelebrityDetail details = celeb.celebrity();
                System.out.println("Name: " + details.name());
                System.out.println("Id: " + details.id());
                System.out.println();
            }
        } while (recognitionResponse != null && recognitionResponse.nextToken() != null);
    } catch (RekognitionException | InterruptedException e) {
        System.out.println(e.getMessage());
        System.exit(1);
    }
}
Also used : RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) GetCelebrityRecognitionResponse(software.amazon.awssdk.services.rekognition.model.GetCelebrityRecognitionResponse) VideoMetadata(software.amazon.awssdk.services.rekognition.model.VideoMetadata) CelebrityRecognition(software.amazon.awssdk.services.rekognition.model.CelebrityRecognition) CelebrityDetail(software.amazon.awssdk.services.rekognition.model.CelebrityDetail) GetCelebrityRecognitionRequest(software.amazon.awssdk.services.rekognition.model.GetCelebrityRecognitionRequest)

Example 3 with RekognitionException

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

the class VideoCelebrityDetection method StartCelebrityDetection.

// snippet-start:[rekognition.java2.recognize_video_celebrity.main]
public static void StartCelebrityDetection(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();
        StartCelebrityRecognitionRequest recognitionRequest = StartCelebrityRecognitionRequest.builder().jobTag("Celebrities").notificationChannel(channel).video(vidOb).build();
        StartCelebrityRecognitionResponse startCelebrityRecognitionResult = rekClient.startCelebrityRecognition(recognitionRequest);
        startJobId = startCelebrityRecognitionResult.jobId();
    } catch (RekognitionException e) {
        System.out.println(e.getMessage());
        System.exit(1);
    }
}
Also used : RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) Video(software.amazon.awssdk.services.rekognition.model.Video) StartCelebrityRecognitionResponse(software.amazon.awssdk.services.rekognition.model.StartCelebrityRecognitionResponse) S3Object(software.amazon.awssdk.services.rekognition.model.S3Object) StartCelebrityRecognitionRequest(software.amazon.awssdk.services.rekognition.model.StartCelebrityRecognitionRequest)

Example 4 with RekognitionException

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

the class VideoDetectSegment method getSegmentResults.

public static void getSegmentResults(RekognitionClient rekClient) {
    try {
        String paginationToken = null;
        GetSegmentDetectionResponse segDetectionResponse = null;
        Boolean finished = false;
        String status = "";
        int yy = 0;
        do {
            if (segDetectionResponse != null)
                paginationToken = segDetectionResponse.nextToken();
            GetSegmentDetectionRequest recognitionRequest = GetSegmentDetectionRequest.builder().jobId(startJobId).nextToken(paginationToken).maxResults(10).build();
            // Wait until the job succeeds
            while (!finished) {
                segDetectionResponse = rekClient.getSegmentDetection(recognitionRequest);
                status = segDetectionResponse.jobStatusAsString();
                if (status.compareTo("SUCCEEDED") == 0)
                    finished = true;
                else {
                    System.out.println(yy + " status is: " + status);
                    Thread.sleep(1000);
                }
                yy++;
            }
            finished = false;
            // Proceed when the job is done - otherwise VideoMetadata is null
            List<VideoMetadata> videoMetaData = segDetectionResponse.videoMetadata();
            for (VideoMetadata metaData : videoMetaData) {
                System.out.println("Format: " + metaData.format());
                System.out.println("Codec: " + metaData.codec());
                System.out.println("Duration: " + metaData.durationMillis());
                System.out.println("FrameRate: " + metaData.frameRate());
                System.out.println("Job");
            }
            List<SegmentDetection> detectedSegment = segDetectionResponse.segments();
            String type = detectedSegment.get(0).type().toString();
            if (type.contains(SegmentType.TECHNICAL_CUE.toString())) {
                System.out.println("Technical Cue");
                TechnicalCueSegment segmentCue = detectedSegment.get(0).technicalCueSegment();
                System.out.println("\tType: " + segmentCue.type());
                System.out.println("\tConfidence: " + segmentCue.confidence().toString());
            }
            if (type.contains(SegmentType.SHOT.toString())) {
                System.out.println("Shot");
                ShotSegment segmentShot = detectedSegment.get(0).shotSegment();
                System.out.println("\tIndex " + segmentShot.index());
                System.out.println("\tConfidence: " + segmentShot.confidence().toString());
            }
            long seconds = detectedSegment.get(0).durationMillis();
            System.out.println("\tDuration : " + Long.toString(seconds) + " milliseconds");
            System.out.println("\tStart time code: " + detectedSegment.get(0).startTimecodeSMPTE());
            System.out.println("\tEnd time code: " + detectedSegment.get(0).endTimecodeSMPTE());
            System.out.println("\tDuration time code: " + detectedSegment.get(0).durationSMPTE());
            System.out.println();
        } while (segDetectionResponse != null && segDetectionResponse.nextToken() != null);
    } catch (RekognitionException | InterruptedException e) {
        System.out.println(e.getMessage());
        System.exit(1);
    }
}
Also used : GetSegmentDetectionResponse(software.amazon.awssdk.services.rekognition.model.GetSegmentDetectionResponse) RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) VideoMetadata(software.amazon.awssdk.services.rekognition.model.VideoMetadata) SegmentDetection(software.amazon.awssdk.services.rekognition.model.SegmentDetection) TechnicalCueSegment(software.amazon.awssdk.services.rekognition.model.TechnicalCueSegment) ShotSegment(software.amazon.awssdk.services.rekognition.model.ShotSegment) GetSegmentDetectionRequest(software.amazon.awssdk.services.rekognition.model.GetSegmentDetectionRequest)

Example 5 with RekognitionException

use of software.amazon.awssdk.services.rekognition.model.RekognitionException 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();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Label(software.amazon.awssdk.services.rekognition.model.Label) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Image(software.amazon.awssdk.services.rekognition.model.Image) URL(java.net.URL) DetectLabelsResponse(software.amazon.awssdk.services.rekognition.model.DetectLabelsResponse) SdkBytes(software.amazon.awssdk.core.SdkBytes) DetectLabelsRequest(software.amazon.awssdk.services.rekognition.model.DetectLabelsRequest)

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