예제 #1
0
        protected override async Task ExecuteAsync(WorkerRequest request, ProcessRekognitionResult requestInput)
        {
            var workerJobHelper =
                new WorkerJobHelper <AIJob>(
                    DbTableProvider.Table(request.TableName()),
                    ResourceManagerProvider.GetResourceManager(request),
                    request,
                    requestInput.JobAssignmentId);

            try
            {
                await workerJobHelper.InitializeAsync();

                var jobInput = workerJobHelper.JobInput;

                S3Locator outputLocation;
                if (!jobInput.TryGet <S3Locator>(nameof(outputLocation), out outputLocation))
                {
                    throw new Exception("Invalid or missing output location.");
                }

                var s3Bucket = outputLocation.AwsS3Bucket;

                var rekoJobId   = requestInput.JobInfo.RekoJobId;
                var rekoJobType = requestInput.JobInfo.RekoJobType;
                var status      = requestInput.JobInfo.Status;

                if (status != "SUCCEEDED")
                {
                    throw new Exception($"AI Rekognition failed job info: rekognition status:" + status);
                }

                object data = null;
                switch (rekoJobType)
                {
                case "StartCelebrityRecognition":
                    using (var rekognitionClient = new AmazonRekognitionClient())
                        data = await rekognitionClient.GetCelebrityRecognitionAsync(new GetCelebrityRecognitionRequest
                        {
                            JobId      = rekoJobId, /* required */
                            MaxResults = 1000000,
                            SortBy     = "TIMESTAMP"
                        });
                    break;

                case "StartLabelDetection":
                    throw new NotImplementedException("StartLabelDetection has not yet been implemented");

                case "StartContentModeration":
                    throw new NotImplementedException("StartContentModeration has not yet been implemented");

                case "StartPersonTracking":
                    throw new NotImplementedException("StartPersonTracking has not yet been implemented");

                case "StartFaceDetection":
                    throw new NotImplementedException("StartLabelDetection has not yet been implemented");

                case "StartFaceSearch":
                    throw new NotImplementedException("StartLabelDetection has not yet been implemented");

                default:
                    throw new Exception($"Unknown job type '{rekoJobType}'");
                }

                if (data == null)
                {
                    throw new Exception($"No data was returned by AWS Rekognition");
                }

                var newS3Key = $"reko_{Guid.NewGuid()}.json";
                var s3Params = new PutObjectRequest
                {
                    BucketName  = outputLocation.AwsS3Bucket,
                    Key         = newS3Key,
                    ContentBody = data.ToMcmaJson().ToString(),
                    ContentType = "application/json"
                };

                try
                {
                    var destS3 = await outputLocation.GetClientAsync();

                    await destS3.PutObjectAsync(s3Params);
                }
                catch (Exception error)
                {
                    Logger.Error("Unable to write output file to bucket '" + s3Bucket + "' with key '" + newS3Key + "'");
                    Logger.Exception(error);
                }

                workerJobHelper.JobOutput["outputFile"] = new S3Locator
                {
                    AwsS3Bucket = s3Bucket,
                    AwsS3Key    = newS3Key
                };

                await workerJobHelper.CompleteAsync();
            }
            catch (Exception ex)
            {
                Logger.Exception(ex);
                try
                {
                    await workerJobHelper.FailAsync(ex.ToString());
                }
                catch (Exception innerEx)
                {
                    Logger.Exception(innerEx);
                }
            }
        }
예제 #2
0
        public static async Task ProcessRekognitionResultAsync(AwsAiServiceWorkerRequest @event)
        {
            var resourceManager = @event.GetAwsV4ResourceManager();
            var table           = new DynamoDbTable(@event.StageVariables["TableName"]);
            var jobAssignmentId = @event.JobAssignmentId;

            var job = await RetrieveJobAsync(resourceManager, table, jobAssignmentId);

            try
            {
                var jobInput = job.JobInput;

                S3Locator outputLocation;
                if (!jobInput.TryGet <S3Locator>(nameof(outputLocation), out outputLocation))
                {
                    throw new Exception("Invalid or missing output location.");
                }

                var s3Bucket = outputLocation.AwsS3Bucket;

                var rekoJobId   = @event.JobExternalInfo.RekoJobId;
                var rekoJobType = @event.JobExternalInfo.RekoJobType;
                var status      = @event.JobExternalInfo.Status;

                if (status != "SUCCEEDED")
                {
                    throw new Exception($"AI Rekognition failed job info: rekognition status:" + status);
                }

                object data = null;
                switch (rekoJobType)
                {
                case "StartCelebrityRecognition":
                    var getCelebRecognitionParams = new GetCelebrityRecognitionRequest
                    {
                        JobId      = rekoJobId, /* required */
                        MaxResults = 1000000,
                        SortBy     = "TIMESTAMP"
                    };

                    var rekognitionClient = new AmazonRekognitionClient();
                    data = await rekognitionClient.GetCelebrityRecognitionAsync(getCelebRecognitionParams);

                    break;

                case "StartLabelDetection":
                    throw new NotImplementedException("StartLabelDetection has not yet been implemented");

                case "StartContentModeration":
                    throw new NotImplementedException("StartContentModeration has not yet been implemented");

                case "StartPersonTracking":
                    throw new NotImplementedException("StartPersonTracking has not yet been implemented");

                case "StartFaceDetection":
                    throw new NotImplementedException("StartLabelDetection has not yet been implemented");

                case "StartFaceSearch":
                    throw new NotImplementedException("StartLabelDetection has not yet been implemented");

                default:
                    throw new Exception($"Unknown job type '{rekoJobType}'");
                }

                if (data == null)
                {
                    throw new Exception($"No data was returned by AWS Rekognition");
                }

                var newS3Key = $"reko_{Guid.NewGuid()}.json";
                var s3Params = new PutObjectRequest
                {
                    BucketName  = outputLocation.AwsS3Bucket,
                    Key         = newS3Key,
                    ContentBody = data.ToMcmaJson().ToString(),
                    ContentType = "application/json"
                };

                try
                {
                    var destS3 = await outputLocation.GetClientAsync();

                    await destS3.PutObjectAsync(s3Params);
                }
                catch (Exception error)
                {
                    Logger.Error("Unable to write output file to bucket '" + s3Bucket + "' with key '" + newS3Key + "'");
                    Logger.Exception(error);
                }

                var jobOutput = new JobParameterBag();
                jobOutput["outputFile"] = new S3Locator
                {
                    AwsS3Bucket = s3Bucket,
                    AwsS3Key    = newS3Key
                };

                await UpdateJobAssignmentWithOutputAsync(table, jobAssignmentId, jobOutput);
                await UpdateJobAssignmentStatusAsync(resourceManager, table, jobAssignmentId, "COMPLETED");
            }
            catch (Exception error)
            {
                Logger.Exception(error);

                try
                {
                    await UpdateJobAssignmentStatusAsync(resourceManager, table, jobAssignmentId, "FAILED", error.ToString());
                }
                catch (Exception innerError)
                {
                    Logger.Exception(innerError);
                }
            }
        }