public async Task <NumberPlateTrigger> FunctionHandler(S3Event evnt, ILambdaContext context)
        {
            var s3Event = evnt.Records?[0].S3;

            context.Logger.LogLine("EVENT Received: " + JsonConvert.SerializeObject(s3Event));

            if (regExNumberPlate == null)
            {
                context.Logger.LogLine("regExNumberPlate is not yet populated. Calling getNumberPlateFromSecretsManager()...");
                regExNumberPlate = await getNumberPlateFromSecretsManager(context);

                context.Logger.LogLine("regExNumberPlate is " + regExNumberPlate);
            }

            NumberPlateTrigger result = new NumberPlateTrigger
            {
                bucket        = s3Event.Bucket.Name,
                key           = s3Event.Object.Key,
                contentType   = "",
                contentLength = s3Event.Object.Size,
                charge        = int.Parse(Environment.GetEnvironmentVariable("TollgateCharge")),
                numberPlate   = new NumberPlate()
                {
                    numberPlateRegEx = this.regExNumberPlate,
                    detected         = false
                }
            };

            AWSXRayRecorder recorder = AWSXRayRecorder.Instance;

            recorder.BeginSubsegment("TollGantry::Detect Number Plate in Captured Image");
            recorder.AddMetadata("bucket", s3Event.Bucket.Name);
            recorder.AddMetadata("key", s3Event.Object.Key);
            recorder.AddMetadata("regex", this.regExNumberPlate);
            //
            // TODO: Call Rekognition to detect text in the captured image
            //
            recorder.EndSubsegment();

            //
            // Kick off the step function
            //
            context.Logger.LogLine("Starting the state machine");
            IAmazonStepFunctions stepFunctionsClient = new AmazonStepFunctionsClient();
            await stepFunctionsClient.StartExecutionAsync(new StartExecutionRequest()
            {
                StateMachineArn = Environment.GetEnvironmentVariable("NumberPlateProcessStateMachine"), Input = JsonConvert.SerializeObject(result)
            });

            context.Logger.LogLine("State machine started");
            return(result);
        }
Exemplo n.º 2
0
        public async Task <NumberPlateTrigger> FunctionHandler(S3Event evnt, ILambdaContext context)
        {
            var s3Event = evnt.Records?[0].S3;

            context.Logger.LogLine("EVENT Received: " + JsonConvert.SerializeObject(s3Event));

            if (regExNumberPlate == null)
            {
                context.Logger.LogLine("regExNumberPlate is not yet populated. Calling getNumberPlateFromSecretsManager()...");
                regExNumberPlate = await getNumberPlateFromSecretsManager(context);

                context.Logger.LogLine("regExNumberPlate is " + regExNumberPlate);
            }

            //var response = await this.S3Client.GetObjectMetadataAsync(s3Event.Bucket.Name, s3Event.Object.Key);
            //return response.Headers.ContentType;

            NumberPlateTrigger result = new NumberPlateTrigger
            {
                bucket        = s3Event.Bucket.Name,
                key           = s3Event.Object.Key,
                contentType   = "",
                contentLength = s3Event.Object.Size,
                charge        = int.Parse(Environment.GetEnvironmentVariable("TollgateCharge")),
                numberPlate   = new NumberPlate()
                {
                    numberPlateRegEx = this.regExNumberPlate,
                    detected         = false
                }
            };

            recorder.BeginSubsegment("TollGantry::Detect Number Plate in Captured Image");
            recorder.AddMetadata("bucket", s3Event.Bucket.Name);
            recorder.AddMetadata("key", s3Event.Object.Key);
            recorder.AddMetadata("regex", this.regExNumberPlate);

            S3Object s3Object = new S3Object();

            s3Object.Bucket = s3Event.Bucket.Name;
            s3Object.Name   = s3Event.Object.Key;
            DetectTextRequest detectTextReq = new DetectTextRequest {
                Image = new Image {
                    S3Object = s3Object
                }
            };

            context.Logger.LogLine("Calling Rekognition ... ");
            DetectTextResponse detectTextResponse = await rekognitionClient.DetectTextAsync(detectTextReq);

            context.Logger.LogLine($"Response from Rekognition: {JsonConvert.SerializeObject(detectTextResponse)}");

            // Check if the a valid number was detected...
            foreach (var textItem in detectTextResponse.TextDetections)
            {
                if (!result.numberPlate.detected && textItem.Type.Value == "LINE" && textItem.Confidence > float.Parse(Environment.GetEnvironmentVariable("RekognitionTextMinConfidence")))
                {
                    Regex           regex   = new Regex(regExNumberPlate);
                    MatchCollection matches = regex.Matches(textItem.DetectedText);
                    context.Logger.LogLine($"Matches collection: {matches.Count}");
                    string plateNumber = "";
                    foreach (Match match in matches)
                    {
                        plateNumber += (match.Groups[1].Value + match.Groups[2].Value);
                    }
                    if (!string.IsNullOrEmpty(plateNumber))
                    {
                        result.numberPlate.detected          = true;
                        result.numberPlate.confidence        = textItem.Confidence;
                        result.numberPlate.numberPlateString = plateNumber;
                        context.Logger.LogLine($"A valid plate number was detected ({result.numberPlate.numberPlateString})");
                    }
                }
            }

            recorder.EndSubsegment();

            //
            // At this point, we either know it is a valid number plate
            // or it couldn't be determined with adequate confidence
            // so we need manual intervention
            //

            //
            // Kick off the step function
            //

            context.Logger.LogLine("Starting the state machine");
            //TODO: add code to start the state machine
            context.Logger.LogLine("State machine started");

            return(result);
        }