/*******************************************************************************
* 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
{
///
/// Returns an array of celebrities recognized in the input image. For more information,
/// see Recognizing celebrities in the Amazon Rekognition Developer Guide.
///
///
/// RecognizeCelebrities
returns the 64 largest faces in the image. It lists
/// the recognized celebrities in the CelebrityFaces
array and any unrecognized
/// faces in the UnrecognizedFaces
array. RecognizeCelebrities
/// doesn't return celebrities whose faces aren't among the largest 64 faces in the image.
///
/// For each celebrity recognized, RecognizeCelebrities
returns a Celebrity
/// object. The Celebrity
object contains the celebrity name, ID, URL links
/// to additional information, match confidence, and a ComparedFace
object
/// that you can use to locate the celebrity's face on the image.
///
/// Amazon Rekognition doesn't retain information about which images a celebrity has been
/// recognized in. Your application must store this information and use the Celebrity
/// ID property as a unique identifier for the celebrity. If you don't store the celebrity
/// name or additional information URLs returned by RecognizeCelebrities
,
/// you will need the ID to identify the celebrity in a call to the GetCelebrityInfo
/// operation.
///
/// You pass the input image either 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.
///
/// For an example, see Recognizing celebrities in an image in the Amazon Rekognition
/// Developer Guide.
///
/// This operation requires permissions to perform the rekognition:RecognizeCelebrities
/// operation.
///
///
[Cmdlet("Find", "REKCelebrity")]
[OutputType("Amazon.Rekognition.Model.RecognizeCelebritiesResponse")]
[AWSCmdlet("Calls the Amazon Rekognition RecognizeCelebrities API operation.", Operation = new[] {"RecognizeCelebrities"}, SelectReturnType = typeof(Amazon.Rekognition.Model.RecognizeCelebritiesResponse))]
[AWSCmdletOutput("Amazon.Rekognition.Model.RecognizeCelebritiesResponse",
"This cmdlet returns an Amazon.Rekognition.Model.RecognizeCelebritiesResponse object containing multiple properties. The object can also be referenced from properties attached to the cmdlet entry in the $AWSHistory stack."
)]
public partial class FindREKCelebrityCmdlet : 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 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.RecognizeCelebritiesResponse).
/// Specifying the name of a property of type Amazon.Rekognition.Model.RecognizeCelebritiesResponse 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));
}
context.ImageContent = this.ImageContent;
context.ImageBucket = this.ImageBucket;
context.ImageName = this.ImageName;
context.ImageVersion = this.ImageVersion;
// 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.RecognizeCelebritiesRequest();
// 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;
}
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.RecognizeCelebritiesResponse CallAWSServiceOperation(IAmazonRekognition client, Amazon.Rekognition.Model.RecognizeCelebritiesRequest request)
{
Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "Amazon Rekognition", "RecognizeCelebrities");
try
{
#if DESKTOP
return client.RecognizeCelebrities(request);
#elif CORECLR
return client.RecognizeCelebritiesAsync(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 byte[] ImageContent { get; set; }
public System.String ImageBucket { get; set; }
public System.String ImageName { get; set; }
public System.String ImageVersion { get; set; }
public System.Func Select { get; set; } =
(response, cmdlet) => response;
}
}
}