/// <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); }
/// <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 FunctionHandler(InputEvent input, ILambdaContext context) { var logger = new ImageRecognitionLogger(input, context); var thumbnail = JsonSerializer.Deserialize <Thumbnail>(JsonSerializer.Serialize(input.ParallelResults[1])); var labels = JsonSerializer.Deserialize <List <Label> >(JsonSerializer.Serialize(input.ParallelResults[0])); var photoUpdate = new Photo { PhotoId = WebUtility.UrlDecode(input.PhotoId), ProcessingStatus = ProcessingStatus.Succeeded, FullSize = new PhotoImage { Key = WebUtility.UrlDecode(input.SourceKey), Width = input.ExtractedMetadata?.Dimensions?.Width, Height = input.ExtractedMetadata?.Dimensions?.Height }, Format = input.ExtractedMetadata?.Format, ExifMake = input.ExtractedMetadata?.ExifMake, ExifModel = input.ExtractedMetadata?.ExifModel, Thumbnail = new PhotoImage { Key = WebUtility.UrlDecode(thumbnail?.s3key), Width = thumbnail?.width, Height = thumbnail?.height }, ObjectDetected = labels.Select(l => l.Name).ToArray(), GeoLocation = input.ExtractedMetadata?.Geo, UpdatedDate = DateTime.UtcNow }; // update photo table. await _ddbContext.SaveAsync(photoUpdate).ConfigureAwait(false); var data = JsonSerializer.Serialize(photoUpdate); await logger.WriteMessageAsync( new MessageEvent { Message = "Photo recognition metadata stored succesfully", Data = data, CompleteEvent = true }, ImageRecognitionLogger.Target.All); Console.WriteLine(data); }
/// <summary> /// A simple function that takes a s3 bucket input and extract metadata of Image. /// </summary> /// <param name="input"></param> /// <param name="context"></param> /// <returns></returns> public async Task <ImageMetadata> FunctionHandler(ExecutionInput state, ILambdaContext context) { var logger = new ImageRecognitionLogger(state, context); var srcKey = WebUtility.UrlDecode(state.SourceKey); var tmpPath = Path.Combine(Path.GetTempPath(), Path.GetFileName(srcKey)); try { var metadata = new ImageMetadata(); using (var response = await S3Client.GetObjectAsync(state.Bucket, srcKey)) { using (var sourceImage = Image.Load(response.ResponseStream, out var format)) { metadata.OriginalImagePixelCount = sourceImage.Width * sourceImage.Height; metadata.Width = sourceImage.Width; metadata.Height = sourceImage.Height; metadata.ExifProfile = sourceImage.Metadata.ExifProfile; metadata.Format = format.Name; } } await logger.WriteMessageAsync(new MessageEvent { Message = "Photo metadata extracted succesfully" }, ImageRecognitionLogger.Target.All); return(metadata); } finally { if (File.Exists(tmpPath)) { File.Delete(tmpPath); } } }
/// <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 <ThumbnailInfo> FunctionHandler(Input input, ILambdaContext context) { var logger = new ImageRecognitionLogger(input, context); var size = input.ExtractedMetadata.Dimensions; var scalingFactor = Math.Min( MAX_WIDTH / size.Width, MAX_HEIGHT / size.Height ); var width = Convert.ToInt32(scalingFactor * size.Width); var height = Convert.ToInt32(scalingFactor * size.Height); var image = await GenerateThumbnail(input.Bucket, input.SourceKey, width, height); var destinationKey = input.SourceKey.Replace("uploads", "resized"); using (var stream = new MemoryStream()) { await image.thumbnailImage.SaveAsync(stream, image.format); stream.Position = 0; context.Logger.LogLine($"Saving thumbnail to {destinationKey} with size {stream.Length}"); await S3Client.PutObjectAsync(new PutObjectRequest { BucketName = input.Bucket, Key = destinationKey, InputStream = stream }); await logger.WriteMessageAsync(new MessageEvent { Message = "Photo thumbnail created" }, ImageRecognitionLogger.Target.All); return(new ThumbnailInfo(width, height, destinationKey, input.Bucket)); } }