/// <summary>
        /// Get or create a PersonGroup
        /// </summary>
        /// <param name="groupId"></param>
        /// <param name="groupName"></param>
        /// <returns></returns>
        private static async Task <PersonGroup> createGroup(string groupId, string groupName)
        {
            Task <PersonGroup> result = null;

            //Trying to get group specified by groupId
            try
            {
                var tGet = Clnt.GetPersonGroupAsync(groupId);
                tGet.Wait();
                result = tGet;
            }
            catch (Exception)
            {
                //If the group does not exist, I create IT

                Task tCreate = Clnt.CreatePersonGroupAsync(groupId, groupName);
                tCreate.Wait();

                var tGet = Clnt.GetPersonGroupAsync(groupId);
                tGet.Wait();
                result = tGet;
            }

            return(result.Result);
        }
Beispiel #2
0
        private async Task CreatePersonGroupIfNotExist()
        {
            Microsoft.ProjectOxford.Face.Contract.PersonGroup personGroup;

            try
            {
                personGroup = await faceServiceClient.GetPersonGroupAsync(PERSON_GROUP_ID);
            }
            catch (FaceAPIException)
            {
                await faceServiceClient.CreatePersonGroupAsync(PERSON_GROUP_ID, PERSON_GROUP_NAME);

                personGroup = await faceServiceClient.GetPersonGroupAsync(PERSON_GROUP_ID);
            }
        }
        public async Task LoadDataAsync()
        {
            Message = "Loading Data..";

            // Check if face group alredy exists, otherwise create it.
            try
            {
                await faceClient.GetPersonGroupAsync(Settings.PersonGroupId);
            }
            catch (FaceAPIException ex)
            {
                if (ex.ErrorCode == "PersonGroupNotFound")
                {
                    await faceClient.CreatePersonGroupAsync(Settings.PersonGroupId, Settings.PersonGroupId);

                    newModel = true;
                }
                else
                {
                    throw;
                }
            }
            catch (Exception ex)
            {
            }

            await mediaCapture.StartPreviewAsync();

            Identify();
        }
Beispiel #4
0
        public async Task SubirRostroAGrupo(string IdGrupo, string filename)
        {
            bool ExisteGrupo;

            try
            {
                await faceServiceClient.GetPersonGroupAsync(IdGrupo);

                ExisteGrupo = true;
            }
            catch
            {
                ExisteGrupo = false;
            }

            if (!ExisteGrupo)
            {
                await faceServiceClient.CreatePersonGroupAsync(IdGrupo, IdGrupo);
            }

            var tag     = System.IO.Path.GetFileName(filename.Replace("$", ""));
            var persona = await faceServiceClient.CreatePersonAsync(IdGrupo, tag);

            using (var fiStream = File.OpenRead(filename))
            {
                var persistFace = await faceServiceClient.AddPersonFaceAsync(IdGrupo, persona.PersonId, fiStream, tag);
            }
            await faceServiceClient.TrainPersonGroupAsync(IdGrupo);
        }
        private async void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                speechSynthesizer.SpeakAsync("Hi Visitor, Welcome to the new world of NCR.");
                await Task.Delay(1000);

                speechSynthesizer.SpeakAsync("To verify your face. Please put your face clearly infront of the ATM.");

                SpeakerIdentificationServiceClient _serviceClient = new SpeakerIdentificationServiceClient(speakerAPISubscriptionKey);

                bool groupExists = false;

                var faceServiceClient = new FaceServiceClient(faceAPISubscriptionKey);
                // Test whether the group already exists
                try
                {
                    Title = String.Format("Request: Group {0} will be used to build a person database. Checking whether the group exists.", GroupName);
                    Console.WriteLine("Request: Group {0} will be used to build a person database. Checking whether the group exists.", GroupName);

                    await faceServiceClient.GetPersonGroupAsync(GroupName);

                    groupExists = true;
                    Title       = String.Format("Response: Group {0} exists.", GroupName);
                    Console.WriteLine("Response: Group {0} exists.", GroupName);
                }
                catch (FaceAPIException ex)
                {
                    if (ex.ErrorCode != "PersonGroupNotFound")
                    {
                        Title = String.Format("Response: {0}. {1}", ex.ErrorCode, ex.ErrorMessage);
                        return;
                    }
                    else
                    {
                        Title = String.Format("Response: Group {0} did not exist previously.", GroupName);
                    }
                }

                if (groupExists)
                {
                    Title = String.Format("Success..... Now your Group  {0} ready to use.", GroupName);
                    webcam.Start();
                    return;
                }

                else
                {
                    Console.WriteLine("Group did not exist. First you need to create a group");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error : ", ex.Message);
            }
        }
Beispiel #6
0
        /// <summary>
        /// Create a person group with ID and name provided if none can be found in the service.
        /// </summary>
        private async void CreatePersonGroupButton_ClickAsync(object sender, RoutedEventArgs e)
        {
            personGroupId   = PersonGroupIdTextBox.Text;
            personGroupName = PersonGroupNameTextBox.Text;
            PersonGroupStatusTextBlock.Foreground = new SolidColorBrush(Colors.Black);
            authKey = AuthKeyTextBox.Text;

            if (string.IsNullOrWhiteSpace(personGroupId) == false && string.IsNullOrWhiteSpace(personGroupName) == false && string.IsNullOrWhiteSpace(authKey) == false)
            {
                PersonGroupCreateErrorText.Visibility = Visibility.Collapsed;
                await ApiCallAllowed(true);

                faceServiceClient = new FaceServiceClient(authKey);

                if (null != faceServiceClient)
                {
                    // You may experience issues with this below call, if you are attempting connection with
                    // a service location other than 'West US'
                    PersonGroup[] groups = await faceServiceClient.ListPersonGroupsAsync();

                    var matchedGroups = groups.Where(p => p.PersonGroupId == personGroupId);

                    if (matchedGroups.Count() > 0)
                    {
                        knownGroup = matchedGroups.FirstOrDefault();

                        PersonGroupStatusTextBlock.Text = "Found existing: " + knownGroup.Name;
                    }

                    if (null == knownGroup)
                    {
                        await ApiCallAllowed(true);

                        await faceServiceClient.CreatePersonGroupAsync(personGroupId, personGroupName);

                        knownGroup = await faceServiceClient.GetPersonGroupAsync(personGroupId);

                        PersonGroupStatusTextBlock.Text = "Created new group: " + knownGroup.Name;
                    }

                    if (PersonGroupStatusTextBlock.Text != "- Person Group status -")
                    {
                        PersonGroupStatusTextBlock.Foreground = new SolidColorBrush(Colors.Green);
                    }
                    else
                    {
                        PersonGroupStatusTextBlock.Foreground = new SolidColorBrush(Colors.Red);
                    }
                }
            }
            else
            {
                PersonGroupCreateErrorText.Text       = "Make sure you provide: a Person Group ID, a Person Group Name, and the Authentication Key in the section above.";
                PersonGroupCreateErrorText.Visibility = Visibility.Visible;
            }
        }
Beispiel #7
0
 private async Task CreatePersonGroupIsNotExist(FaceServiceClient client, string personListId)
 {
     try
     {
         await client.GetPersonGroupAsync(personListId);
     }
     catch (FaceAPIException)
     {
         // not found
         await client.CreatePersonGroupAsync(personGroupId : personListId, name : personListId);
     }
 }
        public async Task LoadDataAsync()
        {
            Message = "Loading Data..";

            // Check if face group alredy exists, otherwise create it.
            try
            {
                await faceClient.GetPersonGroupAsync(Settings.PersonGroupId);
            }
            catch (FaceAPIException ex)
            {
                if (ex.ErrorCode == "PersonGroupNotFound")
                {
                    await faceClient.CreatePersonGroupAsync(Settings.PersonGroupId, Settings.PersonGroupId);
                }
                else
                {
                    throw;
                }
            }

            currentEvent = await meetupService.GetCurrentEventAsync();

            if (currentEvent == null)
            {
                Message = "No event scheduled.";
                return;
            }

            registeredPersons = (await faceClient.GetPersonsAsync(Settings.PersonGroupId)).ToList();
            RSVPs             = await meetupService.GetRSVPsAsync(currentEvent.Id);

            // Get comments start with 'Welcome' to track who comes to the event.
            RSVPComments = (await meetupService.GetCommentsAsync(currentEvent.Id)).Where(x => x.CommentDetail.StartsWith("Welcome ")).ToList();

            // Check if RSVPed meetup member is registered to Face API.
            foreach (RSVP rsvp in RSVPs)
            {
                var registeredPerson = registeredPersons.FirstOrDefault(x => x.Name == rsvp.Member.Name);
                if (registeredPerson == null)
                {
                    var userData = new JObject();
                    userData["memberId"] = rsvp.Member.Id;
                    var createdPersonResult = await faceClient.CreatePersonAsync(Settings.PersonGroupId, rsvp.Member.Name, userData.ToString());

                    registeredPersons.Add(await faceClient.GetPersonAsync(Settings.PersonGroupId, createdPersonResult.PersonId));
                }
            }

            await mediaCapture.StartPreviewAsync();

            Identify();
        }
        public async Task UpsertPersonAsync(StorageFile image, string personName)
        {
            Log?.WriteLine($"Upsert person: {personName}");

            // person group
            try
            {
                var personGroup = await _faceClient.GetPersonGroupAsync(_groupName);

                Log?.WriteLine($"Found person group: {personGroup.Name}");
            }
            catch (FaceAPIException ex)
            {
                if (ex.ErrorCode == "PersonGroupNotFound")
                {
                    Log?.WriteLine("Creating person group");
                    await _faceClient.CreatePersonGroupAsync(_groupName, _groupName);
                }
            }

            // person
            var people = await _faceClient.GetPersonsAsync(_groupName);

            var personId = people.FirstOrDefault(p => p.Name.ToLowerInvariant() == personName.ToLowerInvariant())?.PersonId;

            if (personId == null || personId == Guid.Empty)
            {
                personId = (await _faceClient.CreatePersonAsync(_groupName, personName)).PersonId;
            }

            // face
            await Task.Run(async() =>
            {
                using (var fileStream = File.OpenRead(image.Path))
                {
                    await _faceClient.AddPersonFaceAsync(_groupName, (Guid)personId, fileStream, image.Path);
                    await _faceClient.TrainPersonGroupAsync(_groupName);

                    while (true)
                    {
                        var trainingStatus = await _faceClient.GetPersonGroupTrainingStatusAsync(_groupName);
                        Log?.WriteLine($"Training Status: {trainingStatus.Status.ToString()}");
                        if (trainingStatus.Status != Microsoft.ProjectOxford.Face.Contract.Status.Running)
                        {
                            break;
                        }

                        await Task.Delay(1500);
                    }
                }
            });
        }
Beispiel #10
0
        public static async void AddPersonToGroup(string personGroupId, string personName, string imgPath)
        {
            try
            {
                await faceServiceClient.GetPersonGroupAsync(personGroupId);

                CreatePersonResult personResult = await faceServiceClient.CreatePersonAsync(personGroupId, personName);

                DetectFaceAndRegister(personGroupId, personResult, imgPath);
            }
            catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }
        }
Beispiel #11
0
 public async Task AddPersonToGroup(string personGroupId, string name, string pathImage)
 {
     try
     {
         await faceServiceClient.GetPersonGroupAsync(personGroupId).ContinueWith(async(x) =>
         {
             CreatePersonResult person = await faceServiceClient.CreatePersonAsync(personGroupId, name);
             await DetectFaceAndRegister(personGroupId, person, pathImage);
         });
     }
     catch (Exception ex)
     {
         Console.WriteLine("Error add Person to Group\n " + ex.Message);
     }
 }
Beispiel #12
0
        public async void AddPerson(string personId, string personName, string path)
        {
            try
            {
                await faceServiceClient.GetPersonGroupAsync(personId);

                CreatePersonResult person = await faceServiceClient.CreatePersonAsync(personId, "Eimantas");

                DetectFaceAndRegister(personId, person, path);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error add person " + ex.Message);
            }
        }
Beispiel #13
0
        public async void AnadirPersona(string grupoPersonaId, string nombre, string path)
        {
            try
            {
                await caraService.GetPersonGroupAsync(grupoPersonaId);

                CreatePersonResult persona = await caraService.CreatePersonAsync(grupoPersonaId, nombre);

                RegistrarCara(grupoPersonaId, persona, path);
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR ANADIR PERSONA\n" + ex.Message);
            }
        }
        public async Task <string> AddPersonToGroup(string personGroupId, string Name)
        {
            try
            {
                await faceServiceClient.GetPersonGroupAsync(personGroupId);

                CreatePersonResult person = await faceServiceClient.CreatePersonAsync(personGroupId, Name);

                //  await faceServiceClient.AddPersonFaceAsync(personGroupId, person.PersonId, s);
                return("" + person.PersonId);
            }
            catch (Exception ex)
            {
                return("Add person error");
            }
        }
Beispiel #15
0
        public async void AddPersonToGroup(string personGroupid, string name, string pathImage)
        {
            try
            {
                await faceServiceClient.GetPersonGroupAsync(personGroupid);

                CreatePersonResult person = await faceServiceClient.CreatePersonAsync(personGroupid, name);

                DetectFaceAndRegister(personGroupid, person, pathImage);
                Console.WriteLine("Thêm thành công");
            }
            catch (Exception e)
            {
                Console.WriteLine("Lỗi thêm thành viên vào nhóm\n" + e.Message);
            }
        }
Beispiel #16
0
        private async Task <bool> CheckIfGroupExistsAsync()
        {
            PersonGroup group = null;

            // create group if first time
            try
            {
                group = await _client.GetPersonGroupAsync(_groupId);

                _personList = await _client.GetPersonsAsync(_groupId);

                return(true);
            }
            catch (FaceAPIException ex)
            {
                return(false);
            }
        }
Beispiel #17
0
        public static async Task <string> AddPersonToGroup(string personGroupId, string personName)
        {
            try
            {
                await faceServiceClient.GetPersonGroupAsync(personGroupId);

                CreatePersonResult personResult = await faceServiceClient.CreatePersonAsync(personGroupId, personName);

                DetectFaceAndRegister(personGroupId, personResult, personName);
                Console.WriteLine("Finished");
                return("COMPLETE");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return("FAILURE");
            }
        }
        /// <summary>
        /// Create a person group with ID and name provided if none can be found in the service.
        /// </summary>
        private async void CreatePersonGroupButton_ClickAsync(object sender, RoutedEventArgs e)
        {
            personGroupId   = PersonGroupIdTextBox.Text;
            personGroupName = PersonGroupNameTextBox.Text;
            PersonGroupStatusTextBlock.Foreground = new SolidColorBrush(Colors.Black);
            authKey           = AuthKeyTextBox.Text;
            faceServiceClient = new FaceServiceClient(authKey);

            if (null != faceServiceClient)
            {
                // You may experience issues with this below call, if you are attempting connection with
                // a service location other than 'West US'
                PersonGroup[] groups = await faceServiceClient.ListPersonGroupsAsync();

                var matchedGroups = groups.Where(p => p.PersonGroupId == personGroupId);

                if (matchedGroups.Count() > 0)
                {
                    knownGroup = matchedGroups.FirstOrDefault();

                    PersonGroupStatusTextBlock.Text = "Found existing: " + knownGroup.Name;
                }

                if (null == knownGroup)
                {
                    await faceServiceClient.CreatePersonGroupAsync(personGroupId, personGroupName);

                    knownGroup = await faceServiceClient.GetPersonGroupAsync(personGroupId);

                    PersonGroupStatusTextBlock.Text = "Created new group: " + knownGroup.Name;
                }

                if (PersonGroupStatusTextBlock.Text != "- Person Group status -")
                {
                    PersonGroupStatusTextBlock.Foreground = new SolidColorBrush(Colors.Green);
                }
                else
                {
                    PersonGroupStatusTextBlock.Foreground = new SolidColorBrush(Colors.Red);
                }
            }
        }
        private async Task <bool> DoesPersonGroupExistAsync(string personGroupId)
        {
            bool result = false;

            try
            {
                PersonGroup personGroup = await _faceServiceClient.GetPersonGroupAsync(personGroupId);

                if (personGroup != null)
                {
                    result = true;
                }
            }
            catch (Exception)
            {
                result = false;
            }

            return(result);
        }
        /// <summary>
        /// Creates a training group.  
        /// </summary>
        /// <param name="PersonGroupID">Name of the person group.</param>
        /// <returns></returns>
        public async Task createFaceGroup(string PersonGroupID)
        {
            bool groupExists = false;
            IFaceServiceClient faceClient = new FaceServiceClient(SubscriptionKey);
            
            // Test whether the group already exists
            try
            {
                await faceClient.GetPersonGroupAsync(PersonGroupID);
                groupExists = true;
            }
            catch (ClientException ex)
            {
                if (ex.Error.Code != "PersonGroupNotFound")
                {
                    throw;
                }
                else
                {

                }
            }

            // check to see if group exists and if so delete the group.
            if (groupExists)
            {
                await faceClient.DeletePersonGroupAsync(PersonGroupID);
            }

            try
            {
                await faceClient.CreatePersonGroupAsync(PersonGroupID, PersonGroupID);
            }
            catch (ClientException ex)
            {
                throw;
            }



        }
        public async Task CheckGroupExistAsync()
        {
            if (_localSettings.Values["GroupId"] == null)
            {
                throw new NullReferenceException("Cannot find GroupId.");
            }

            try
            {
                _groupId = _localSettings.Values["GroupId"].ToString();
                var group = await _serviceClient.GetPersonGroupAsync(_groupId);
            }
            catch (FaceAPIException)
            {
                await _serviceClient.CreatePersonGroupAsync(_groupId, _groupId + "Name");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #22
0
        public static async Task CreateGroupIfNotExists()
        {
            bool groupExists = false;
            FaceServiceClient faceServiceClient = null;

            try
            {
                faceServiceClient = new FaceServiceClient(FaceKey);
                try
                {
                    await faceServiceClient.GetPersonGroupAsync(GroupName);

                    groupExists = true;
                }
                catch (FaceAPIException ex)
                {
                    if (ex.ErrorCode != "PersonGroupNotFound")
                    {
                        Trace.TraceError(ex.ErrorMessage);
                    }
                }

                if (!groupExists)
                {
                    try
                    {
                        await faceServiceClient.CreatePersonGroupAsync(GroupName, GroupName);
                    }
                    catch (FaceAPIException ex)
                    {
                        Trace.TraceError(ex.ErrorMessage);
                    }
                }
            }
            catch (FaceAPIException ex)
            {
                Trace.TraceError(ex.ErrorMessage);
            }
        }
Beispiel #23
0
        private static async Task Main(string[] args)
        {
            //use this and replace person id's to add new users and train them to our person group
            List <string> imgUrls = new List <string>()
            {
                "https://i.imgur.com/Npk2a0U.jpg"
            };
            //"https://i.imgur.com/p9vFrlW.jpg",
            //"https://i.imgur.com/JFBTaUW.jpg",
            //"https://i.imgur.com/ktCHy9S.jpg",
            //"https://i.imgur.com/nCU12mD.jpg" };

            IFaceServiceClient faceServiceClient = new FaceServiceClient("7a4c13f93842422392b8f472f127381b",
                                                                         "https://southcentralus.api.cognitive.microsoft.com/face/v1.0");

            try
            {
                var test = await faceServiceClient.GetPersonGroupAsync("5");

                Console.Write("Got PersonGroup " + test.Name);
                var person = await faceServiceClient.CreatePersonAsync("5", "Craig, Dave", "Craig, Dave");

                foreach (string img in imgUrls)
                {
                    await faceServiceClient.AddPersonFaceAsync("5", person.PersonId, img);
                }
                Console.WriteLine("Training Person Group");
                await faceServiceClient.TrainPersonGroupAsync("5");

                Console.WriteLine("Finished TrainingPerson Group - feel free to test now");
            }
            catch (ClientException ex)
            {
                Console.WriteLine(ex.Error.Code);
                Console.WriteLine(ex.Error.Message);
            }
            Console.ReadKey();
        }
Beispiel #24
0
        private bool CheckIfGroupExists()
        {
            // Test whether the group already exists
            bool groupExists = false;

            try
            {
                using (var faceServiceClient = new FaceServiceClient(this.strSubscriptionKey, this.strEndpoint))
                {
                    Task taskGetPerson = Task.Run(async() => await faceServiceClient.GetPersonGroupAsync(this.PersonGroupId));
                    while (true)
                    {
                        if (taskGetPerson.IsCompleted)
                        {
                            groupExists = true;
                            break;
                        }
                        else
                        {
                            taskGetPerson.Wait();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (((FaceAPIException)ex.InnerException).ErrorCode == "PersonGroupNotFound")
                {
                    groupExists = false;
                }
                else
                {
                    throw ex;
                }
            }
            return(groupExists);
        }
Beispiel #25
0
        /// <summary>
        /// Pick the root person database folder, to minimum the data preparation logic, the folder should be under following construction
        /// Each person's image should be put into one folder named as the person's name
        /// All person's image folder should be put directly under the root person database folder
        /// </summary>
        /// <param name="sender">Event sender</param>
        /// <param name="e">Event argument</param>
        public async void Load()
        {
            bool groupExists = false;

            // Test whether the group already exists
            try
            {
                await FaceServiceClient.GetPersonGroupAsync(GroupName);

                groupExists = true;
            }
            catch (FaceAPIException ex)
            {
                if (ex.ErrorCode != "PersonGroupNotFound")
                {
                    MainWindow.LoaderStatusLabel.Content = "Error";
                    return;
                }
            }

            // If group exists, warn user it will be replaced
            if (groupExists)
            {
                var cleanGroup = System.Windows.MessageBox.Show(string.Format("Requires a clean up for group \"{0}\" before setting up a new person database. Click OK to proceed, group \"{0}\" will be cleared.", GroupName), "Warning", MessageBoxButton.OKCancel);
                if (cleanGroup == MessageBoxResult.OK)
                {
                    MainWindow.LoaderStatusLabel.Content = "Removing Group";
                    await FaceServiceClient.DeletePersonGroupAsync(GroupName);
                }
                else
                {
                    return;
                }
            }

            // Show folder picker
            System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog();
            var result = dlg.ShowDialog();

            // Set the suggestion count is intent to minimum the data preparation step only,
            // it's not corresponding to service side constraint
            const int SuggestionCount = 15;

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                // User picked a root person database folder

                // Call create person group REST API
                // Create person group API call will failed if group with the same name already exists
                MainWindow.LoaderStatusLabel.Content = "Creating Group";
                try
                {
                    await FaceServiceClient.CreatePersonGroupAsync(GroupName, GroupName);

                    MainWindow.LoaderStatusLabel.Content = "Group Created";
                }
                catch (FaceAPIException)
                {
                    MainWindow.LoaderStatusLabel.Content = "Error";
                    return;
                }

                int  processCount  = 0;
                bool forceContinue = false;

                MainWindow.LoaderStatusLabel.Content = "Processing images";

                // Enumerate top level directories, each directory contains one person's images
                int invalidImageCount = 0;
                foreach (var dir in System.IO.Directory.EnumerateDirectories(dlg.SelectedPath))
                {
                    var tasks      = new List <Task>();
                    var tag        = System.IO.Path.GetFileName(dir);
                    var personName = tag;
                    var faces      = new ObservableCollection <Face>();

                    // Call create person REST API, the new create person id will be returned
                    var personId = (await FaceServiceClient.CreatePersonAsync(GroupName, personName)).PersonId.ToString();

                    string img;
                    // Enumerate images under the person folder, call detection
                    var imageList = new ConcurrentBag <string>(
                        Directory.EnumerateFiles(dir, "*.*", SearchOption.AllDirectories)
                        .Where(s => s.ToLower().EndsWith(".jpg") || s.ToLower().EndsWith(".png") || s.ToLower().EndsWith(".bmp") || s.ToLower().EndsWith(".gif")));

                    while (imageList.TryTake(out img))
                    {
                        tasks.Add(Task.Factory.StartNew(
                                      async(obj) =>
                        {
                            var imgPath = obj as string;

                            using (var fStream = File.OpenRead(imgPath))
                            {
                                try
                                {
                                    // Update person faces on server side
                                    var persistFace = await FaceServiceClient.AddPersonFaceAsync(GroupName, Guid.Parse(personId), fStream, imgPath);
                                    return(new Tuple <string, Microsoft.ProjectOxford.Face.Contract.AddPersistedFaceResult>(imgPath, persistFace));
                                }
                                catch (FaceAPIException ex)
                                {
                                    // if operation conflict, retry.
                                    if (ex.ErrorCode.Equals("ConcurrentOperationConflict"))
                                    {
                                        imageList.Add(imgPath);
                                        return(null);
                                    }
                                    // if operation cause rate limit exceed, retry.
                                    else if (ex.ErrorCode.Equals("RateLimitExceeded"))
                                    {
                                        imageList.Add(imgPath);
                                        return(null);
                                    }
                                    else if (ex.ErrorMessage.Contains("more than 1 face in the image."))
                                    {
                                        Interlocked.Increment(ref invalidImageCount);
                                    }
                                    // Here we simply ignore all detection failure in this sample
                                    // You may handle these exceptions by check the Error.Error.Code and Error.Message property for ClientException object
                                    return(new Tuple <string, Microsoft.ProjectOxford.Face.Contract.AddPersistedFaceResult>(imgPath, null));
                                }
                            }
                        },
                                      img).Unwrap());

                        if (processCount >= SuggestionCount && !forceContinue)
                        {
                            var continueProcess = System.Windows.Forms.MessageBox.Show("The images loaded have reached the recommended count, may take long time if proceed. Would you like to continue to load images?", "Warning", System.Windows.Forms.MessageBoxButtons.YesNo);
                            if (continueProcess == System.Windows.Forms.DialogResult.Yes)
                            {
                                forceContinue = true;
                            }
                            else
                            {
                                break;
                            }
                        }

                        if (tasks.Count >= _maxConcurrentProcesses || imageList.IsEmpty)
                        {
                            await Task.WhenAll(tasks);

                            tasks.Clear();
                        }
                    }
                }

                try
                {
                    // Start train person group
                    MainWindow.LoaderStatusLabel.Content = "Training Person Group";
                    await FaceServiceClient.TrainPersonGroupAsync(GroupName);

                    // Wait until train completed
                    while (true)
                    {
                        await Task.Delay(1000);

                        var status = await FaceServiceClient.GetPersonGroupTrainingStatusAsync(GroupName);

                        if (status.Status != Microsoft.ProjectOxford.Face.Contract.Status.Running)
                        {
                            break;
                        }
                    }
                }
                catch (FaceAPIException)
                {
                    MainWindow.LoaderStatusLabel.Content = "Error";
                }
            }
            MainWindow.LoaderStatusLabel.Content = "Done";
            GC.Collect();
        }
Beispiel #26
0
        // PUT: api/Uploadphoto/5
        public static async void Put(string facebookID, string fileDir)
        {
            bool groupExists       = false;
            var  faceServiceClient = new FaceServiceClient(subscriptionKey);

            try
            {
                //MainWindow.Log("Request: Group {0} will be used for build person database. Checking whether group exists.", GroupName);

                await faceServiceClient.GetPersonGroupAsync(GroupName);

                groupExists = true;
                //MainWindow.Log("Response: Group {0} exists.", GroupName);
            }
            catch (FaceAPIException ex)
            {
                if (ex.ErrorCode != "PersonGroupNotFound")
                {
                    //MainWindow.Log("Response: {0}. {1}", ex.ErrorCode, ex.ErrorMessage);
                    return;
                }
                else
                {
                    //MainWindow.Log("Response: Group {0} does not exist before.", GroupName);
                }
            }
            if (!groupExists)
            {
                //MainWindow.Log("Request: Creating group \"{0}\"", GroupName);
                try
                {
                    await faceServiceClient.CreatePersonGroupAsync(GroupName, GroupName);

                    //MainWindow.Log("Response: Success. Group \"{0}\" created", GroupName);
                }
                catch (FaceAPIException ex)
                {
                    //MainWindow.Log("Response: {0}. {1}", ex.ErrorCode, ex.ErrorMessage);
                    return;
                }
            }
            var    tag         = facebookID;
            var    faces       = new ObservableCollection <Face>();
            string personName  = tag;
            string personID    = (await faceServiceClient.CreatePersonAsync(GroupName, personName)).PersonId.ToString();
            var    fStream     = File.OpenRead(fileDir);
            var    persistFace = await faceServiceClient.AddPersonFaceAsync(GroupName, Guid.Parse(personID), fStream, fileDir);

            //return new Tuple<string, ClientContract.AddPersistedFaceResult>(value, persistFace);
            //await Task.WhenAll(tasks);
            try
            {
                // Start train person group
                //MainWindow.Log("Request: Training group \"{0}\"", GroupName);
                await faceServiceClient.TrainPersonGroupAsync(GroupName);

                // Wait until train completed
                while (true)
                {
                    await Task.Delay(1000);

                    var status = await faceServiceClient.GetPersonGroupTrainingStatusAsync(GroupName);

                    Console.WriteLine("Response: {0}. Group \"{1}\" training process is {2}", "Success", GroupName, status.Status);
                    if (status.Status != (Status)2)
                    {
                        break;
                    }
                }
            }
            catch (FaceAPIException ex)
            {
                //MainWindow.Log("Response: {0}. {1}", ex.ErrorCode, ex.ErrorMessage);
            }
        }
        /// <summary>
        /// Pick the root person database folder, to minimum the data preparation logic, the folder should be under following construction
        /// Each person's image should be put into one folder named as the person's name
        /// All person's image folder should be put directly under the root person database folder
        /// </summary>
        /// <param name="sender">Event sender</param>
        /// <param name="e">Event argument</param>
        private async void FolderPicker_Click(object sender, RoutedEventArgs e)
        {
            bool groupExists = false;

            MainWindow mainWindow = Window.GetWindow(this) as MainWindow;
            string subscriptionKey = mainWindow._scenariosControl.SubscriptionKey;

            var faceServiceClient = new FaceServiceClient(subscriptionKey);

            // Test whether the group already exists
            try
            {
                MainWindow.Log("Request: Group {0} will be used for build person database. Checking whether group exists.", GroupName);

                await faceServiceClient.GetPersonGroupAsync(GroupName);
                groupExists = true;
                MainWindow.Log("Response: Group {0} exists.", GroupName);
            }
            catch (FaceAPIException ex)
            {
                if (ex.ErrorCode != "PersonGroupNotFound")
                {
                    MainWindow.Log("Response: {0}. {1}", ex.ErrorCode, ex.ErrorMessage);
                    return;
                }
                else
                {
                    MainWindow.Log("Response: Group {0} does not exist before.", GroupName);
                }
            }

            if (groupExists)
            {
                var cleanGroup = System.Windows.MessageBox.Show(string.Format("Requires a clean up for group \"{0}\" before setup new person database. Click OK to proceed, group \"{0}\" will be fully cleaned up.", GroupName), "Warning", MessageBoxButton.OKCancel);
                if (cleanGroup == MessageBoxResult.OK)
                {
                    await faceServiceClient.DeletePersonGroupAsync(GroupName);
                }
                else
                {
                    return;
                }
            }

            // Show folder picker
            System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog();
            var result = dlg.ShowDialog();

            // Set the suggestion count is intent to minimum the data preparation step only,
            // it's not corresponding to service side constraint
            const int SuggestionCount = 15;

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                // User picked a root person database folder
                // Clear person database
                Persons.Clear();
                TargetFaces.Clear();
                SelectedFile = null;

                // Call create person group REST API
                // Create person group API call will failed if group with the same name already exists
                MainWindow.Log("Request: Creating group \"{0}\"", GroupName);
                try
                {
                    await faceServiceClient.CreatePersonGroupAsync(GroupName, GroupName);
                    MainWindow.Log("Response: Success. Group \"{0}\" created", GroupName);
                }
                catch (FaceAPIException ex)
                {
                    MainWindow.Log("Response: {0}. {1}", ex.ErrorCode, ex.ErrorMessage);
                    return;
                }

                int processCount = 0;
                bool forceContinue = false;

                MainWindow.Log("Request: Preparing faces for identification, detecting faces in chosen folder.");

                // Enumerate top level directories, each directory contains one person's images
                foreach (var dir in System.IO.Directory.EnumerateDirectories(dlg.SelectedPath))
                {
                    var tasks = new List<Task>();
                    var tag = System.IO.Path.GetFileName(dir);
                    Person p = new Person();
                    p.PersonName = tag;

                    var faces = new ObservableCollection<Face>();
                    p.Faces = faces;

                    // Call create person REST API, the new create person id will be returned
                    MainWindow.Log("Request: Creating person \"{0}\"", p.PersonName);
                    p.PersonId = (await faceServiceClient.CreatePersonAsync(GroupName, p.PersonName)).PersonId.ToString();
                    MainWindow.Log("Response: Success. Person \"{0}\" (PersonID:{1}) created", p.PersonName, p.PersonId);

                    // Enumerate images under the person folder, call detection
                    foreach (var img in System.IO.Directory.EnumerateFiles(dir, "*.jpg", System.IO.SearchOption.AllDirectories))
                    {
                        tasks.Add(Task.Factory.StartNew(
                            async (obj) =>
                            {
                                var imgPath = obj as string;

                                using (var fStream = File.OpenRead(imgPath))
                                {
                                    try
                                    {
                                        // Update person faces on server side
                                        var persistFace = await faceServiceClient.AddPersonFaceAsync(GroupName, Guid.Parse(p.PersonId), fStream, imgPath);
                                        return new Tuple<string, ClientContract.AddPersistedFaceResult>(imgPath, persistFace);
                                    }
                                    catch (FaceAPIException)
                                    {
                                        // Here we simply ignore all detection failure in this sample
                                        // You may handle these exceptions by check the Error.Error.Code and Error.Message property for ClientException object
                                        return new Tuple<string, ClientContract.AddPersistedFaceResult>(imgPath, null);
                                    }
                                }
                            },
                            img).Unwrap().ContinueWith((detectTask) =>
                            {
                                // Update detected faces for rendering
                                var detectionResult = detectTask.Result;
                                if (detectionResult == null || detectionResult.Item2 == null)
                                {
                                    return;
                                }

                                this.Dispatcher.Invoke(
                                    new Action<ObservableCollection<Face>, string, ClientContract.AddPersistedFaceResult>(UIHelper.UpdateFace),
                                    faces,
                                    detectionResult.Item1,
                                    detectionResult.Item2);
                            }));
                        if (processCount >= SuggestionCount && !forceContinue)
                        {
                            var continueProcess = System.Windows.Forms.MessageBox.Show("The images loaded have reached the recommended count, may take long time if proceed. Would you like to continue to load images?", "Warning", System.Windows.Forms.MessageBoxButtons.YesNo);
                            if (continueProcess == System.Windows.Forms.DialogResult.Yes)
                            {
                                forceContinue = true;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    Persons.Add(p);

                    await Task.WhenAll(tasks);
                }

                MainWindow.Log("Response: Success. Total {0} faces are detected.", Persons.Sum(p => p.Faces.Count));

                try
                {
                    // Start train person group
                    MainWindow.Log("Request: Training group \"{0}\"", GroupName);
                    await faceServiceClient.TrainPersonGroupAsync(GroupName);

                    // Wait until train completed
                    while (true)
                    {
                        await Task.Delay(1000);
                        var status = await faceServiceClient.GetPersonGroupTrainingStatusAsync(GroupName);
                        MainWindow.Log("Response: {0}. Group \"{1}\" training process is {2}", "Success", GroupName, status.Status);
                        if (status.Status != Contract.Status.Running)
                        {
                            break;
                        }
                    }
                }
                catch (FaceAPIException ex)
                {
                    MainWindow.Log("Response: {0}. {1}", ex.ErrorCode, ex.ErrorMessage);
                }
            }
        }
        /// <summary>
        /// Pick the root person database folder, to minimum the data preparation logic, the folder should be under following construction
        /// Each person's image should be put into one folder named as the person's name
        /// All person's image folder should be put directly under the root person database folder
        /// </summary>
        /// <param name="sender">Event sender</param>
        /// <param name="e">Event argument</param>
        private async void FolderPicker_Click(object sender, RoutedEventArgs e)
        {
            bool groupExists = false;

            MainWindow mainWindow      = Window.GetWindow(this) as MainWindow;
            string     subscriptionKey = mainWindow._scenariosControl.SubscriptionKey;

            var faceServiceClient = new FaceServiceClient(subscriptionKey);

            // Test whether the group already exists
            try
            {
                MainWindow.Log("Request: Group {0} will be used for build person database. Checking whether group exists.", GroupName);

                await faceServiceClient.GetPersonGroupAsync(GroupName);

                groupExists = true;
                MainWindow.Log("Response: Group {0} exists.", GroupName);
            }
            catch (FaceAPIException ex)
            {
                if (ex.ErrorCode != "PersonGroupNotFound")
                {
                    MainWindow.Log("Response: {0}. {1}", ex.ErrorCode, ex.ErrorMessage);
                    return;
                }
                else
                {
                    MainWindow.Log("Response: Group {0} does not exist before.", GroupName);
                }
            }

            if (groupExists)
            {
                var cleanGroup = System.Windows.MessageBox.Show(string.Format("Requires a clean up for group \"{0}\" before setup new person database. Click OK to proceed, group \"{0}\" will be fully cleaned up.", GroupName), "Warning", MessageBoxButton.OKCancel);
                if (cleanGroup == MessageBoxResult.OK)
                {
                    await faceServiceClient.DeletePersonGroupAsync(GroupName);
                }
                else
                {
                    return;
                }
            }

            // Show folder picker
            System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog();
            var result = dlg.ShowDialog();

            // Set the suggestion count is intent to minimum the data preparation step only,
            // it's not corresponding to service side constraint
            const int SuggestionCount = 15;

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                // User picked a root person database folder
                // Clear person database
                Persons.Clear();
                TargetFaces.Clear();
                SelectedFile = null;

                // Call create person group REST API
                // Create person group API call will failed if group with the same name already exists
                MainWindow.Log("Request: Creating group \"{0}\"", GroupName);
                try
                {
                    await faceServiceClient.CreatePersonGroupAsync(GroupName, GroupName);

                    MainWindow.Log("Response: Success. Group \"{0}\" created", GroupName);
                }
                catch (FaceAPIException ex)
                {
                    MainWindow.Log("Response: {0}. {1}", ex.ErrorCode, ex.ErrorMessage);
                    return;
                }

                int  processCount  = 0;
                bool forceContinue = false;

                MainWindow.Log("Request: Preparing faces for identification, detecting faces in chosen folder.");

                // Enumerate top level directories, each directory contains one person's images
                foreach (var dir in System.IO.Directory.EnumerateDirectories(dlg.SelectedPath))
                {
                    var    tasks = new List <Task>();
                    var    tag   = System.IO.Path.GetFileName(dir);
                    Person p     = new Person();
                    p.PersonName = tag;

                    var faces = new ObservableCollection <Face>();
                    p.Faces = faces;

                    // Call create person REST API, the new create person id will be returned
                    MainWindow.Log("Request: Creating person \"{0}\"", p.PersonName);
                    p.PersonId = (await faceServiceClient.CreatePersonAsync(GroupName, p.PersonName)).PersonId.ToString();
                    MainWindow.Log("Response: Success. Person \"{0}\" (PersonID:{1}) created", p.PersonName, p.PersonId);

                    // Enumerate images under the person folder, call detection
                    foreach (var img in System.IO.Directory.EnumerateFiles(dir, "*.jpg", System.IO.SearchOption.AllDirectories))
                    {
                        tasks.Add(Task.Factory.StartNew(
                                      async(obj) =>
                        {
                            var imgPath = obj as string;

                            using (var fStream = File.OpenRead(imgPath))
                            {
                                try
                                {
                                    // Update person faces on server side
                                    var persistFace = await faceServiceClient.AddPersonFaceAsync(GroupName, Guid.Parse(p.PersonId), fStream, imgPath);
                                    return(new Tuple <string, ClientContract.AddPersistedFaceResult>(imgPath, persistFace));
                                }
                                catch (FaceAPIException)
                                {
                                    // Here we simply ignore all detection failure in this sample
                                    // You may handle these exceptions by check the Error.Error.Code and Error.Message property for ClientException object
                                    return(new Tuple <string, ClientContract.AddPersistedFaceResult>(imgPath, null));
                                }
                            }
                        },
                                      img).Unwrap().ContinueWith((detectTask) =>
                        {
                            // Update detected faces for rendering
                            var detectionResult = detectTask.Result;
                            if (detectionResult == null || detectionResult.Item2 == null)
                            {
                                return;
                            }

                            this.Dispatcher.Invoke(
                                new Action <ObservableCollection <Face>, string, ClientContract.AddPersistedFaceResult>(UIHelper.UpdateFace),
                                faces,
                                detectionResult.Item1,
                                detectionResult.Item2);
                        }));
                        if (processCount >= SuggestionCount && !forceContinue)
                        {
                            var continueProcess = System.Windows.Forms.MessageBox.Show("The images loaded have reached the recommended count, may take long time if proceed. Would you like to continue to load images?", "Warning", System.Windows.Forms.MessageBoxButtons.YesNo);
                            if (continueProcess == System.Windows.Forms.DialogResult.Yes)
                            {
                                forceContinue = true;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    Persons.Add(p);

                    await Task.WhenAll(tasks);
                }

                MainWindow.Log("Response: Success. Total {0} faces are detected.", Persons.Sum(p => p.Faces.Count));

                try
                {
                    // Start train person group
                    MainWindow.Log("Request: Training group \"{0}\"", GroupName);
                    await faceServiceClient.TrainPersonGroupAsync(GroupName);

                    // Wait until train completed
                    while (true)
                    {
                        await Task.Delay(1000);

                        var status = await faceServiceClient.GetPersonGroupTrainingStatusAsync(GroupName);

                        MainWindow.Log("Response: {0}. Group \"{1}\" training process is {2}", "Success", GroupName, status.Status);
                        if (status.Status != Contract.Status.Running)
                        {
                            break;
                        }
                    }
                }
                catch (FaceAPIException ex)
                {
                    MainWindow.Log("Response: {0}. {1}", ex.ErrorCode, ex.ErrorMessage);
                }
            }
        }
Beispiel #29
0
        private async Task LoadPersonGroup()
        {
            bool groupExists = false;

            _faceClient = new FaceServiceClient(Properties.Settings.Default.FaceAPIKey.Trim(), Properties.Settings.Default.FaceAPIHost);
            try
            {
                await _faceClient.GetPersonGroupAsync(GroupName);

                groupExists = true;
            }
            catch (FaceAPIException ex)
            {
                if (ex.ErrorCode != "PersonGroupNotFound")
                {
                    return;
                }
            }

            if (groupExists)
            {
                await _faceClient.DeletePersonGroupAsync(GroupName);
            }

            try
            {
                await _faceClient.CreatePersonGroupAsync(GroupName, GroupName);
            }
            catch (FaceAPIException ex)
            {
                return;
            }

            int invalidImageCount = 0;

            foreach (var dir in System.IO.Directory.EnumerateDirectories(@"D:\Hackathon_Live_Project\exit0\source\app\LiveCameraSample\Data\PersonGroup"))
            {
                var             tasks = new List <Task>();
                var             tag   = System.IO.Path.GetFileName(dir);
                PersonViewModel p     = new PersonViewModel();
                p.PersonName = tag;

                var faces = new ObservableCollection <FaceViewModel>();
                p.Faces = faces;

                p.PersonId = (await _faceClient.CreatePersonAsync(GroupName, p.PersonName)).PersonId.ToString();
                var pName = p.PersonName.Split('-');
                listOfNames.Add(p.PersonId, new UserDetails()
                {
                    UserName = pName[1].ToString(), ZoomId = pName[2].ToString()
                });

                string img;
                // Enumerate images under the person folder, call detection
                var imageList =
                    new ConcurrentBag <string>(
                        Directory.EnumerateFiles(dir, "*.*", SearchOption.AllDirectories)
                        .Where(s => s.ToLower().EndsWith(".jpg") || s.ToLower().EndsWith(".png") || s.ToLower().EndsWith(".bmp") || s.ToLower().EndsWith(".gif")));

                while (imageList.TryTake(out img))
                {
                    tasks.Add(Task.Factory.StartNew(
                                  async(obj) =>
                    {
                        var imgPath = obj as string;

                        using (var fStream = File.OpenRead(imgPath))
                        {
                            try
                            {
                                // Update person faces on server side
                                var persistFace = await _faceClient.AddPersonFaceAsync(GroupName, Guid.Parse(p.PersonId), fStream, imgPath);
                                return(new Tuple <string, ClientContract.AddPersistedFaceResult>(imgPath, persistFace));
                            }
                            catch (FaceAPIException ex)
                            {
                                // if operation conflict, retry.
                                if (ex.ErrorCode.Equals("ConcurrentOperationConflict"))
                                {
                                    imageList.Add(imgPath);
                                    return(null);
                                }
                                // if operation cause rate limit exceed, retry.
                                else if (ex.ErrorCode.Equals("RateLimitExceeded"))
                                {
                                    imageList.Add(imgPath);
                                    return(null);
                                }
                                else if (ex.ErrorMessage.Contains("more than 1 face in the image."))
                                {
                                    Interlocked.Increment(ref invalidImageCount);
                                }
                                // Here we simply ignore all detection failure in this sample
                                // You may handle these exceptions by check the Error.Error.Code and Error.Message property for ClientException object
                                return(new Tuple <string, ClientContract.AddPersistedFaceResult>(imgPath, null));
                            }
                        }
                    },
                                  img).Unwrap().ContinueWith(async(detectTask) =>
                    {
                        // Update detected faces for rendering
                        var detectionResult = detectTask?.Result;
                        if (detectionResult == null || detectionResult.Item2 == null)
                        {
                            return;
                        }

                        if (tasks.Count >= _maxConcurrentProcesses || imageList.IsEmpty)
                        {
                            await Task.WhenAll(tasks);
                            tasks.Clear();
                        }
                    }));

                    Persons.Add(p);
                }
                try
                {
                    await _faceClient.TrainPersonGroupAsync(GroupName);

                    // Wait until train completed
                    while (true)
                    {
                        await Task.Delay(2000);

                        var status = await _faceClient.GetPersonGroupTrainingStatusAsync(GroupName);

                        if (status.Status != Microsoft.ProjectOxford.Face.Contract.Status.Running)
                        {
                            break;
                        }
                    }
                }
                catch (FaceAPIException ex)
                {
                }
            }
            GC.Collect();
        }
Beispiel #30
0
        public static async Task <string> AddFace(MemoryStream faceStream, string personName, string groupId, string groupDisplayName, FaceServiceClient FaceClient, bool showMsgBox = true)
        {
            string statusStr;

            try
            {
                // Does PersonGroup already exist
                try
                {
                    await FaceClient.GetPersonGroupAsync(groupId);
                }
                catch (Exception)
                {
                    // person group does not exist - create it
                    await FaceClient.CreatePersonGroupAsync(groupId, groupDisplayName);

                    // FIX there needs to be a wait or something to detect the new personGroup
                    await FaceClient.GetPersonGroupAsync(groupId);
                }
                //Get list of faces if any
                Person[] people = await FaceClient.ListPersonsAsync(groupId);

                Person p = people.FirstOrDefault(myP => myP.Name.Equals(personName, StringComparison.OrdinalIgnoreCase));
                Guid   personId;
                if (p != null)
                {
                    // person already exists - train our model to include new picture
                    personId = p.PersonId;
                }
                else
                {
                    // personGroupId is the group to add the person to, personName is what the user typed in to identify this face
                    CreatePersonResult myPerson = await FaceClient.CreatePersonAsync(groupId, personName);

                    personId = myPerson.PersonId;
                }
                // Person - List Persons in a Person Group
                // Detect faces in the image and add
                await FaceClient.AddPersonFaceAsync(groupId, personId, faceStream);

                // whenever we add a face, docs says we need to retrain - do it!

                //await retrainPersonGroup(_options.PersonGroupId);

                //// I think this is needed
                await FaceClient.TrainPersonGroupAsync(groupId);

                while (true)
                {
                    TrainingStatus trainingStatus = await FaceClient.GetPersonGroupTrainingStatusAsync(groupId);

                    if (trainingStatus.Status != Status.Running)
                    {
                        break;
                    }

                    await Task.Delay(1000);
                }

                statusStr = $@"A new face with name '{personName}' has been added and / or trained successfully in the personGroup '{groupId}'";

                //await UpdatePersonListAsync();
                //await _frmMain.UpdatePersonListAsync();
            }
            catch (Exception ex)
            {
                statusStr = $@"Unhandled excpetion while trying to add face: {ex.Message}";
            }

            if (showMsgBox)
            {
                MessageBox.Show(statusStr);
            }
            return(statusStr);
        }
        /// <summary>
        /// Add photos to the training group using Microsoft Face API
        /// </summary>
        /// <param name="Photos">List of photos to add</param>
        /// <param name="PersonGroupID">Name of the training group</param>
        /// <returns></returns>
        public async Task addPhotosToTrainingGroup(Dictionary<string, PhotoPerson> Photos, string PersonGroupID)
        {
            IFaceServiceClient faceClient = new FaceServiceClient(SubscriptionKey);

            // Get the group and add photos to the group.
            // The input dictionary is organized by person ID.  The output dictionary is organized by the GUID returned by the added photo from the API.
            try
            {
                await faceClient.GetPersonGroupAsync(PersonGroupID);

                // training photos can support multiple pictures per person (more pictures will make the training more effective).  
                // each photo is added as a Face object within the Face API and attached to a person.

                foreach (PhotoPerson person in Photos.Values)
                {
                    Person p = new Person();
                    p.Name = person.Name;
                    p.PersonId = Guid.NewGuid();

                    List<Guid> faceIDs = new List<Guid>();

                    
                    foreach (Photo photo in person.Photos)
                    {
                        Stream stream = new MemoryStream(photo.Image);
                        Face[] face = await faceClient.DetectAsync(stream);

                        // check for multiple faces - should only have one for a training set.
                        if (face.Length != 1)
                            throw new FaceDetectionException("Expected to detect 1 face but found " + face.Length + " faces for person " + p.Name);
                        else
                            faceIDs.Add(face[0].FaceId);
                    }

                    Guid[] faceIDarray = faceIDs.ToArray();

                    // create the person in the training group with the image array of faces.
                    CreatePersonResult result = await faceClient.CreatePersonAsync(PersonGroupID, faceIDarray, p.Name, null);
                    p.PersonId = result.PersonId;
                    TrainingPhotos.Add(p.PersonId, person);

                }

                await faceClient.TrainPersonGroupAsync(PersonGroupID);
                // Wait until train completed
                while (true)
                {
                    await Task.Delay(1000);
                    var status = await faceClient.GetPersonGroupTrainingStatusAsync(PersonGroupID);
                    if (status.Status != "running")
                    {
                        break;
                    }
                }
            }
            catch (ClientException ex)
            {
                throw;
            }



        }
        private async void StartButton_Click(object sender, RoutedEventArgs e)
        {
            if (!CameraList.HasItems)
            {
                MessageArea.Text = "No cameras found; cannot start processing";
                return;
            }

            // Clean leading/trailing spaces in API keys.
            Properties.Settings.Default.FaceAPIKey    = Properties.Settings.Default.FaceAPIKey.Trim();
            Properties.Settings.Default.EmotionAPIKey = Properties.Settings.Default.EmotionAPIKey.Trim();
            Properties.Settings.Default.VisionAPIKey  = Properties.Settings.Default.VisionAPIKey.Trim();

            // Create API clients.
            _faceClient = new FaceServiceClient(Properties.Settings.Default.FaceAPIKey, Properties.Settings.Default.FaceAPIHost);
            //_faceClient = new FaceServiceClient("5d017c9a97c745c6a1e45c2a8edaec95", "https://southcentralus.api.cognitive.microsoft.com/face/v1.0");

            _emotionClient = new EmotionServiceClient(Properties.Settings.Default.EmotionAPIKey, Properties.Settings.Default.EmotionAPIHost);
            _visionClient  = new VisionServiceClient(Properties.Settings.Default.VisionAPIKey, Properties.Settings.Default.VisionAPIHost);

            // How often to analyze.
            _grabber.TriggerAnalysisOnInterval(Properties.Settings.Default.AnalysisInterval);

            // Reset message.
            MessageArea.Text = "";

            // Record start time, for auto-stop
            _startTime = DateTime.Now;

            // Process IP Cam
            if (CameraList.SelectedIndex == CameraList.Items.Count - 1)
            {
                await _grabber.StartProcessingCameraAsync(-1, Properties.Settings.Default.IPCamURL);
            }
            else
            {
                await _grabber.StartProcessingCameraAsync(CameraList.SelectedIndex);
            }


            //Windana

            _groupId = Properties.Settings.Default.FaceApiGroup;
            try
            {
                await _faceClient.GetPersonGroupAsync(_groupId);
            }
            catch
            {
                try
                {
                    await _faceClient.DeletePersonGroupAsync(_groupId);

                    await _faceClient.CreatePersonGroupAsync(_groupId, _groupId);
                }
                catch (Exception ex)
                {
                    BotClient_OnResponse(ex.Message, MessageType.Metadata);
                }
            }
            //End Windana
        }
Beispiel #33
0
 public async Task <PersonGroup> GetPersonGroup(string id)
 {
     return(await FaceServiceClient.GetPersonGroupAsync(id));
 }