Example #1
0
        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...");
        }
Example #2
0
        private static async Task <object> GetVideoLabelsResult(string fileName, AmazonRekognitionClient rekognitionClient)
        {
            var startRequest = new StartLabelDetectionRequest
            {
                MinConfidence = 50,
                Video         = new Video
                {
                    S3Object = new Amazon.Rekognition.Model.S3Object
                    {
                        Bucket = bucketName,
                        Name   = fileName
                    }
                },
                JobTag = "DetectingLabels"
            };

            var startLabelDetectionResult = await rekognitionClient.StartLabelDetectionAsync(startRequest);

            return(startLabelDetectionResult.JobId);
        }
Example #3
0
        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);
        }
Example #4
0
        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();
        }
Example #5
0
        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");
            }
        }