/// <summary> /// Unmarshaller the response from the service to the response class. /// </summary> /// <param name="context"></param> /// <returns></returns> public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context) { GetLabelDetectionResponse response = new GetLabelDetectionResponse(); context.Read(); int targetDepth = context.CurrentDepth; while (context.ReadAtDepth(targetDepth)) { if (context.TestExpression("JobStatus", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.JobStatus = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("LabelModelVersion", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.LabelModelVersion = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("Labels", targetDepth)) { var unmarshaller = new ListUnmarshaller <LabelDetection, LabelDetectionUnmarshaller>(LabelDetectionUnmarshaller.Instance); response.Labels = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("NextToken", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.NextToken = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("StatusMessage", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.StatusMessage = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("VideoMetadata", targetDepth)) { var unmarshaller = VideoMetadataUnmarshaller.Instance; response.VideoMetadata = unmarshaller.Unmarshall(context); continue; } } return(response); }
private static List <VideoEntity> CheckAnalyzeStatus() { List <VideoEntity> videoLabels = new List <VideoEntity>(); int sizeOfList = listOfVideoObjects.Count; System.Threading.Thread.Sleep(15000); do { foreach (VideoEntity video in listOfVideoObjects) { GetLabelDetectionRequest getLabelDetectionRequest = new GetLabelDetectionRequest { JobId = video.ID, MaxResults = 5 }; GetLabelDetectionResponse getLabelDetectionResponse = client.GetLabelDetection(getLabelDetectionRequest); string status = getLabelDetectionResponse.JobStatus; if (status.Equals("IN_PROGRESS")) { Console.WriteLine(video.Name + ": Analysis in progress..."); } else if (status.Equals("SUCCEEDED") && video.IsAnalyzed == false) { Console.WriteLine(video.Name + ": Analysis succeeded"); video.IsAnalyzed = true; video.Labels = getLabelDetectionResponse.Labels; videoLabels.Add(video); sizeOfList--; } } if (sizeOfList == 0) { break; } Console.WriteLine(); //wait 45 seconds and loop again System.Threading.Thread.Sleep(15000); } while (sizeOfList > 0); return(videoLabels); }
private async Task <bool> ProcessLabels(ILambdaContext context, string jobId) { GetLabelDetectionResponse response = null; do { GetLabelDetectionRequest request = new GetLabelDetectionRequest() { JobId = jobId, MaxResults = MaxResults, NextToken = response?.NextToken, SortBy = LabelDetectionSortBy.TIMESTAMP }; response = await this.rekClient.GetLabelDetectionAsync(request).ConfigureAwait(false); return(!response.Labels.Any() || response.Labels.All(x => x.Label.Name == "White Board" && x.Label.Confidence > 40)); } while (response?.NextToken != null); }
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(); }
public async Task StartAnalyzingVideosAsync(string key, string bucketName) { string startJobID = string.Empty; StartLabelDetectionRequest startLabelDetectionRequest = new StartLabelDetectionRequest { Video = new Video { S3Object = new S3Object { Bucket = bucketName, Name = key } }, MinConfidence = 50 }; var startLabelDetectionResponse = await client.StartLabelDetectionAsync(startLabelDetectionRequest); if (startLabelDetectionResponse != null) { startJobID = startLabelDetectionResponse.JobId; } Console.WriteLine("Analysis of {0} has started. Waiting 20 seconds to check status.", key); GetLabelDetectionRequest labelDetectionRequest = new GetLabelDetectionRequest() { JobId = startJobID }; System.Threading.Thread.Sleep(20000); bool isPackage = false; bool isComplete = false; // while job is not "successful" while (!isComplete) { GetLabelDetectionResponse labelDetectionResponse = await client.GetLabelDetectionAsync(labelDetectionRequest); if (labelDetectionResponse.JobStatus == VideoJobStatus.SUCCEEDED) { isComplete = true; Console.WriteLine("Job status is: " + labelDetectionResponse.JobStatus); foreach (var label in labelDetectionResponse.Labels) { if (label.Label.Name.Equals("Package Delivery") || label.Label.Name.Equals("Carton") || label.Label.Name.Equals("Box")) { isPackage = true; break; } } } if (isPackage) { break; } Console.WriteLine("Still analyzing..."); System.Threading.Thread.Sleep(20000); } AmazonSimpleNotificationServiceClient snsClient = new AmazonSimpleNotificationServiceClient(Amazon.RegionEndpoint.USEast1); if (isPackage) { string ARN = "arn:aws:sns:us-east-1:207457486405:PackageDelivery"; PublishRequest pubRequest = new PublishRequest { TopicArn = ARN, Message = "You have a delivery." }; await snsClient.PublishAsync(pubRequest); Console.WriteLine("You have a delivery!"); } else { Console.WriteLine("Someone's at your door but there is no delivery"); } }