Пример #1
0
        /// <summary>
        /// Detect face and return the face id of a image file
        /// </summary>
        /// <param name="imageFile">
        /// image file to detect face
        /// </param>
        /// <returns>face id</returns>
        private async Task <Guid[]> DetectFacesFromImage(StorageFile imageFile)
        {
            var stream = await imageFile.OpenStreamForReadAsync();

            var faces = await _faceApiClient.DetectAsync(stream);

            if (faces == null || faces.Length < 1)
            {
                throw new FaceRecognitionException(FaceRecognitionExceptionType.NoFaceDetected);
            }

            return(FaceApiUtils.FacesToFaceIds(faces));
        }
        public async Task <Dictionary <HSFace, HSPerson> > FaceRecognizeAsync(StorageFile imageFile)
        {
            var recogResult = new Dictionary <HSFace, HSPerson>();

            if (!FaceApiUtils.ValidateImageFile(imageFile))
            {
                throw new FaceRecognitionException(FaceRecognitionExceptionType.InvalidImage);
            }

            // detect all faces in the image
            var arFaces = await DetectFacesFromImage(imageFile);

            // try to identify all faces to person
            var identificationResults = await FaceServiceHelper.IdentifyAsync(WhitelistId, FaceApiUtils.FacesToFaceIds(arFaces));

            // add identified person name to result list
            foreach (var result in identificationResults)
            {
                if (result.Candidates.Length > 0)
                {
                    var person = _whitelist.GetPersonById(result.Candidates[0].PersonId);
                    Debug.WriteLine(String.Format("Detected person {0} face ID Confidence: {1}% image file: {2}",
                                                  person.Name, Math.Round(result.Candidates[0].Confidence * 100, 1), imageFile.Path));
                    recogResult.Add(new HSFace(result.FaceId, imageFile.Path), person);
                }
                else
                {
                    foreach (ClientContract.Face detectedFace in arFaces)
                    {
                        if (detectedFace.FaceId == result.FaceId)
                        {
                            recogResult.Add(new HSFace(result.FaceId, imageFile.Path, detectedFace.FaceAttributes), null);
                            Debug.WriteLine(String.Format("Unknown {0} age {1} face ID {2} image file: {3}", detectedFace.FaceAttributes.Gender,
                                                          detectedFace.FaceAttributes.Age, detectedFace.FaceId, imageFile.Path));
                        }
                    }
                }
            }

            return(recogResult);
        }