This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task ExecuteAsync(SNSEvent evnt, ILambdaContext context) | |
{ | |
var snsData = JsonConvert.DeserializeObject<SnsRecord>(evnt.Records?[0].Sns.Message); | |
var s3Data = snsData.Records.ElementAt(0).S3; | |
using (var client = new AmazonRekognitionClient(RegionEndpoint.USEast1)) | |
{ | |
var detectedLabels = await DetectLabelsAsync(client, | |
s3Data.Bucket.Name, | |
s3Data.Object.Key); | |
await WriteToDynamoTableAsync(s3Data.Object.Key, detectedLabels); | |
context.Logger.LogLine("Operation Complete"); | |
} | |
} | |
async Task<IList<Label>> DetectLabelsAsync(AmazonRekognitionClient client, string bucketName, string keyName) | |
{ | |
var labelsRequest = new DetectLabelsRequest | |
{ | |
Image = new Image | |
{ | |
S3Object = new S3Object() | |
{ | |
Name = keyName, | |
Bucket = bucketName | |
} | |
}, | |
MaxLabels = 10, | |
MinConfidence = 80f | |
}; | |
var response = await client.DetectLabelsAsync(labelsRequest); | |
return response.Labels; | |
} | |
async Task WriteToDynamoTableAsync(string imageName, ICollection<Label> labelList) | |
{ | |
var clientConfig = new AmazonDynamoDBConfig | |
{ | |
RegionEndpoint = RegionEndpoint.USEast1 | |
}; | |
var itemDataDictionary = new Dictionary<string, AttributeValue> | |
{ | |
{ "Id", new AttributeValue { S = Guid.NewGuid().ToString() }}, | |
{ "ImageName", new AttributeValue { S = imageName } } | |
}; | |
foreach (var label in labelList) | |
{ | |
itemDataDictionary.Add(label.Name, new AttributeValue { N = label.Confidence.ToString() }); | |
} | |
using (var client = new AmazonDynamoDBClient(clientConfig)) | |
{ | |
var request = new PutItemRequest | |
{ | |
TableName = "ImageDataTable2", | |
Item = itemDataDictionary | |
}; | |
await client.PutItemAsync(request); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment