Example #1
0
        /// <summary>
        /// Add All detected faces to a specific collection
        /// </summary>
        /// <param name="collectionId"></param>
        /// <param name="imageId"></param>
        /// <param name="image"></param>
        public FaceRecord AddImageToCollection(string collectionId, Amazon.Rekognition.Model.Image image)
        {
            AmazonRekognitionClient rekognitionClient = AmazonClient.GetInstance();

            //Validate that image contains only one face.
            DetectFacesResponse detectFacesResponse = rekognitionClient.DetectFaces(new Amazon.Rekognition.Model.DetectFacesRequest
            {
                Attributes = new List <string> {
                    "ALL"
                },
                Image = image
            });

            if (null != detectFacesResponse.FaceDetails && detectFacesResponse.FaceDetails.Count > 1)
            {
                throw new ArgumentNullException("Many faces in the image");
            }

            IndexFacesRequest indexFacesRequest = new IndexFacesRequest()
            {
                Image               = image,
                CollectionId        = collectionId,
                DetectionAttributes = new List <String>()
                {
                    "ALL"
                }
            };

            IndexFacesResponse indexFacesResponse = rekognitionClient.IndexFaces(indexFacesRequest);

            return(indexFacesResponse.FaceRecords.FirstOrDefault());
        }
Example #2
0
        public List <string> GetAllFacesInCollection(string collectionId)
        {
            AmazonRekognitionClient rekognitionClient = AmazonClient.GetInstance();

            ListFacesResponse listFacesResponse = null;

            List <string> faces = new List <string>();

            string paginationToken = null;

            do
            {
                if (listFacesResponse != null)
                {
                    paginationToken = listFacesResponse.NextToken;
                }

                ListFacesRequest listFacesRequest = new ListFacesRequest()
                {
                    CollectionId = collectionId,
                    MaxResults   = 1,
                    NextToken    = paginationToken
                };

                listFacesResponse = rekognitionClient.ListFaces(listFacesRequest);
                foreach (Amazon.Rekognition.Model.Face face in listFacesResponse.Faces)
                {
                    faces.Add(face.FaceId);
                }
            }while (listFacesResponse != null && !String.IsNullOrEmpty(listFacesResponse.NextToken));

            return(faces);
        }
Example #3
0
        public void RemoveFacesFromCollection(string collectionId, List <string> removedFaces)
        {
            AmazonRekognitionClient rekognitionClient = AmazonClient.GetInstance();

            DeleteFacesRequest deleteFacesRequest = new DeleteFacesRequest()
            {
                CollectionId = collectionId,
                FaceIds      = removedFaces
            };

            DeleteFacesResponse deleteFacesResponse = rekognitionClient.DeleteFaces(deleteFacesRequest);
        }
Example #4
0
        public List <FaceRecord> Recognize(string collectionId, Amazon.Rekognition.Model.Image image)
        {
            //1- Detect faces in the input image and adds them to the specified collection.
            AmazonRekognitionClient rekognitionClient = AmazonClient.GetInstance();

            IndexFacesRequest indexFacesRequest = new IndexFacesRequest()
            {
                Image               = image,
                CollectionId        = collectionId,
                DetectionAttributes = new List <String>()
                {
                    "DEFAULT"
                }
            };

            IndexFacesResponse indexFacesResponse = rekognitionClient.IndexFaces(indexFacesRequest);

            //2- Search all detected faces in the collection
            SearchFacesResponse searchFacesResponse = null;

            List <FaceRecord> matchedFaces = new List <FaceRecord>();

            if (null != indexFacesResponse && null != indexFacesResponse.FaceRecords && 0 != indexFacesResponse.FaceRecords.Count)
            {
                foreach (FaceRecord face in indexFacesResponse.FaceRecords)
                {
                    searchFacesResponse = rekognitionClient.SearchFaces(new SearchFacesRequest
                    {
                        CollectionId       = collectionId,
                        FaceId             = face.Face.FaceId,
                        FaceMatchThreshold = 70F,
                        MaxFaces           = 2
                    });

                    if (searchFacesResponse.FaceMatches != null && searchFacesResponse.FaceMatches.Count != 0)
                    {
                        matchedFaces.Add(face);
                    }
                }

                //Remove newly added faces to the collection

                _collectionService.RemoveFacesFromCollection(collectionId, indexFacesResponse.FaceRecords.Select(x => x.Face.FaceId).ToList());
            }

            return(matchedFaces);
        }
Example #5
0
        public bool AddCollection(string collectionId)
        {
            try
            {
                AmazonRekognitionClient rekognitionClient = AmazonClient.GetInstance();

                CreateCollectionRequest createCollectionRequest = new CreateCollectionRequest()
                {
                    CollectionId = collectionId
                };

                CreateCollectionResponse createCollectionResponse = rekognitionClient.CreateCollection(createCollectionRequest);
                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }