private static void StartAnalyizingVideos(AmazonRekognitionClient client, List <string> videoNames) { foreach (string video in videoNames) { StartLabelDetectionRequest startLabelDetectionRequest = new StartLabelDetectionRequest() { Video = new Video { S3Object = new S3Object { Bucket = bucketName, Name = video } }, MinConfidence = 90 }; StartLabelDetectionResponse response = client.StartLabelDetection(startLabelDetectionRequest); startJobId = response.JobId; VideoEntity videoEntity = new VideoEntity(); videoEntity.ID = response.JobId; videoEntity.Name = video; videoEntity.IsAnalyzed = false; listOfVideoObjects.Add(videoEntity); } Console.WriteLine("Analysis of " + listOfVideoObjects.Count + " videos started. Waiting 45 seconds before checking for results..."); }
/// <summary> /// Unmarshaller the response from the service to the response class. /// </summary> /// <param name="context"></param> /// <returns></returns> public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context) { StartLabelDetectionResponse response = new StartLabelDetectionResponse(); context.Read(); int targetDepth = context.CurrentDepth; while (context.ReadAtDepth(targetDepth)) { if (context.TestExpression("JobId", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.JobId = unmarshaller.Unmarshall(context); continue; } } return(response); }
private async Task <bool> ProcessVideoMessages(ILambdaContext context, StartLabelDetectionResponse response) { bool validLength; do { ReceiveMessageResponse messageResponse = await sqsClient.ReceiveMessageAsync(new ReceiveMessageRequest { QueueUrl = QUrl, MaxNumberOfMessages = 10, WaitTimeSeconds = 15 }).ConfigureAwait(false); if (!messageResponse.Messages.Any()) { continue; } Message message = messageResponse.Messages.SingleOrDefault(x => x.Body.Contains(response.JobId)); if (message == null) { context.Logger.LogLine("job received is not of interest"); messageResponse.Messages.ForEach(async msg => await sqsClient.DeleteMessageAsync(new DeleteMessageRequest { QueueUrl = QUrl, ReceiptHandle = msg.ReceiptHandle })); } else { validLength = await ProcessInterestedMessage(context, response, message).ConfigureAwait(false); break; } } while (true); return(validLength); }
public async Task <bool> FunctionHandler(FileInfo fileInfo, ILambdaContext context) { StartLabelDetectionRequest request = new StartLabelDetectionRequest() { Video = new Video { S3Object = new S3Object { Bucket = fileInfo.Bucket, Name = fileInfo.Key } }, MinConfidence = 40.0f, NotificationChannel = new NotificationChannel { RoleArn = "arn:aws:iam::518495728486:role/hackathon-rek-role", SNSTopicArn = "arn:aws:sns:us-east-1:518495728486:AmazonRekognition-hackathon-2018" } }; StartLabelDetectionResponse response = await rekClient.StartLabelDetectionAsync(request).ConfigureAwait(false); bool validLength = await ProcessVideoMessages(context, response); return(validLength); }
private async Task <bool> ProcessInterestedMessage(ILambdaContext context, StartLabelDetectionResponse response, Message message) { bool validLength = false; if (message.Body.Contains("SUCCEEDED")) { context.Logger.LogLine( $"job with jobId {response.JobId} found and succeeded, continuing to process remaining messages"); validLength = await this.ProcessLabels(context, response.JobId).ConfigureAwait(false); } else { context.Logger.LogLine($"job with jobId {response.JobId} found and failed, please check cloud watch logs for more details"); } await sqsClient.DeleteMessageAsync(new DeleteMessageRequest { QueueUrl = QUrl, ReceiptHandle = message.ReceiptHandle }).ConfigureAwait(false); return(validLength); }
static void Main(string[] args) { try { // Constructs a SharedCredentialsFile object from the default credentials file. SharedCredentialsFile sharedCredentialsFile = new SharedCredentialsFile(); // Get the [default] profile from the credentials file. CredentialProfile defaultProfile = GetDefaultProfile(sharedCredentialsFile); if (defaultProfile != null) { // Get the credentials (access key, secret access key, etc.) AWSCredentials credentials = AWSCredentialsFactory.GetAWSCredentials(defaultProfile, new SharedCredentialsFile()); AmazonRekognitionClient rekognitionClient = new AmazonRekognitionClient(credentials, RegionEndpoint.USEast1); foreach (VideoItem vi in videosList) { Video v = new Video() { S3Object = GetVideo(vi.FileName) }; StartLabelDetectionRequest request = new StartLabelDetectionRequest() { Video = v, MinConfidence = 90 }; StartLabelDetectionResponse response = rekognitionClient.StartLabelDetection(request); if (response != null) { vi.JobId = response.JobId; } } Console.WriteLine("Analyses of all {0} videos started. Waiting for 45 seconds before start checking for results...", videosList.Length); Console.WriteLine(); // Sleep for 45 seconds System.Threading.Thread.Sleep(45000); while (completeCount < videosList.Length) { foreach (VideoItem vi in videosList) { if (!vi.AnalysisComplete) { GetLabelDetectionRequest labelDetectionRequest = new GetLabelDetectionRequest() { JobId = vi.JobId, MaxResults = 5 }; GetLabelDetectionResponse labelDetectionResponse = rekognitionClient.GetLabelDetection(labelDetectionRequest); StringBuilder sb = new StringBuilder(); sb.Append(vi.FileName + ": "); if (labelDetectionResponse.JobStatus == VideoJobStatus.SUCCEEDED) { vi.AnalysisComplete = true; vi.AnalysisSucceeded = true; completeCount++; Console.ForegroundColor = ConsoleColor.Green; sb.Append("Analysis succeeded"); vi.Labels = labelDetectionResponse.Labels; } else if (labelDetectionResponse.JobStatus == VideoJobStatus.FAILED) { vi.AnalysisComplete = true; vi.AnalysisSucceeded = false; completeCount++; Console.ForegroundColor = ConsoleColor.Red; sb.Append("Analysis failed"); } else if (labelDetectionResponse.JobStatus == VideoJobStatus.IN_PROGRESS) { // Nothing to do Console.ForegroundColor = ConsoleColor.Yellow; sb.Append("Analysis in progress..."); } Console.WriteLine(sb.ToString()); } if (completeCount == videosList.Length) { Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(); Console.WriteLine("Done analyzing all {0} videos.", videosList.Length); Console.WriteLine("Printing results:", videosList.Length); Console.WriteLine("----------------------------------------------------"); Console.WriteLine(); foreach (VideoItem video in videosList) { StringBuilder sb = new StringBuilder(); sb.AppendLine(video.FileName + ":"); sb.AppendLine(); foreach (LabelDetection lbl in video.Labels) { sb.AppendLine(lbl.Label.Name + " (" + lbl.Label.Confidence + " %)"); } Console.WriteLine(sb.ToString()); } break; } else { // Sleep for 5 seconds before checking the next video System.Threading.Thread.Sleep(5000); } } Console.WriteLine(); } } else { Console.WriteLine("AWS [default] profile not found"); } } catch (AmazonRekognitionException ex) { Console.WriteLine("AWS Rekognition ERROR: {0}", ex.Message); } catch (Exception ex) { Console.WriteLine("ERROR: {0}", ex.Message); } Console.ReadLine(); }