Ejemplo n.º 1
0
        public async Task <IEnumerable <PhotoFace> > FindPeople(IRandomAccessStream stream)
        {
            Face[]           faces      = null;
            IdentifyResult[] results    = null;
            List <PhotoFace> photoFaces = new List <PhotoFace>();

            try
            {
                // find all faces
                faces = await _client.DetectAsync(stream.AsStream());

                // no faces found
                if (faces.Count() == 0)
                {
                    return(photoFaces);
                }

                if (await CheckIfGroupExistsAsync())
                {
                    results = await _client.IdentifyAsync(_groupId, faces.Select(f => f.FaceId).ToArray());
                }

                for (var i = 0; i < faces.Length; i++)
                {
                    var face = faces[i];

                    var photoFace = new PhotoFace()
                    {
                        Rect       = face.FaceRectangle,
                        Identified = false
                    };

                    if (results != null)
                    {
                        var result = results[i];
                        if (result.Candidates.Length > 0)
                        {
                            photoFace.PersonId   = result.Candidates[0].PersonId;
                            photoFace.Name       = _personList.Where(p => p.PersonId == result.Candidates[0].PersonId).FirstOrDefault()?.Name;
                            photoFace.Identified = true;
                        }
                    }

                    photoFaces.Add(photoFace);
                }
            }
            catch (FaceAPIException ex)
            {
            }

            return(photoFaces);
        }
Ejemplo n.º 2
0
        public async Task <bool> AddImageForPerson(PhotoFace photoFace, IRandomAccessStream stream)
        {
            if (photoFace == null || photoFace.Name == null)
            {
                return(false);
            }

            bool trained = false;

            // create group
            try
            {
                if (!await CheckIfGroupExistsAsync())
                {
                    await _client.CreatePersonGroupAsync(_groupId, _groupId);
                }

                if (photoFace.PersonId == null || _personList == null || !_personList.Any(p => p.PersonId == photoFace.PersonId))
                {
                    photoFace.PersonId = (await _client.CreatePersonAsync(_groupId, photoFace.Name)).PersonId;
                }

                await _client.AddPersonFaceAsync(_groupId, photoFace.PersonId, stream.AsStream(), targetFace : photoFace.Rect);

                await _client.TrainPersonGroupAsync(_groupId);

                while (!trained)
                {
                    await Task.Delay(1000);

                    var status = await _client.GetPersonGroupTrainingStatusAsync(_groupId);

                    switch (status.Status)
                    {
                    case Status.Succeeded:
                        trained = true;
                        break;

                    case Status.Failed:
                        return(false);
                    }
                }

                _personList = await _client.GetPersonsAsync(_groupId);

                return(true);
            }
            catch (FaceAPIException ex)
            {
                return(false);
            }
        }