/// <summary>
        ///     A simple function that takes a string and returns both the upper and lower case version of the string.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task <List <Label> > FunctionHandler(ExecutionInput input, ILambdaContext context)
        {
            var logger = new ImageRecognitionLogger(input, context);

            Console.WriteLine($"Looking for labels in image {input.Bucket}:{input.SourceKey}");

            var key = WebUtility.UrlDecode(input.SourceKey);

            var detectResponses = await RekognitionClient.DetectLabelsAsync(new DetectLabelsRequest
            {
                MinConfidence = MinConfidence,
                MaxLabels     = MaxLabels,
                Image         = new Image
                {
                    S3Object = new S3Object
                    {
                        Bucket = input.Bucket,
                        Name   = key
                    }
                }
            });

            await logger.WriteMessageAsync(new MessageEvent { Message = "Photo labels extracted successfully" },
                                           ImageRecognitionLogger.Target.All);

            return(detectResponses.Labels);
        }
示例#2
0
        public static async Task <S3Event> FunctionHandler(S3Event input, ILambdaContext context)
        {
            var tags   = new List <Tag>();
            var record = input?.Records?.FirstOrDefault();

            if (record == null)
            {
                Console.WriteLine("Input does not contain an S3 record.");
                throw new Exception();
            }

            record.S3.Object.Key = "skateboard_resized3.jpg";

            if (!SupportedImageTypes.Contains(Path.GetExtension(record.S3.Object.Key)))
            {
                Console.WriteLine($"Object {record.S3.Bucket.Name}:{record.S3.Object.Key} is not a supported image type");
                throw new Exception();
            }

            Console.WriteLine($"Looking for labels in image {record.S3.Bucket.Name}:{record.S3.Object.Key}");

            AWSXRayRecorder.Instance.BeginSubsegment("RekognitionDetectLabels");
            var detectResponses = await RekognitionClient.DetectLabelsAsync(new DetectLabelsRequest
            {
                MinConfidence = MinConfidence,
                Image         = new Image
                {
                    S3Object = new Amazon.Rekognition.Model.S3Object
                    {
                        Bucket = record.S3.Bucket.Name,
                        Name   = record.S3.Object.Key
                    }
                }
            });

            AWSXRayRecorder.Instance.EndSubsegment();

            foreach (var label in detectResponses.Labels)
            {
                if (tags.Count < 10)
                {
                    Console.WriteLine($"\tFound Label {label.Name} with confidence {label.Confidence}");
                    tags.Add(new Tag {
                        Key = label.Name, Value = label.Confidence.ToString()
                    });
                }
                else
                {
                    Console.WriteLine($"\tSkipped label {label.Name} with confidence {label.Confidence} because the maximum number of tags has been reached");
                }
            }

            AWSXRayRecorder.Instance.BeginSubsegment("S3PutObjectTagging");
            await S3Client.PutObjectTaggingAsync(new PutObjectTaggingRequest
            {
                BucketName = record.S3.Bucket.Name,
                Key        = record.S3.Object.Key,
                Tagging    = new Tagging
                {
                    TagSet = tags
                }
            });

            AWSXRayRecorder.Instance.EndSubsegment();

            AWSXRayRecorder.Instance.BeginSubsegment("S3DeleteObjectTagging");
            await S3Client.DeleteObjectTaggingAsync(new DeleteObjectTaggingRequest
            {
                BucketName = record.S3.Bucket.Name,
                Key        = record.S3.Object.Key
            });

            AWSXRayRecorder.Instance.EndSubsegment();

            return(input);
        }