/*******************************************************************************
* Copyright 2012-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
* this file except in compliance with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file.
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
* *****************************************************************************
*
* AWS Tools for Windows (TM) PowerShell (TM)
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using Amazon.PowerShell.Common;
using Amazon.Runtime;
using Amazon.Rekognition;
using Amazon.Rekognition.Model;
namespace Amazon.PowerShell.Cmdlets.REK
{
///
/// Detects instances of real-world entities within an image (JPEG or PNG) provided as
/// input. This includes objects like flower, tree, and table; events like wedding, graduation,
/// and birthday party; and concepts like landscape, evening, and nature.
///
///
///
/// For an example, see Analyzing images stored in an Amazon S3 bucket in the Amazon Rekognition
/// Developer Guide.
///
/// You pass the input image as base64-encoded image bytes or as a reference to an image
/// in an Amazon S3 bucket. If you use the AWS CLI to call Amazon Rekognition operations,
/// passing image bytes is not supported. The image must be either a PNG or JPEG formatted
/// file.
/// Optional Parameters
/// You can specify one or both of the GENERAL_LABELS
and IMAGE_PROPERTIES
/// feature types when calling the DetectLabels API. Including GENERAL_LABELS
/// will ensure the response includes the labels detected in the input image, while including
/// IMAGE_PROPERTIES
will ensure the response includes information about
/// the image quality and color.
///
/// When using GENERAL_LABELS
and/or IMAGE_PROPERTIES
you can
/// provide filtering criteria to the Settings parameter. You can filter with sets of
/// individual labels or with label categories. You can specify inclusive filters, exclusive
/// filters, or a combination of inclusive and exclusive filters. For more information
/// on filtering see Detecting
/// Labels in an Image.
///
/// You can specify MinConfidence
to control the confidence threshold for
/// the labels returned. The default is 55%. You can also add the MaxLabels
/// parameter to limit the number of labels returned. The default and upper limit is 1000
/// labels.
/// Response Elements
/// For each object, scene, and concept the API returns one or more labels. The API returns
/// the following types of information about labels:
/// -
/// Name - The name of the detected label.
///
-
/// Confidence - The level of confidence in the label assigned to a detected object.
///
///
-
/// Parents - The ancestor labels for a detected label. DetectLabels returns a hierarchical
/// taxonomy of detected labels. For example, a detected car might be assigned the label
/// car. The label car has two parent labels: Vehicle (its parent) and Transportation
/// (its grandparent). The response includes the all ancestors for a label, where every
/// ancestor is a unique label. In the previous example, Car, Vehicle, and Transportation
/// are returned as unique labels in the response.
///
-
/// Aliases - Possible Aliases for the label.
///
-
/// Categories - The label categories that the detected label belongs to.
///
-
/// BoundingBox — Bounding boxes are described for all instances of detected common object
/// labels, returned in an array of Instance objects. An Instance object contains a BoundingBox
/// object, describing the location of the label on the input image. It also includes
/// the confidence for the accuracy of the detected bounding box.
///
/// The API returns the following information regarding the image, as part of the ImageProperties
/// structure:
/// -
/// Quality - Information about the Sharpness, Brightness, and Contrast of the input image,
/// scored between 0 to 100. Image quality is returned for the entire image, as well as
/// the background and the foreground.
///
-
/// Dominant Color - An array of the dominant colors in the image.
///
-
/// Foreground - Information about the sharpness, brightness, and dominant colors of the
/// input image’s foreground.
///
-
/// Background - Information about the sharpness, brightness, and dominant colors of the
/// input image’s background.
///
/// The list of returned labels will include at least one label for every detected object,
/// along with information about that label. In the following example, suppose the input
/// image has a lighthouse, the sea, and a rock. The response includes all three labels,
/// one for each object, as well as the confidence in the label:
/// {Name: lighthouse, Confidence: 98.4629}
{Name: rock,Confidence: 79.2097}
{Name: sea,Confidence: 75.061}
/// The list of labels can include multiple labels for the same object. For example, if
/// the input image shows a flower (for example, a tulip), the operation might return
/// the following three labels.
/// {Name: flower,Confidence: 99.0562}
{Name: plant,Confidence: 99.0562}
{Name: tulip,Confidence: 99.0562}
/// In this example, the detection algorithm more precisely identifies the flower as a
/// tulip.
///
/// If the object detected is a person, the operation doesn't provide the same facial
/// details that the DetectFaces operation provides.
///
/// This is a stateless API operation that doesn't return any data.
///
/// This operation requires permissions to perform the rekognition:DetectLabels
/// action.
///
///
[Cmdlet("Find", "REKLabel")]
[OutputType("Amazon.Rekognition.Model.DetectLabelsResponse")]
[AWSCmdlet("Calls the Amazon Rekognition DetectLabels API operation.", Operation = new[] {"DetectLabels"}, SelectReturnType = typeof(Amazon.Rekognition.Model.DetectLabelsResponse))]
[AWSCmdletOutput("Amazon.Rekognition.Model.DetectLabelsResponse",
"This cmdlet returns an Amazon.Rekognition.Model.DetectLabelsResponse object containing multiple properties. The object can also be referenced from properties attached to the cmdlet entry in the $AWSHistory stack."
)]
public partial class FindREKLabelCmdlet : AmazonRekognitionClientCmdlet, IExecutor
{
#region Parameter ImageBucket
///
///
/// Name of the S3 bucket.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String ImageBucket { get; set; }
#endregion
#region Parameter ImageContent
///
///
/// Blob of image bytes up to 5 MBs. Note that the maximum image size you can pass to
/// DetectCustomLabels
is 4MB.
///
/// The cmdlet will automatically convert the supplied parameter of type string, string[], System.IO.FileInfo or System.IO.Stream to byte[] before supplying it to the service.
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Amazon.PowerShell.Common.MemoryStreamParameterConverter]
public byte[] ImageContent { get; set; }
#endregion
#region Parameter Feature
///
///
/// A list of the types of analysis to perform. Specifying GENERAL_LABELS uses the label
/// detection feature, while specifying IMAGE_PROPERTIES returns information regarding
/// image color and quality. If no option is specified GENERAL_LABELS is used by default.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("Features")]
public System.String[] Feature { get; set; }
#endregion
#region Parameter GeneralLabels_LabelCategoryExclusionFilter
///
///
/// The label categories that should be excluded from the return from DetectLabels.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("Settings_GeneralLabels_LabelCategoryExclusionFilters")]
public System.String[] GeneralLabels_LabelCategoryExclusionFilter { get; set; }
#endregion
#region Parameter GeneralLabels_LabelCategoryInclusionFilter
///
///
/// The label categories that should be included in the return from DetectLabels.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("Settings_GeneralLabels_LabelCategoryInclusionFilters")]
public System.String[] GeneralLabels_LabelCategoryInclusionFilter { get; set; }
#endregion
#region Parameter GeneralLabels_LabelExclusionFilter
///
///
/// The labels that should be excluded from the return from DetectLabels.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("Settings_GeneralLabels_LabelExclusionFilters")]
public System.String[] GeneralLabels_LabelExclusionFilter { get; set; }
#endregion
#region Parameter GeneralLabels_LabelInclusionFilter
///
///
/// The labels that should be included in the return from DetectLabels.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("Settings_GeneralLabels_LabelInclusionFilters")]
public System.String[] GeneralLabels_LabelInclusionFilter { get; set; }
#endregion
#region Parameter ImageProperties_MaxDominantColor
///
///
/// The maximum number of dominant colors to return when detecting labels in an image.
/// The default value is 10.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("Settings_ImageProperties_MaxDominantColors")]
public System.Int32? ImageProperties_MaxDominantColor { get; set; }
#endregion
#region Parameter MaxLabel
///
///
/// Maximum number of labels you want the service to return in the response. The service
/// returns the specified number of highest confidence labels.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("MaxLabels")]
public System.Int32? MaxLabel { get; set; }
#endregion
#region Parameter MinConfidence
///
///
/// Specifies the minimum confidence level for the labels to return. Amazon Rekognition
/// doesn't return any labels with confidence lower than this specified value.If MinConfidence
is not specified, the operation returns labels with
/// a confidence values greater than or equal to 55 percent.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.Single? MinConfidence { get; set; }
#endregion
#region Parameter ImageName
///
///
/// S3 object key name.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String ImageName { get; set; }
#endregion
#region Parameter ImageVersion
///
///
/// If the bucket is versioning enabled, you can specify the object version.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String ImageVersion { get; set; }
#endregion
#region Parameter Select
///
/// Use the -Select parameter to control the cmdlet output. The default value is '*'.
/// Specifying -Select '*' will result in the cmdlet returning the whole service response (Amazon.Rekognition.Model.DetectLabelsResponse).
/// Specifying the name of a property of type Amazon.Rekognition.Model.DetectLabelsResponse will result in that property being returned.
/// Specifying -Select '^ParameterName' will result in the cmdlet returning the selected cmdlet parameter value.
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public string Select { get; set; } = "*";
#endregion
protected override void ProcessRecord()
{
this._AWSSignerType = "v4";
base.ProcessRecord();
var context = new CmdletContext();
// allow for manipulation of parameters prior to loading into context
PreExecutionContextLoad(context);
if (ParameterWasBound(nameof(this.Select)))
{
context.Select = CreateSelectDelegate(Select) ??
throw new System.ArgumentException("Invalid value for -Select parameter.", nameof(this.Select));
}
if (this.Feature != null)
{
context.Feature = new List(this.Feature);
}
context.ImageContent = this.ImageContent;
context.ImageBucket = this.ImageBucket;
context.ImageName = this.ImageName;
context.ImageVersion = this.ImageVersion;
context.MaxLabel = this.MaxLabel;
context.MinConfidence = this.MinConfidence;
if (this.GeneralLabels_LabelCategoryExclusionFilter != null)
{
context.GeneralLabels_LabelCategoryExclusionFilter = new List(this.GeneralLabels_LabelCategoryExclusionFilter);
}
if (this.GeneralLabels_LabelCategoryInclusionFilter != null)
{
context.GeneralLabels_LabelCategoryInclusionFilter = new List(this.GeneralLabels_LabelCategoryInclusionFilter);
}
if (this.GeneralLabels_LabelExclusionFilter != null)
{
context.GeneralLabels_LabelExclusionFilter = new List(this.GeneralLabels_LabelExclusionFilter);
}
if (this.GeneralLabels_LabelInclusionFilter != null)
{
context.GeneralLabels_LabelInclusionFilter = new List(this.GeneralLabels_LabelInclusionFilter);
}
context.ImageProperties_MaxDominantColor = this.ImageProperties_MaxDominantColor;
// allow further manipulation of loaded context prior to processing
PostExecutionContextLoad(context);
var output = Execute(context) as CmdletOutput;
ProcessOutput(output);
}
#region IExecutor Members
public object Execute(ExecutorContext context)
{
System.IO.MemoryStream _ImageContentStream = null;
try
{
var cmdletContext = context as CmdletContext;
// create request
var request = new Amazon.Rekognition.Model.DetectLabelsRequest();
if (cmdletContext.Feature != null)
{
request.Features = cmdletContext.Feature;
}
// populate Image
var requestImageIsNull = true;
request.Image = new Amazon.Rekognition.Model.Image();
System.IO.MemoryStream requestImage_imageContent = null;
if (cmdletContext.ImageContent != null)
{
_ImageContentStream = new System.IO.MemoryStream(cmdletContext.ImageContent);
requestImage_imageContent = _ImageContentStream;
}
if (requestImage_imageContent != null)
{
request.Image.Bytes = requestImage_imageContent;
requestImageIsNull = false;
}
Amazon.Rekognition.Model.S3Object requestImage_image_S3Object = null;
// populate S3Object
var requestImage_image_S3ObjectIsNull = true;
requestImage_image_S3Object = new Amazon.Rekognition.Model.S3Object();
System.String requestImage_image_S3Object_imageBucket = null;
if (cmdletContext.ImageBucket != null)
{
requestImage_image_S3Object_imageBucket = cmdletContext.ImageBucket;
}
if (requestImage_image_S3Object_imageBucket != null)
{
requestImage_image_S3Object.Bucket = requestImage_image_S3Object_imageBucket;
requestImage_image_S3ObjectIsNull = false;
}
System.String requestImage_image_S3Object_imageName = null;
if (cmdletContext.ImageName != null)
{
requestImage_image_S3Object_imageName = cmdletContext.ImageName;
}
if (requestImage_image_S3Object_imageName != null)
{
requestImage_image_S3Object.Name = requestImage_image_S3Object_imageName;
requestImage_image_S3ObjectIsNull = false;
}
System.String requestImage_image_S3Object_imageVersion = null;
if (cmdletContext.ImageVersion != null)
{
requestImage_image_S3Object_imageVersion = cmdletContext.ImageVersion;
}
if (requestImage_image_S3Object_imageVersion != null)
{
requestImage_image_S3Object.Version = requestImage_image_S3Object_imageVersion;
requestImage_image_S3ObjectIsNull = false;
}
// determine if requestImage_image_S3Object should be set to null
if (requestImage_image_S3ObjectIsNull)
{
requestImage_image_S3Object = null;
}
if (requestImage_image_S3Object != null)
{
request.Image.S3Object = requestImage_image_S3Object;
requestImageIsNull = false;
}
// determine if request.Image should be set to null
if (requestImageIsNull)
{
request.Image = null;
}
if (cmdletContext.MaxLabel != null)
{
request.MaxLabels = cmdletContext.MaxLabel.Value;
}
if (cmdletContext.MinConfidence != null)
{
request.MinConfidence = cmdletContext.MinConfidence.Value;
}
// populate Settings
var requestSettingsIsNull = true;
request.Settings = new Amazon.Rekognition.Model.DetectLabelsSettings();
Amazon.Rekognition.Model.DetectLabelsImagePropertiesSettings requestSettings_settings_ImageProperties = null;
// populate ImageProperties
var requestSettings_settings_ImagePropertiesIsNull = true;
requestSettings_settings_ImageProperties = new Amazon.Rekognition.Model.DetectLabelsImagePropertiesSettings();
System.Int32? requestSettings_settings_ImageProperties_imageProperties_MaxDominantColor = null;
if (cmdletContext.ImageProperties_MaxDominantColor != null)
{
requestSettings_settings_ImageProperties_imageProperties_MaxDominantColor = cmdletContext.ImageProperties_MaxDominantColor.Value;
}
if (requestSettings_settings_ImageProperties_imageProperties_MaxDominantColor != null)
{
requestSettings_settings_ImageProperties.MaxDominantColors = requestSettings_settings_ImageProperties_imageProperties_MaxDominantColor.Value;
requestSettings_settings_ImagePropertiesIsNull = false;
}
// determine if requestSettings_settings_ImageProperties should be set to null
if (requestSettings_settings_ImagePropertiesIsNull)
{
requestSettings_settings_ImageProperties = null;
}
if (requestSettings_settings_ImageProperties != null)
{
request.Settings.ImageProperties = requestSettings_settings_ImageProperties;
requestSettingsIsNull = false;
}
Amazon.Rekognition.Model.GeneralLabelsSettings requestSettings_settings_GeneralLabels = null;
// populate GeneralLabels
var requestSettings_settings_GeneralLabelsIsNull = true;
requestSettings_settings_GeneralLabels = new Amazon.Rekognition.Model.GeneralLabelsSettings();
List requestSettings_settings_GeneralLabels_generalLabels_LabelCategoryExclusionFilter = null;
if (cmdletContext.GeneralLabels_LabelCategoryExclusionFilter != null)
{
requestSettings_settings_GeneralLabels_generalLabels_LabelCategoryExclusionFilter = cmdletContext.GeneralLabels_LabelCategoryExclusionFilter;
}
if (requestSettings_settings_GeneralLabels_generalLabels_LabelCategoryExclusionFilter != null)
{
requestSettings_settings_GeneralLabels.LabelCategoryExclusionFilters = requestSettings_settings_GeneralLabels_generalLabels_LabelCategoryExclusionFilter;
requestSettings_settings_GeneralLabelsIsNull = false;
}
List requestSettings_settings_GeneralLabels_generalLabels_LabelCategoryInclusionFilter = null;
if (cmdletContext.GeneralLabels_LabelCategoryInclusionFilter != null)
{
requestSettings_settings_GeneralLabels_generalLabels_LabelCategoryInclusionFilter = cmdletContext.GeneralLabels_LabelCategoryInclusionFilter;
}
if (requestSettings_settings_GeneralLabels_generalLabels_LabelCategoryInclusionFilter != null)
{
requestSettings_settings_GeneralLabels.LabelCategoryInclusionFilters = requestSettings_settings_GeneralLabels_generalLabels_LabelCategoryInclusionFilter;
requestSettings_settings_GeneralLabelsIsNull = false;
}
List requestSettings_settings_GeneralLabels_generalLabels_LabelExclusionFilter = null;
if (cmdletContext.GeneralLabels_LabelExclusionFilter != null)
{
requestSettings_settings_GeneralLabels_generalLabels_LabelExclusionFilter = cmdletContext.GeneralLabels_LabelExclusionFilter;
}
if (requestSettings_settings_GeneralLabels_generalLabels_LabelExclusionFilter != null)
{
requestSettings_settings_GeneralLabels.LabelExclusionFilters = requestSettings_settings_GeneralLabels_generalLabels_LabelExclusionFilter;
requestSettings_settings_GeneralLabelsIsNull = false;
}
List requestSettings_settings_GeneralLabels_generalLabels_LabelInclusionFilter = null;
if (cmdletContext.GeneralLabels_LabelInclusionFilter != null)
{
requestSettings_settings_GeneralLabels_generalLabels_LabelInclusionFilter = cmdletContext.GeneralLabels_LabelInclusionFilter;
}
if (requestSettings_settings_GeneralLabels_generalLabels_LabelInclusionFilter != null)
{
requestSettings_settings_GeneralLabels.LabelInclusionFilters = requestSettings_settings_GeneralLabels_generalLabels_LabelInclusionFilter;
requestSettings_settings_GeneralLabelsIsNull = false;
}
// determine if requestSettings_settings_GeneralLabels should be set to null
if (requestSettings_settings_GeneralLabelsIsNull)
{
requestSettings_settings_GeneralLabels = null;
}
if (requestSettings_settings_GeneralLabels != null)
{
request.Settings.GeneralLabels = requestSettings_settings_GeneralLabels;
requestSettingsIsNull = false;
}
// determine if request.Settings should be set to null
if (requestSettingsIsNull)
{
request.Settings = null;
}
CmdletOutput output;
// issue call
var client = Client ?? CreateClient(_CurrentCredentials, _RegionEndpoint);
try
{
var response = CallAWSServiceOperation(client, request);
object pipelineOutput = null;
pipelineOutput = cmdletContext.Select(response, this);
output = new CmdletOutput
{
PipelineOutput = pipelineOutput,
ServiceResponse = response
};
}
catch (Exception e)
{
output = new CmdletOutput { ErrorResponse = e };
}
return output;
}
finally
{
if( _ImageContentStream != null)
{
_ImageContentStream.Dispose();
}
}
}
public ExecutorContext CreateContext()
{
return new CmdletContext();
}
#endregion
#region AWS Service Operation Call
private Amazon.Rekognition.Model.DetectLabelsResponse CallAWSServiceOperation(IAmazonRekognition client, Amazon.Rekognition.Model.DetectLabelsRequest request)
{
Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "Amazon Rekognition", "DetectLabels");
try
{
#if DESKTOP
return client.DetectLabels(request);
#elif CORECLR
return client.DetectLabelsAsync(request).GetAwaiter().GetResult();
#else
#error "Unknown build edition"
#endif
}
catch (AmazonServiceException exc)
{
var webException = exc.InnerException as System.Net.WebException;
if (webException != null)
{
throw new Exception(Utils.Common.FormatNameResolutionFailureMessage(client.Config, webException.Message), webException);
}
throw;
}
}
#endregion
internal partial class CmdletContext : ExecutorContext
{
public List Feature { get; set; }
public byte[] ImageContent { get; set; }
public System.String ImageBucket { get; set; }
public System.String ImageName { get; set; }
public System.String ImageVersion { get; set; }
public System.Int32? MaxLabel { get; set; }
public System.Single? MinConfidence { get; set; }
public List GeneralLabels_LabelCategoryExclusionFilter { get; set; }
public List GeneralLabels_LabelCategoryInclusionFilter { get; set; }
public List GeneralLabels_LabelExclusionFilter { get; set; }
public List GeneralLabels_LabelInclusionFilter { get; set; }
public System.Int32? ImageProperties_MaxDominantColor { get; set; }
public System.Func Select { get; set; } =
(response, cmdlet) => response;
}
}
}