Пример #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());
        }
Пример #2
0
        public static void AddFace(string _externalImageId, string _collectionId)
        {
            try
            {
                string photo        = _externalImageId;
                string collectionId = _collectionId;

                using (rekognitionClient = new AmazonRekognitionClient(collectionRegion))
                {
                    AddingFace();
                }

                void AddingFace()
                {
                    Image image = new Image()
                    {
                        S3Object = new S3Object()
                        {
                            Bucket = bucket,
                            Name   = photo
                        }
                    };

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

                    IndexFacesResponse indexFacesResponse = rekognitionClient.IndexFaces(indexFacesRequest);

                    Console.WriteLine(photo + " added");

                    foreach (FaceRecord faceRecord in indexFacesResponse.FaceRecords)
                    {
                        Console.WriteLine("Face detected: Faceid is " +
                                          faceRecord.Face.FaceId);
                    }
                }
            }
            catch (AmazonRekognitionException e)
            {
                Console.WriteLine("AmazonRekognitionException: " + e);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e);
            }
        }
Пример #3
0
        public ActionResult Index(ImageTrainModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            try
            {
                using (Stream inputStream = model.File.InputStream)
                {
                    MemoryStream memoryStream = inputStream as MemoryStream;
                    if (memoryStream == null)
                    {
                        memoryStream = new MemoryStream();
                        inputStream.CopyTo(memoryStream);
                    }

                    var collectionsList = client.ListCollections(new ListCollectionsRequest {
                        MaxResults = 10
                    });
                    //client.CreateCollection(new CreateCollectionRequest
                    //{
                    //    CollectionId = "TrainedImages"
                    //});

                    IndexFacesRequest indexFacesRequest = new IndexFacesRequest()
                    {
                        Image = new Image
                        {
                            Bytes = memoryStream
                        },
                        CollectionId        = "TrainedImages",
                        ExternalImageId     = model.Name,
                        DetectionAttributes = new List <String>()
                        {
                            "ALL"
                        }
                    };

                    IndexFacesResponse indexFacesResponse = client.IndexFaces(indexFacesRequest);
                    if (indexFacesResponse.HttpStatusCode == System.Net.HttpStatusCode.OK)
                    {
                        ViewBag.Message = "Image trained successfully";
                    }
                }
            }
            catch (Exception ex)
            {
                ViewBag.Message = ex.Message.ToString();
            }
            return(View());
        }
Пример #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);
        }
Пример #5
0
    public static void Example()
    {
        String collectionId = "MyCollection";
        String bucket       = "bucket";
        String photo        = "input.jpg";

        AmazonRekognitionClient rekognitionClient = new AmazonRekognitionClient();

        Image image = new Image()
        {
            S3Object = new S3Object()
            {
                Bucket = bucket,
                Name   = photo
            }
        };

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

        IndexFacesResponse indexFacesResponse = rekognitionClient.IndexFaces(indexFacesRequest);

        Console.WriteLine(photo + " added");
        foreach (FaceRecord faceRecord in indexFacesResponse.FaceRecords)
        {
            Console.WriteLine("Face detected: Faceid is " +
                              faceRecord.Face.FaceId);
        }
    }
Пример #6
0
        public void RekognitionIndexFaces()
        {
            #region to-add-a-face-to-a-collection-1482179542923

            var client   = new AmazonRekognitionClient();
            var response = client.IndexFaces(new IndexFacesRequest
            {
                CollectionId        = "myphotos",
                DetectionAttributes = new List <string> {
                },
                ExternalImageId     = "myphotoid",
                Image = new Image {
                    S3Object = new S3Object {
                        Bucket = "mybucket",
                        Name   = "myphoto"
                    }
                }
            });

            List <FaceRecord> faceRecords           = response.FaceRecords;
            string            orientationCorrection = response.OrientationCorrection;

            #endregion
        }
Пример #7
0
        public ActionResult IndexFaceUpload(Arquivo arquivo, string Nome)
        {
            try
            {
                string Semelhanca = string.Empty;
                string input      = string.Empty;

                HttpPostedFileBase arquivoPostado = null;
                foreach (string fileInputName in Request.Files)
                {
                    var nome = arquivo.NomeLocal;

                    arquivoPostado = Request.Files[fileInputName];


                    var target = new MemoryStream();
                    arquivoPostado.InputStream.CopyTo(target);
                    arquivo.Conteudo        = target.ToArray();
                    arquivo.UsuarioInclusao = CustomAuthorizationProvider.UsuarioAutenticado.Login;
                    arquivo.DataInclusao    = DateTime.Now;
                    arquivo.Extensao        = Path.GetExtension(arquivoPostado.FileName);
                    arquivo.NomeLocal       = arquivoPostado.FileName;

                    byte[] inputImageData   = arquivo.Conteudo;
                    var    inputImageStream = new MemoryStream(inputImageData);

                    var item = arquivo.Conteudo;

                    input = Nome.Trim().Replace(" ", "") + ".jpg";


                    var rekognitionClient = new AmazonRekognitionClient("AKIAIBLZ7KFAN6XG3NNA", "2nukFOTDN0zv/y2tzeCiLrAHM5TwbFgvEqqZA9zn", RegionEndpoint.USWest2);

                    Image image = new Image()
                    {
                        Bytes = inputImageStream
                    };

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

                    try
                    {
                        IndexFacesResponse indexFacesResponse = rekognitionClient.IndexFaces(indexFacesRequest);
                    }
                    catch (Exception)
                    {
                        throw new Exception("Imagem não Indexada!");
                    }
                }

                if (input != null)
                {
                    return(Json(new { sucesso = "O Empregado '" + input + "' foi analisado com êxito." }));
                }
                else
                {
                    return(Json(new { sucesso = "Empregado não encontrado!" }));
                }
            }
            catch (Exception ex)
            {
                return(Json(new { erro = ex.Message }));
            }
        }