public async Task <CustomFaceModel[]> GetIdentifyResultAsync(Stream picture)
        {
            CustomFaceModel[] customFaceModels = null;
            try
            {
                // await WaitIfOverCallLimitAsync();
                Face[] detectResults = await _serviceClient.DetectAsync(picture);

                Guid[]           guids           = detectResults.Select(x => x.FaceId).ToArray();
                IdentifyResult[] identifyResults = await _serviceClient.IdentifyAsync(_groupId, guids);

                customFaceModels = new CustomFaceModel[detectResults.Length];
                for (int i = 0; i < identifyResults.Length; i++)
                {
                    FaceRectangle rectangle = detectResults[i].FaceRectangle;

                    // Set initial name to Unknown.
                    string name = "Unknown";
                    try
                    {
                        name = (await _serviceClient.GetPersonAsync(_groupId, identifyResults[i].Candidates[0].PersonId)).Name;
                    }
                    catch (Exception)
                    {
                        // Just catch person not found.
                        // It will return a name "Unknown" for this one.
                    }

                    CustomFaceModel model = new CustomFaceModel()
                    {
                        Name   = name,
                        Top    = rectangle.Top,
                        Left   = rectangle.Left,
                        Width  = rectangle.Width,
                        Height = rectangle.Height
                    };

                    customFaceModels[i] = model;
                }
                ;
            }
            catch (Exception)
            {
                // Just catch it.
            }

            return(customFaceModels);
        }
        public async Task <string> IdentifyPerson(string groupId, string imageFile)
        {
            string strPerson = "No one identified";

            using (Stream s = File.OpenRead(imageFile))
            {
                var faces = await faceServiceClient.DetectAsync(s);

                var faceIds = faces.Select(face => face.FaceId).ToArray();

                var results = await faceServiceClient.IdentifyAsync(groupId, faceIds);

                foreach (var identifyResult in results)
                {
                    //Console.WriteLine("Result of face: {0}", identifyResult.FaceId);
                    if (identifyResult.Candidates.Length == 0)
                    {
                        strPerson = "No one identified";
                    }
                    else
                    {
                        // Get top 1 among all candidates returned
                        var candidateId = identifyResult.Candidates[0].PersonId;
                        var person      = await faceServiceClient.GetPersonAsync(groupId, candidateId);

                        strPerson = $"Identified as {person.Name}";
                    }
                }
            }
            return(strPerson);
        }
Beispiel #3
0
        // Returns true if face matches.
        public async Task <bool> AuthenticateFaceAsync(string imageUrl, Guid personId)
        {
            var faces = await faceServiceClient.DetectAsync(imageUrl);

            var faceIds = faces.Select(face => face.FaceId).ToArray();

            var result          = (await faceServiceClient.IdentifyAsync(PERSON_GROUP_ID, faceIds, 3));
            var identifyResults = result.OfType <Microsoft.ProjectOxford.Face.Contract.IdentifyResult>().ToList();

            foreach (var identifyResult in identifyResults)
            {
                if (identifyResult.Candidates.Length == 0)
                {
                    continue;
                }

                var candidateId = identifyResult.Candidates[0].PersonId;
                var person      = await faceServiceClient.GetPersonAsync(PERSON_GROUP_ID, candidateId);

                if (person.PersonId == personId)
                {
                    return(true);
                }
            }

            return(false);
        }
        async void btnIdentifyPhoto_Clicked(object sender, System.EventArgs e)
        {
            var    faceServiceClient = new FaceServiceClient(Keys.FaceAPIKey);
            string personGroupId     = "celebs";

            if (file != null)
            {
                var faces = await faceServiceClient.DetectAsync(file.GetStream());

                var faceIds = faces.Select(face => face.FaceId).ToArray();

                var results = await faceServiceClient.IdentifyAsync(personGroupId, faceIds);

                foreach (var identifyResult in results)
                {
                    //DisplayAlert("Results!",$"Result of face: {identifyResult.FaceId}","Yay!");
                    if (identifyResult.Candidates.Length == 0)
                    {
                        DisplayAlert("Bummer", "No one identified", "Ok");
                    }
                    else
                    {
                        // Get top 1 among all candidates returned
                        var candidateId = identifyResult.Candidates[0].PersonId;
                        var person      = await faceServiceClient.GetPersonAsync(personGroupId, candidateId);

                        DisplayAlert("Found!", $"Identified as {person.Name}", "Nice!");
                    }
                }
            }
            else
            {
                DisplayAlert("No Image", "Please pick or take a photo first", "Ok");
            }
        }
Beispiel #5
0
        private static async void IdentifyFace(string personGroupId, string imgPath)
        {
            using (Stream s = File.OpenRead(imgPath))
            {
                var faces = await faceServiceClient.DetectAsync(s);        //detect face range from picture

                var faceIds = faces.Select(face => face.FaceId).ToArray(); //Get all faceIds to array

                try
                {
                    var results = await faceServiceClient.IdentifyAsync(personGroupId, faceIds);

                    foreach (var identifyResult in results)
                    {
                        Console.WriteLine($"Result of face: {identifyResult.FaceId}");
                        if (identifyResult.Candidates.Length == 0)
                        {
                            Console.WriteLine("No one identified");
                        }
                        else
                        {
                            var candidateId = identifyResult.Candidates[0].PersonId;
                            var person      = await faceServiceClient.GetPersonAsync(personGroupId, candidateId);

                            Console.WriteLine($"Identified as {person.Name}");
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("[Error] " + ex.Message);
                }
            }
        }
Beispiel #6
0
        private static async Task <HttpResponseMessage> GetResponse(HttpRequestMessage req, bool isDebug, FaceServiceClient faceService, Guid personId, string personName = null, string echoId = null)
        {
            var     resp       = req.CreateResponse(HttpStatusCode.OK);
            dynamic respConent = new ExpandoObject();

            if (echoId != null)
            {
                respConent.PersonId = echoId;
            }
            else
            {
                respConent.PersonId = personId.ToString();
            }
            if (isDebug)
            {
                if (personName != null)
                {
                    respConent.PersonName = personName;
                }
                else
                {
                    var person = await faceService.GetPersonAsync(personGroupId, personId);

                    respConent.PersonName = person.Name;
                }
            }
            resp.Content = new StringContent(JsonConvert.SerializeObject(respConent), Encoding.UTF8, "application/json");

            return(resp);
        }
        public async Task <ObjectId> Identify(byte[] image)
        {
            ObjectId result = ObjectId.Empty;

            try
            {
                var face = await DetectFace(image);

                if (face == null)
                {
                    return(ObjectId.Empty);
                }


                var resp = await _client.IdentifyAsync("users_id", new Guid[] { face.FaceId });

                if (resp.Length == 1)
                {
                    var apiId = resp.First().Candidates.First().PersonId;
                    var dbId  = await _client.GetPersonAsync("users_id", apiId);

                    result = new ObjectId(dbId?.Name);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Identify: {ex.Message}");
            }
            return(result);
        }
Beispiel #8
0
        static async Task DetectAndVerifyEmployee(string personGroupId, string imgPath, Guid personId)
        {
            // This is the person we hope to find !
            var person = await _faceServiceClient.GetPersonAsync(personGroupId, personId);

            Console.WriteLine("...Trying to confirm that the photo submitted matches with {0}", person.Name);

            using (Stream s = File.OpenRead(imgPath))
            {
                var faces = await _faceServiceClient.DetectAsync(s);

                var faceIds = faces.Select(face => face.FaceId).ToArray();

                // Here, I only care about the first face in the list of faces detected assuming there is only one.
                var verifyResult = await _faceServiceClient.VerifyAsync(faceIds[0], personGroupId, personId);

                if (verifyResult.Confidence > 0.75)
                {
                    Console.WriteLine("We believe the picture correspond to: {0} with confidence: {1}\n", person.Name, verifyResult.Confidence);
                }
                else if (verifyResult.Confidence <= 0.75 && verifyResult.Confidence > 0.5)
                {
                    Console.WriteLine("Humm, not too sure it's {0}, confidence is low: {1}\n", person.Name, verifyResult.Confidence);
                }
                else
                {
                    Console.WriteLine("No way, you're trying to fool us, this is definitely not {0}. We got a confidence of: {1}\n", person.Name, verifyResult.Confidence);
                }
            }
        }
Beispiel #9
0
        public async void RecognitionFace(string personGroupId, string imagePath)
        {
            using (Stream s = File.OpenRead(imagePath))
            {
                var faces = await faceServiceClient.DetectAsync(s);

                var faceIds = faces.Select(face => face.FaceId).ToArray();

                try
                {
                    var results = await faceServiceClient.IdentifyAsync(personGroupId, faceIds);

                    foreach (var identifyResult in results)
                    {
                        Console.WriteLine($"Kết quả: {identifyResult.FaceId}");
                        if (identifyResult.Candidates.Length == 0)
                        {
                            Console.WriteLine("Không nhận diện");
                        }
                        else
                        {
                            var candidateId = identifyResult.Candidates[0].PersonId;
                            var person      = await faceServiceClient.GetPersonAsync(personGroupId, candidateId);

                            Console.WriteLine($"Xác định là: {person.Name}");
                        }
                    }
                }catch (Exception e)
                {
                    Console.WriteLine("Lỗi nhận dạng khuôn mặt: " + e.Message);
                }
            }
        }
        private async void identifyFace(String testImageFile)
        {
            using (Stream s = File.OpenRead(testImageFile))
            {
                try
                {
                    var faces = await faceServiceClient.DetectAsync(s);

                    var faceIds = faces.Select(face => face.FaceId).ToArray();

                    var results = await faceServiceClient.IdentifyAsync("myfriends", faceIds);

                    foreach (var identifyResult in results)
                    {
                        Console.WriteLine("Result of face: {0}", identifyResult.FaceId);
                        if (identifyResult.Candidates.Length == 0)
                        {
                            Console.WriteLine("No one identified");
                        }
                        else
                        {
                            // Get top 1 among all candidates returned
                            var candidateId = identifyResult.Candidates[0].PersonId;
                            var person      = await faceServiceClient.GetPersonAsync("myfriends", candidateId);

                            Console.WriteLine("Identified as {0}", person.Name);
                        }
                    }
                } catch (FaceAPIException e)
                {
                    Console.WriteLine("Error with identifying. " + e.Message + " " + e.ErrorCode);
                }
            }
        }
Beispiel #11
0
        public async Task <ActionResult> Identify([FromBody] string imageFile, string groupId)
        {
            var client = new FaceServiceClient(_faceApiKey, _faceApiEndpoint);

            Stream image = Images.DecodeBase64Image(imageFile);

            Face[] detectResult = await client.DetectAsync(image);

            if (detectResult.Length > 0)
            {
                Face             firstFace   = detectResult[0];
                IdentifyResult[] identResult = await client.IdentifyAsync(groupId, new Guid[] { firstFace.FaceId });

                Guid?topResultId = identResult.FirstOrDefault().Candidates.FirstOrDefault()?.PersonId;
                if (topResultId != null)
                {
                    var person = await client.GetPersonAsync(groupId, topResultId.Value);

                    return(Ok(person.Name));
                }
                else
                {
                    return(Ok("Unknown person..."));
                }
            }
            else
            {
                return(NotFound("No faces found on the image."));
            }
        }
        public async Task <string> VerifyFaceAgainstTraindedGroup(string personGroupId, Stream stream)
        {
            var faces = await _faceServiceClient.DetectAsync(stream);

            var faceIds = faces.Select(face => face.FaceId).ToArray();

            var results = await _faceServiceClient.IdentifyAsync(personGroupId, faceIds);

            foreach (var identifyResult in results)
            {
                if (identifyResult.Candidates.Length == 0)
                {
                    return("No one identified");
                }
                else
                {
                    // Get top 1 among all candidates returned
                    var candidateId = identifyResult.Candidates[0].PersonId;
                    var person      = await _faceServiceClient.GetPersonAsync(personGroupId, candidateId);

                    return("Identified as: " + person.Name + " with face ID: " + identifyResult.FaceId);
                }
            }
            return("No one identified");
        }
        public override async Task <RecognizeFaceResult> ExecuteAsync(RecognizeFaceContext request)
        {
            var retResult = new RecognizeFaceResult();

            try
            {
                var    keyProvider = DependencyService.Get <IApiKeyProvider>();
                string faceKey     = keyProvider.GetApiKey(ApiKeyType.FaceApi);

                var faceServiceClient = new FaceServiceClient(faceKey);
                using (MemoryStream ms = new MemoryStream(request.FaceImage))
                {
                    // detect faces in image
                    var faces = await faceServiceClient.DetectAsync(ms);

                    var faceIds = faces.Select(face => face.FaceId).ToArray();

                    // Identify the person in the photo, based on the face.
                    try
                    {
                        var results = await faceServiceClient.IdentifyAsync(request.GroupId, faceIds);

                        if (results.Any() && results[0].Candidates.Any())
                        {
                            var result = results[0].Candidates[0].PersonId;

                            // Fetch the person from the PersonId and display their name.
                            retResult.Person = await faceServiceClient.GetPersonAsync(request.GroupId, result);

                            if (retResult.Person != null)
                            {
                                retResult.Confidence = results[0].Candidates[0].Confidence;
                            }
                        }
                        else
                        {
                            retResult.Notification.Add("No match found");
                        }
                    }
                    catch (FaceAPIException fex)
                    {
                        if (fex.ErrorCode == FaceApiErrorCode.PersonGroupNotFound)
                        {
                            retResult.Notification.Add("No people have been registered");
                        }
                        else
                        {
                            retResult.Notification.Add("Recognize Face Failed! " + fex.ErrorMessage);
                        }

                        return(retResult);
                    }
                }
            }
            catch (Exception ex)
            {
                retResult.Notification.Add("Recognize Face Failed! " + ex.Message);
            }
            return(retResult);
        }
Beispiel #14
0
        public async void ReconocerCara(string grupoPersonaId, string path)
        {
            using (Stream s = File.OpenRead(path))
            {
                var cara = await caraService.DetectAsync(s);

                var caraId = cara.Select(c => c.FaceId).ToArray();
                try
                {
                    var resultado = await caraService.IdentifyAsync(grupoPersonaId, caraId);

                    foreach (var identificar in resultado)
                    {
                        Console.WriteLine(identificar.FaceId);
                        if (identificar.Candidates.Length == 0)
                        {
                            Console.WriteLine("NO HAY RESULTADOS");
                        }
                        else
                        {
                            var idCandidato = identificar.Candidates[0].PersonId;
                            var persona     = await caraService.GetPersonAsync(grupoPersonaId, idCandidato);

                            Console.WriteLine(persona.Name);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("ERROR IDENTIFICAR PERSONA\n" + ex.Message);
                }
            }
        }
Beispiel #15
0
        public static async Task <string> GetViewerName(StorageFile photoFile)
        {
            // generic placeholder for viewer's name
            string personName = "friend";

            using (FileStream photoStream = new FileStream(photoFile.Path, FileMode.Open))
            {
                // ask Face API to detect faces in photo
                Face[] faces = await faceServiceClient.DetectAsync(photoStream);

                if (faces.Length > 0)
                {
                    // convert result to array of id's to identify
                    Guid[] faceIds = faces.Select(face => face.FaceId).ToArray();

                    // ask face API to identify face from a trained person group and the id's we got back
                    IdentifyResult[] results = await faceServiceClient.IdentifyAsync(AzureCredentials.personGroup, faceIds);

                    // go through the returned identification results from the Face API
                    foreach (IdentifyResult identifyResult in results)
                    {
                        // if we have a prediction for who they are
                        if (identifyResult.Candidates.Length > 0)
                        {
                            Guid candidateId = identifyResult.Candidates[0].PersonId;
                            // extract the candidate's name using their ID
                            Person person = await faceServiceClient.GetPersonAsync(AzureCredentials.personGroup, candidateId);

                            personName = person.Name;
                        }
                    }
                }
                return(personName);
            }
        }
        /// <summary>
        /// Identifica los rostros de las personas del grupo en la foto.
        /// </summary>
        /// <returns>The face async.</returns>
        /// <param name="file">File.</param>
        public async Task <List <string> > IdentifyFaceAsync(string file)
        {
            var result = new List <string>();
            var faces  = await FaceServiceClient.DetectAsync(file);

            var faceIds = faces.Select(face => face.FaceId).ToArray();

            var results = await FaceServiceClient.IdentifyAsync(PersonGroupId, faceIds);

            foreach (var identifyResult in results)
            {
                if (identifyResult.Candidates.Length == 0)
                {
                    result.Add("Unknown");
                }
                else
                {
                    // Obtenemos el primer candidato de la lista retornada
                    var candidateId = identifyResult.Candidates[0].PersonId;
                    var person      = await FaceServiceClient.GetPersonAsync(PersonGroupId, candidateId);

                    result.Add(person.Name);
                }
            }
            return(result);
        }
Beispiel #17
0
        private async Task <string> IdentifyFacesAsync(Uri uri, string personGroupId)
        {
            _waitCall.AddCallTimeToQueue();
            var faces = await _faceServiceClient.DetectAsync(uri.ToString());

            var faceIds = faces.Select(face => face.FaceId).ToArray();

            _waitCall.AddCallTimeToQueue();
            var results = await _faceServiceClient.IdentifyAsync(personGroupId, faceIds);

            foreach (var identifyResult in results)
            {
                if (identifyResult.Candidates.Length == 0)
                {
                    return(ApplicationConstants.NoOneIdentified);
                }
                else
                {
                    var candidateId = identifyResult.Candidates[0].PersonId;
                    _waitCall.AddCallTimeToQueue();
                    var person = await _faceServiceClient.GetPersonAsync(personGroupId, candidateId);

                    return($"{person.Name}");
                }
            }
            return(string.Empty);
        }
        public static async Task <string> ExecuteFindSimilarFaceCommandAsync(string personGroupId, MediaFile photo)
        {
            var stream = photo.GetStream();


            var faceServiceClient = new FaceServiceClient(Constants.FaceApiKey, Constants.FaceEndpoint);
            // Step 4a - Detect the faces in this photo.
            var faces = await faceServiceClient.DetectAsync(stream);

            var faceIds = faces.Select(face => face.FaceId).ToArray();

            if (faceIds.Length == 0)
            {
                return(null);
            }

            // Step 4b - Identify the person in the photo, based on the face.
            var results = await faceServiceClient.IdentifyAsync(personGroupId, faceIds);

            var result = results[0].Candidates[0].PersonId;

            // Step 4c - Fetch the person from the PersonId and display their name.
            var person = await faceServiceClient.GetPersonAsync(personGroupId, result);

            return(person.Name);
        }
Beispiel #19
0
        public static async void Recognize()
        {
            string testImageFile = @"C:\projects\FaceIdentification\FaceIdentification\faces\norbert\me_cv.JPG";

            using (Stream s = File.OpenRead(testImageFile))
            {
                var faces = await faceServiceClient.DetectAsync(s);

                var faceIds = faces.Select(face => face.FaceId).ToArray();

                var results = await faceServiceClient.IdentifyAsync(personGroupId, faceIds);

                foreach (var identifyResult in results)
                {
                    Console.WriteLine("Result of face: {0}", identifyResult.FaceId);
                    if (identifyResult.Candidates.Length == 0)
                    {
                        Console.WriteLine("No one identified");
                    }
                    else
                    {
                        // Get top 1 among all candidates returned
                        var candidateId = identifyResult.Candidates[0].PersonId;
                        var person      = await faceServiceClient.GetPersonAsync(personGroupId, candidateId);

                        Console.WriteLine("Identified as {0}", person.Name);
                    }
                }
            }
        }
Beispiel #20
0
        public async Task <Person> Identify(FaceServiceClient _faceClient, Face[] faces)
        {
            var faceIds = faces.Select(face => face.FaceId).ToArray();

            Properties.Settings.Default.FaceAPICallCount++;
            if (faceIds.Length > 0)
            {
                foreach (var identifyResult in await _faceClient.IdentifyAsync(_groupId, faceIds))
                {
                    if (identifyResult.Candidates.Length != 0)
                    {
                        var candidateId = identifyResult.Candidates[0].PersonId;
                        var person      = await _faceClient.GetPersonAsync(_groupId, candidateId);

                        Properties.Settings.Default.FaceAPICallCount++;
                        return(person);
                        // user identificated: person.name is the associated name
                    }
                    else
                    {
                        // user not recognized
                    }
                }
            }
            return(null);
        }
Beispiel #21
0
        private static async void IdentifyFace(string personGroupID, string imgPath)
        {
            using (Stream s = File.OpenRead(imgPath))
            {
                var faces = await faceServiceClient.DetectAsync(s);

                var faceIds = faces.Select(face => face.FaceId).ToArray();
                try
                {
                    var results = await faceServiceClient.IdentifyAsync(personGroupID, faceIds);

                    foreach (var identifyResult in results)
                    {
                        Console.WriteLine($"Result of face: {identifyResult.FaceId}");
                        if (identifyResult.Candidates.Length == 0)
                        {
                            Console.WriteLine("No person identified");
                        }
                        else
                        {
                            var candidatedID = identifyResult.Candidates[0].PersonId;
                            var person       = await faceServiceClient.GetPersonAsync(personGroupID, candidatedID);

                            Console.WriteLine($"Identified as {person.Name}");
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
        public static async Task <(Face[] faces, Person person, Emotion[] emotions)> DetectAndIdentifyFace(Bitmap image)
        {
            FaceServiceClient    fsc = new FaceServiceClient(Settings.Instance.FaceApiKey, FaceApiEndpoint);
            EmotionServiceClient esc = new EmotionServiceClient(Settings.Instance.EmotionApiKey);

            //FACE Detection
            //TODO add detection interval as param
            Emotion[] emotions = null;
            Person    person   = null;

            Face[] faces = null;


            //Detect and identify only once per 10 seconds
            if (lastFaceDetectTime.AddSeconds(10) < DateTime.Now)
            {
                lastFaceDetectTime = DateTime.Now;

                MemoryStream memoryStream = new MemoryStream();
                image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);

                //We need to seek to begin
                memoryStream.Seek(0, SeekOrigin.Begin);
                faces = await fsc.DetectAsync(memoryStream, true, true, new List <FaceAttributeType>() { FaceAttributeType.Age, FaceAttributeType.Gender });

                if (faces.Any())
                {
                    var rec = new Microsoft.ProjectOxford.Common.Rectangle[] { faces.First().FaceRectangle.ToRectangle() };
                    //Emotions

                    //We need to seek to begin, due to problems with parallel access we needed to create new memory stream
                    memoryStream = new MemoryStream();
                    image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    memoryStream.Seek(0, SeekOrigin.Begin);

                    //We call Emotion API and we include face rectangle information,
                    //as this way the call is cheaper, as emotion api does not have to run face detection
                    emotions = await esc.RecognizeAsync(memoryStream, rec);


                    //Person Identification
                    var groups = await fsc.ListPersonGroupsAsync();

                    var groupId = groups.First().PersonGroupId;

                    //We are interested only in first candidate
                    var identifyResult = await fsc.IdentifyAsync(groupId, new Guid[] { faces.First().FaceId }, 1);

                    var candidate = identifyResult?.FirstOrDefault()?.Candidates?.FirstOrDefault();

                    if (candidate != null)
                    {
                        person = await fsc.GetPersonAsync(groupId, candidate.PersonId);
                    }
                }
            }
            return(faces, person, emotions);
        }
Beispiel #23
0
        async Task ExecuteFindSimilarFaceCommandAsync()
        {
            if (IsBusy)
            {
                return;
            }

            IsBusy = true;

            try
            {
                MediaFile photo;

                await CrossMedia.Current.Initialize();

                // Take photo
                if (CrossMedia.Current.IsCameraAvailable)
                {
                    photo = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
                    {
                        Directory = "Employee Directory",
                        Name      = "photo.jpg"
                    });
                }
                else
                {
                    photo = await CrossMedia.Current.PickPhotoAsync();
                }

                // Upload to cognitive services
                using (var stream = photo.GetStream())
                {
                    var faceServiceClient = new FaceServiceClient("22e49721a20e457880a32138afc9e027");

                    // Step 4 - Upload our photo and see who it is!
                    var faces = await faceServiceClient.DetectAsync(stream);

                    var faceIds = faces.Select(face => face.FaceId).ToArray();

                    var results = await faceServiceClient.IdentifyAsync(personGroupId, faceIds);

                    var result = results[0].Candidates[0].PersonId;

                    var person = await faceServiceClient.GetPersonAsync(personGroupId, result);

                    UserDialogs.Instance.Loading($"Person identified is {person.Name}.");
                }
            }
            catch (Exception ex)
            {
                UserDialogs.Instance.Loading(ex.Message);
            }
            finally
            {
                IsBusy = false;
            }
        }
        /// <summary>
        /// Identify the person by using Cognitive Face API
        /// </summary>
        private async void Identify()
        {
            while (true)
            {
                Message = "Seeing you...";
                using (var stream = await DetectFaceAsync())
                {
                    try
                    {
                        var faces = await faceClient.DetectAsync(ImageConverter.ConvertImage(stream));

                        if (!faces.Any())
                        {
                            continue;
                        }

                        Person person;

                        if (newModel)
                        {
                            person = await RegisterAsync(stream);
                        }
                        else
                        {
                            var identifyResults = await faceClient.IdentifyAsync(Settings.PersonGroupId, faces.Select(x => x.FaceId).ToArray());

                            if (identifyResults.FirstOrDefault()?.Candidates?.Count() > 0)
                            {
                                person = await faceClient.GetPersonAsync(Settings.PersonGroupId, identifyResults.First().Candidates.First().PersonId);
                            }
                            else
                            {
                                person = await RegisterAsync(stream);
                            }
                        }
                        Message = $"Hi {person.Name}!";
                        await Task.Delay(2000);
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
        }
        private static async Task <IEnumerable <Guid> > IdentifyAsync(Stream inputImageStream, Stream outputImage, string name)
        {
            var identifiedFaces   = new List <Guid>();
            var faceServiceClient = new FaceServiceClient(AppSettings.FaceApi.Key, AppSettings.FaceApi.Uri);
            var personGroupId     = AppSettings.FaceApi.PersonGroupId;
            var image             = Image.FromStream(inputImageStream);
            var pen = new Pen(Color.Red, 2);

            inputImageStream.Seek(0, SeekOrigin.Begin);

            var faces = await faceServiceClient.DetectAsync(inputImageStream);

            var faceIds = faces.Select(face => face.FaceId).ToArray();

            if (!faceIds.Any())
            {
                _log.Info($"[{_context.InvocationId.ToString()}] No faces detected");

                return(null);
            }

            var results = await faceServiceClient.IdentifyAsync(personGroupId, faceIds);

            foreach (var result in results)
            {
                if (result.Candidates.Length == 0)
                {
                    continue;
                }

                var candidateId = result.Candidates[0].PersonId;
                var person      = await faceServiceClient.GetPersonAsync(personGroupId, candidateId);

                var face = faces.Where(f => f.FaceId == result.FaceId).FirstOrDefault().FaceRectangle;

                using (Graphics graphics = Graphics.FromImage(image))
                {
                    graphics.DrawRectangle(pen, new Rectangle(face.Left, face.Top, face.Width, face.Height));
                }

                identifiedFaces.Add(result.FaceId);
                _log.Info($"[{_context.InvocationId.ToString()}] FaceId: {result.FaceId} => Identified as: {person.Name}");
            }

            using (var stream = new MemoryStream())
            {
                image.Save(stream, ImageFormat.Png);

                var byteArray = stream.ToArray();

                await outputImage.WriteAsync(byteArray, 0, byteArray.Length);
            }

            return(identifiedFaces);
        }
 public IEnumerable <Guid> GetPersonFaces(Person person)
 {
     try
     {
         return(Task.Run(() => Api.GetPersonAsync(person.GroupName.ToLowerInvariant(), person.Id.Value)).GetAwaiter().GetResult().PersistedFaceIds);
     }
     catch (FaceAPIException ex)
     {
         throw new FaceApiException(ex.ErrorMessage);
     }
 }
Beispiel #27
0
        static async Task TestImageData(string pictureToTest)
        {
            try
            {
                var idx         = pictureToTest.LastIndexOf('\\');
                var picFileName = pictureToTest.Substring(idx + 1);
                Console.WriteLine($"Identifling persons in provided picture <{picFileName}>....");
                using (Stream s = File.OpenRead(pictureToTest))
                {
                    //#5. API to detect a customized face image.
                    var faces = await faceServiceClient.DetectAsync(s);

                    if (!faces.Any())
                    {
                        Console.WriteLine($"Did not detect any faces in picture {picFileName}");
                    }
                    else
                    {
                        var faceIds = faces.Select(face => face.FaceId).ToArray();

                        //#6. Identify the result for the customized image.
                        var results = await faceServiceClient.IdentifyAsync(personGroupId, faceIds);

                        foreach (var identifyResult in results)
                        {
                            if (identifyResult.Candidates.Length == 0)
                            {
                                Console.WriteLine($"FaceId {identifyResult.FaceId} is identified as 'Unknown' in picture {picFileName}.");
                            }
                            else
                            {
                                // Get top 1 among all candidates returned
                                var candidateId = identifyResult.Candidates[0].PersonId;
                                var person      = await faceServiceClient.GetPersonAsync(personGroupId, candidateId);

                                if (person != null && !string.IsNullOrWhiteSpace(person.Name))
                                {
                                    Console.WriteLine($"FaceId {identifyResult.FaceId} is identified as {person.Name} in picture <{picFileName}>");
                                }
                            }
                        }
                    }
                }
            }
            catch (FaceAPIException faex)
            {
                throw faex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #28
0
        public async Task <List <IdentifyResults> > Identify(string imgUrl)
        {
            var result = new List <IdentifyResults>();

            var faces = await ServiceClient.DetectAsync(imgUrl);

            var facesId = faces.Select(face => face.FaceId).ToArray();

            int iterCount = faces.Count() / FacesPerOnce;

            iterCount += (faces.Count() % FacesPerOnce == 0) ? 0 : 1;

            var identifyResults = new List <IdentifyResult>();
            var buffer          = new Guid[FacesPerOnce];
            var residue         = faces.Count();

            for (int i = 0; i < iterCount; i++)
            {
                var count = (residue < FacesPerOnce) ? residue : FacesPerOnce;
                Array.Copy(facesId, i * FacesPerOnce, buffer, 0, count);

                var identifyResultsArr = await ServiceClient.IdentifyAsync(GroupId, buffer.Take(count).ToArray());

                identifyResults.AddRange(identifyResultsArr);

                residue -= FacesPerOnce;
                Thread.Sleep(Timeout);
            }

            foreach (var identifyResult in identifyResults)
            {
                if (identifyResult.Candidates.Count() == 0)
                {
                    continue;
                }

                var resultItem = new IdentifyResults();
                foreach (var candidate in identifyResult.Candidates)
                {
                    var person = await ServiceClient.GetPersonAsync(GroupId,
                                                                    candidate.PersonId);

                    resultItem.AddCandidate(person.Name);

                    Thread.Sleep(Timeout);
                }

                result.Add(resultItem);
            }

            return(result);
        }
Beispiel #29
0
        private async void IdentifyFace_Click(object sender, RoutedEventArgs e)
        {
            using (Stream s = await photoFile.OpenStreamForReadAsync())
            {
                var faces = await faceServiceClient.DetectAsync(s);

                status.Text = "faces were detected...";
                var faceIds = faces.Select(face => face.FaceId).ToArray();
                status.Text = "the number of faceids found is: " + faceIds.Length;

                StringBuilder resultText = new StringBuilder();
                try
                {
                    var results = await faceServiceClient.IdentifyAsync(personGroupId, faceIds);

                    status.Text = " The results are reaedy";

                    if (results.Length > 0)
                    {
                        //resultText.Append($"{results.Length} face(s) detected: \t");
                        status.Text = $"{results.Length} face(s) detected: \t";
                    }

                    foreach (var identityResult in results)
                    {
                        if (identityResult.Candidates.Length != 0)
                        {
                            var candidateId = identityResult.Candidates[0].PersonId;
                            var person      = await faceServiceClient.GetPersonAsync(personGroupId, candidateId);

                            // resultText.Append($"Authorized User Detected: {person.Name}\t  Door will be unlocked. \t");
                            status.Text = $"Authorized User Detected: {person.Name}. \t Door will be unlocked. \t";
                            await Timer_Tick();

                            pinValue = GpioPinValue.Low;
                            pin.Write(pinValue);
                            pin.SetDriveMode(GpioPinDriveMode.Output);
                        }
                    }
                    if (resultText.ToString().Equals($"{results.Length} face(s) detected! \t"))
                    {
                        status.Text = "Cannot unlock door.";
                        // resultText.Append("No persons identified\t");
                    }
                    //status.Text = resultText.ToString();
                }
                catch (FaceAPIException ex)
                {
                    status.Text = "An error occurred of type:" + ex.ErrorCode; //ex.message
                }
            }
        }
Beispiel #30
0
        public async Task <JsonResult> GetPerson(string personId)
        {
            try
            {
                var result = await client.GetPersonAsync("testgroup", new Guid(personId));

                return(Json(new { error = "", result = result }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                return(Json(new { error = "Hmmm... Something unexpected happened. Please come back later." }, JsonRequestBehavior.AllowGet));
            }
        }
        public async Task<string[]> FaceUpload(string DeviceId)
        {
            Stream req = null;
            req = await Request.Content.ReadAsStreamAsync();
            byte[] bytes = null;
            MemoryStream ms = new MemoryStream();
            int count = 0;
            do
            {
                byte[] buf = new byte[1024];
                count = req.Read(buf, 0, 1024);
                ms.Write(buf, 0, count);
            } while (req.CanRead && count > 0);
            bytes = ms.ToArray();
            Stream stream = new MemoryStream(bytes);
            FaceServiceClient faceclient = new FaceServiceClient(ConfigurationManager.AppSettings["OxfordSubscriptionKeyPrimary"]);
            Face[] faceresult = null;
            try
            {
                faceresult = await faceclient.DetectAsync(stream, false, false, false, false);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            if (faceresult.Length == 0)
            {
                return new string[]{"Invalid"};
            }
            Guid[] FaceIdSet = new Guid[faceresult.Length];
            for (int i = 0; i < faceresult.Length; i ++)
            {
                FaceIdSet[i] = faceresult[i].FaceId;
            }
            IdentifyResult[] identityresultnew = await faceclient.IdentifyAsync(ConfigurationManager.AppSettings["MemberGroupId"], FaceIdSet, 1);
            string IdentifyResultName = null;
            string[] IdentifyResultJson = new String[identityresultnew.Length];
            int StrangerNum = 0;
            for (int j = 0; j < identityresultnew.Length; j++)
            {
                if (identityresultnew[j].Candidates.Length == 0)
                {
                    IdentifyResultJson[j] = "Stranger";
                    StrangerNum ++;
                }
                else
                {
                    string candidateid = identityresultnew[j].Candidates[0].PersonId.ToString();
                    Person candidate = await faceclient.GetPersonAsync(ConfigurationManager.AppSettings["MemberGroupId"], new Guid(candidateid));
                    IdentifyResultName += candidate.Name + "_";
                    IdentifyResultJson[j] = candidate.Name;
                }
            }
            DateTime temp = DateTime.Now;
            string ImageNameDate = temp.Year.ToString() + "Y" + temp.Month.ToString() + "M" + temp.Day.ToString() + "D" + temp.Hour.ToString() + "h" + temp.Minute.ToString() + "m" + temp.Second.ToString() + "s";
            string ImagePath = await StorageUpload("visitorcapture", ImageNameDate + "_" + IdentifyResultName + StrangerNum.ToString() + "Strangers", bytes);
            
            return IdentifyResultJson;

        }