use of software.amazon.awssdk.services.rekognition.model.Label 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();
}
}
use of software.amazon.awssdk.services.rekognition.model.Label 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.Label 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);
}
}
Aggregations