|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +using Amazon.Lambda.Core; |
| 8 | +using Amazon.Lambda.S3Events; |
| 9 | + |
| 10 | + |
| 11 | +using Amazon.Rekognition; |
| 12 | +using Amazon.Rekognition.Model; |
| 13 | + |
| 14 | +using Amazon.S3; |
| 15 | +using Amazon.S3.Model; |
| 16 | + |
| 17 | +//test |
| 18 | +// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class. |
| 19 | +[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))] |
| 20 | + |
| 21 | +namespace Assignment4AWSLambda |
| 22 | +{ |
| 23 | + public class Function |
| 24 | + { |
| 25 | + /// <summary> |
| 26 | + /// The default minimum confidence used for detecting labels. |
| 27 | + /// </summary> |
| 28 | + public const float DEFAULT_MIN_CONFIDENCE = 70f; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// The name of the environment variable to set which will override the default minimum confidence level. |
| 32 | + /// </summary> |
| 33 | + public const string MIN_CONFIDENCE_ENVIRONMENT_VARIABLE_NAME = "MinConfidence"; |
| 34 | + |
| 35 | + IAmazonS3 S3Client { get; } |
| 36 | + |
| 37 | + IAmazonRekognition RekognitionClient { get; } |
| 38 | + |
| 39 | + float MinConfidence { get; set; } = DEFAULT_MIN_CONFIDENCE; |
| 40 | + |
| 41 | + HashSet<string> SupportedImageTypes { get; } = new HashSet<string> { ".png", ".jpg", ".jpeg" }; |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Default constructor used by AWS Lambda to construct the function. Credentials and Region information will |
| 45 | + /// be set by the running Lambda environment. |
| 46 | + /// |
| 47 | + /// This constuctor will also search for the environment variable overriding the default minimum confidence level |
| 48 | + /// for label detection. |
| 49 | + /// </summary> |
| 50 | + public Function() |
| 51 | + { |
| 52 | + this.S3Client = new AmazonS3Client(); |
| 53 | + this.RekognitionClient = new AmazonRekognitionClient(); |
| 54 | + |
| 55 | + var environmentMinConfidence = System.Environment.GetEnvironmentVariable(MIN_CONFIDENCE_ENVIRONMENT_VARIABLE_NAME); |
| 56 | + if(!string.IsNullOrWhiteSpace(environmentMinConfidence)) |
| 57 | + { |
| 58 | + float value; |
| 59 | + if(float.TryParse(environmentMinConfidence, out value)) |
| 60 | + { |
| 61 | + this.MinConfidence = value; |
| 62 | + Console.WriteLine($"Setting minimum confidence to {this.MinConfidence}"); |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + Console.WriteLine($"Failed to parse value {environmentMinConfidence} for minimum confidence. Reverting back to default of {this.MinConfidence}"); |
| 67 | + } |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + Console.WriteLine($"Using default minimum confidence of {this.MinConfidence}"); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Constructor used for testing which will pass in the already configured service clients. |
| 77 | + /// </summary> |
| 78 | + /// <param name="s3Client"></param> |
| 79 | + /// <param name="rekognitionClient"></param> |
| 80 | + /// <param name="minConfidence"></param> |
| 81 | + public Function(IAmazonS3 s3Client, IAmazonRekognition rekognitionClient, float minConfidence) |
| 82 | + { |
| 83 | + this.S3Client = s3Client; |
| 84 | + this.RekognitionClient = rekognitionClient; |
| 85 | + this.MinConfidence = minConfidence; |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// A function for responding to S3 create events. It will determine if the object is an image and use Amazon Rekognition |
| 90 | + /// to detect labels and add the labels as tags on the S3 object. |
| 91 | + /// </summary> |
| 92 | + /// <param name="input"></param> |
| 93 | + /// <param name="context"></param> |
| 94 | + /// <returns></returns> |
| 95 | + public async Task FunctionHandler(S3Event input, ILambdaContext context) |
| 96 | + { |
| 97 | + foreach(var record in input.Records) |
| 98 | + { |
| 99 | + if(!SupportedImageTypes.Contains(Path.GetExtension(record.S3.Object.Key))) |
| 100 | + { |
| 101 | + Console.WriteLine($"Object {record.S3.Bucket.Name}:{record.S3.Object.Key} is not a supported image type"); |
| 102 | + continue; |
| 103 | + } |
| 104 | + |
| 105 | + Console.WriteLine($"Looking for labels in image {record.S3.Bucket.Name}:{record.S3.Object.Key}"); |
| 106 | + var detectResponses = await this.RekognitionClient.DetectLabelsAsync(new DetectLabelsRequest |
| 107 | + { |
| 108 | + MinConfidence = MinConfidence, |
| 109 | + Image = new Image |
| 110 | + { |
| 111 | + S3Object = new Amazon.Rekognition.Model.S3Object |
| 112 | + { |
| 113 | + Bucket = record.S3.Bucket.Name, |
| 114 | + Name = record.S3.Object.Key |
| 115 | + } |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + var tags = new List<Tag>(); |
| 120 | + foreach(var label in detectResponses.Labels) |
| 121 | + { |
| 122 | + if(tags.Count < 10) |
| 123 | + { |
| 124 | + Console.WriteLine($"\tFound Label {label.Name} with confidence {label.Confidence}"); |
| 125 | + tags.Add(new Tag { Key = label.Name, Value = label.Confidence.ToString() }); |
| 126 | + } |
| 127 | + else |
| 128 | + { |
| 129 | + Console.WriteLine($"\tSkipped label {label.Name} with confidence {label.Confidence} because the maximum number of tags has been reached"); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + await this.S3Client.PutObjectTaggingAsync(new PutObjectTaggingRequest |
| 134 | + { |
| 135 | + BucketName = record.S3.Bucket.Name, |
| 136 | + Key = record.S3.Object.Key, |
| 137 | + Tagging = new Tagging |
| 138 | + { |
| 139 | + TagSet = tags |
| 140 | + } |
| 141 | + }); |
| 142 | + } |
| 143 | + return; |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments