コード例 #1
0
        /// <summary>
        /// Creates person group in Azure face recognition API
        /// </summary>
        /// <returns>boolean</returns>
        public async Task <bool> CreateGroup()
        {
            var people = await _databaseController.GetImageObjects();

            await _faceServiceClient.CreatePersonGroupAsync(_groupId, "fun");

            foreach (var person in people)
            {
                CreatePersonResult result = await _faceServiceClient.CreatePersonAsync(_groupId, person.Id.ToString());

                await _faceServiceClient.AddPersonFaceAsync(_groupId, result.PersonId, RecUtil.GetStreamFromUri(person.ImageContentUri));
            }

            await _faceServiceClient.TrainPersonGroupAsync(_groupId);

            TrainingStatus trainingStatus = null;

            while (true)
            {
                trainingStatus = await _faceServiceClient.GetPersonGroupTrainingStatusAsync(_groupId);

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

                await Task.Delay(1000);
            }

            return(true);
        }
コード例 #2
0
        public async Task <bool> DeletePersonAsync(Guid personId)
        {
            try
            {
                await _client.DeletePersonAsync(_groupId, personId);

                await _client.TrainPersonGroupAsync(_groupId);

                var trained = false;

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

                    var status = await _client.GetPersonGroupTrainingStatusAsync(_groupId);

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

                    case Status.Failed:
                        return(false);
                    }
                }
            }
            catch (Exception ex)
            {
                return(false);
            }

            return(true);
        }
コード例 #3
0
        private async Task LoadGroup()
        {
            try
            {
                // Récupère les groupes
                var groups = await _faceClient.ListPersonGroupsAsync();

                if (!groups.Any())
                {
                    // Première utilisation de l'api
                    await _faceClient.CreatePersonGroupAsync("1", "Miriot");

                    await _faceClient.TrainPersonGroupAsync(_miriotPersonGroupId);
                }
                else
                {
                    // Récupère l'id du premier groupe
                    _miriotPersonGroupId = groups.FirstOrDefault()?.PersonGroupId;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
コード例 #4
0
        private async void NewPersonGroup_Click(object sender, RoutedEventArgs e)
        {
            CreatePersonResult guest = await faceServiceClient.CreatePersonAsync(personGroupId, "Guest");

            //if (guest != null)
            //{
            //    await faceServiceClient.DeletePersonAsync(personGroupId, guest.PersonId);
            //    //The data of the past user has been deleted
            //}

            Stream s = await photoFile.OpenStreamForReadAsync();

            await faceServiceClient.AddPersonFaceAsync(personGroupId, guest.PersonId, s);

            // Training of the new user will now begin.
            status.Text = "Training of the new user will now begin";
            await faceServiceClient.TrainPersonGroupAsync(personGroupId);

            TrainingStatus sstatus = null;

            while (true)
            {
                sstatus = await faceServiceClient.GetPersonGroupTrainingStatusAsync(personGroupId);

                if (sstatus.Status != Status.Running)
                {
                    status.Text = "Person group training complete";
                    break;
                }
                await Task.Delay(1000);
            }
            status.Text = "Training of the new user has been completed. ";
        }
コード例 #5
0
        private async Task StartTrainingAsync()
        {
            Console.WriteLine("Training Group {0}", MAGIC_MIRROR_GROUP);
            await _faceApiClient.TrainPersonGroupAsync(MAGIC_MIRROR_GROUP);

            TrainingStatus trainingStatus = null;

            while (true)
            {
                trainingStatus = await _faceApiClient.GetPersonGroupTrainingStatusAsync(MAGIC_MIRROR_GROUP);

                if (trainingStatus.Status != Status.Running)
                {
                    if (trainingStatus.Status == Status.Failed)
                    {
                        Console.WriteLine("Trainig Failed!");
                        Console.WriteLine(trainingStatus.Message);
                    }
                    break;
                }

                Console.Write(".");
                await Task.Delay(1000);
            }

            Console.WriteLine("Group Trained");
        }
コード例 #6
0
        public async Task <IActionResult> linkFace([FromBody] CustReq req)
        {
            Claim oid      = User.Claims.FirstOrDefault(x => x.Type == "http://schemas.microsoft.com/identity/claims/objectidentifier");
            var   personId = await _userFunctions.getPersonIdAsync(oid.Value);

            if (String.IsNullOrEmpty(personId))
            {
                var person = await _faceClient.CreatePersonAsync(_configuration["personGroupId"], oid.Value);

                await _userFunctions.setPersonIdAsync(oid.Value, person.PersonId.ToString());

                personId = person.PersonId.ToString();
            }

            byte[]       bytes = Convert.FromBase64String(req.image);
            MemoryStream ms    = new MemoryStream(bytes);

            try
            {
                var returnedFace = await _faceClient.AddPersonFaceAsync(_configuration["personGroupId"], Guid.Parse(personId), ms);

                await _faceClient.TrainPersonGroupAsync(_configuration["personGroupId"]);

                return(Json(returnedFace));
            }
            catch (FaceAPIException e)
            {
                return(Json(new { error = e.ErrorMessage }));
            }
        }
コード例 #7
0
        /// <summary>
        /// Register face to Cognitive Face API
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        private async Task <Person> RegisterAsync(InMemoryRandomAccessStream stream)
        {
            if (SelectedRSVP == null)
            {
                Message = "Select your RSVP.";
            }

            while (SelectedRSVP == null)
            {
                await Task.Delay(1000);
            }

            // All the members should be registered when initialized.
            var registeredPerson = registeredPersons.First(x => x.Name == SelectedRSVP.Member.Name);

            // Register face information and discard image.
            var addPersistedFaceResult = await faceClient.AddPersonFaceAsync(
                Settings.PersonGroupId, registeredPerson.PersonId, ImageConverter.ConvertImage(stream));

            stream.Dispose();

            await faceClient.TrainPersonGroupAsync(Settings.PersonGroupId);

            return(await faceClient.GetPersonAsync(Settings.PersonGroupId, registeredPerson.PersonId));
        }
コード例 #8
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);
        }
コード例 #9
0
        private async void CreateGroupButtonMethod()
        {
            faceServiceClient = new FaceServiceClient("fbeb228a7f1943b69c3e6e2861f22ff0");

            //var client = new HttpClient();
            //var queryString = WebUtility.UrlEncode(string.Empty);
            string personGroupId = "group1";
            Guid   personId      = new Guid("a3f0bd5c-b917-41f7-b50f-ae6fb2e3c6a2");
            //// Request header
            //client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "fbeb228a7f1943b69c3e6e2861f22ff0");
            //var uri = "https://westus.api.cognitive.microsoft.com/face/v1.0/persongroups/" + personGroupId + "/persons/"+ personId +"/persistedFaces" ;

            //HttpResponseMessage response;

            MediaCapture mediaCapture = new MediaCapture();
            await mediaCapture.InitializeAsync();

            photo = await KnownFolders.PicturesLibrary.CreateFileAsync(
                "capture1.jpg", CreationCollisionOption.ReplaceExisting);

            ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg();
            await mediaCapture.CapturePhotoToStorageFileAsync(imageProperties, photo);

            //Request body
            Stream image = await photo.OpenStreamForReadAsync();

            await faceServiceClient.AddPersonFaceAsync(personGroupId, personId, image);

            await faceServiceClient.TrainPersonGroupAsync(personGroupId);


            //var faces = await faceServiceClient.DetectAsync(image);
            //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)
            //    {

            //    }
            //    else
            //    {
            //        var candidateId = identifyResult.Candidates[0].PersonId;
            //        var person = await faceServiceClient.GetPersonAsync(personGroupId, candidateId);

            //    }
            //}
            //string body = JsonConvert.SerializeObject(new
            //{
            //    url =  photo.Path,
            //});
            //byte[] byteData = Encoding.UTF8.GetBytes(body);

            //using (var content = new ByteArrayContent(byteData))
            //{
            //    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            //    response = await client.PostAsync(uri, content);
            //}
        }
        private async void TrainPersonGroup(object obj)
        {
            try
            {
                await _faceServiceClient.TrainPersonGroupAsync(SelectedPersonGroup.PersonGroupId);

                while (true)
                {
                    TrainingStatus trainingStatus = await _faceServiceClient.GetPersonGroupTrainingStatusAsync(SelectedPersonGroup.PersonGroupId);

                    if (trainingStatus.Status != Microsoft.ProjectOxford.Face.Contract.Status.Running)
                    {
                        StatusText = $"Person group finished with status: {trainingStatus.Status}";
                        break;
                    }

                    StatusText = "Training person group...";
                    await Task.Delay(1000);
                }
            }
            catch (FaceAPIException ex)
            {
                StatusText = $"Failed to train person group: {ex.ErrorMessage}";
            }
            catch (Exception ex)
            {
                StatusText = $"Failed to train person group: {ex.Message}";
            }
        }
コード例 #11
0
        /// <summary>
        /// Entrena el algoritmo de identificacion con los rostros del grupo.
        /// </summary>
        /// <returns>The person group.</returns>
        public async Task TrainPersonGroup()
        {
            try
            {
                await FaceServiceClient.TrainPersonGroupAsync(PersonGroupId);

                TrainingStatus trainingStatus = null;

                while (true)
                {
                    trainingStatus = await FaceServiceClient.GetPersonGroupTrainingStatusAsync(PersonGroupId);

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

                    await Task.Delay(1000);
                }
            }
            catch
            {
                return;
            }
        }
コード例 #12
0
        private async void SaveButtonClick()
        {
            if (PersonName != "")
            {
                string personGroupId = "group1";
                Stream image         = await file.OpenStreamForReadAsync();

                Person[] persons = await faceServiceClient.GetPersonsAsync(personGroupId);

                Person person;
                Guid   personId;
                try{
                    person   = persons.Where(item => item.Name == personName).First();
                    personId = person.PersonId;
                }
                catch
                {
                    CreatePersonResult friend1 = await faceServiceClient.CreatePersonAsync(
                        personGroupId,
                        personName
                        );

                    personId = friend1.PersonId;
                }
                await faceServiceClient.AddPersonFaceAsync(personGroupId, personId, image);

                await faceServiceClient.TrainPersonGroupAsync(personGroupId);

                NameStackVisibility = "Collapsed";
                TestText           += "\nThe Person will automatically be allowed next time";
            }
        }
コード例 #13
0
        /// <summary>
        /// Trains the Facial Recognition Model in the Azure Face Resource
        /// </summary>
        /// <param name="sender">A sender object</param>
        /// <param name="e">RoutedEventArgs</param>
        /// <remarks>
        /// <para>Model training is blackboxed</para>
        /// </remarks>
        private async void TrainButton_ClickAsync(object sender, RoutedEventArgs e)
        {
            if (personGroupId.Length > 0)
            {
                TrainStatusTextBlock.Foreground = new SolidColorBrush(Colors.Green);
                await ApiCallAllowed(true);

                await faceServiceClient.TrainPersonGroupAsync(personGroupId);

                TrainingStatus trainingStatus = null;
                while (true)
                {
                    await ApiCallAllowed(true);

                    trainingStatus = await faceServiceClient.GetPersonGroupTrainingStatusAsync(personGroupId);

                    if (trainingStatus.Status != Status.Running)
                    {
                        break;
                    }
                    await Task.Delay(1000);
                }

                TrainStatusTextBlock.Text = "Submission Status: Training Completed!";
            }
            else
            {
                TrainStatusTextBlock.Foreground = new SolidColorBrush(Colors.Red);
                TrainStatusTextBlock.Text       = "Submission Status: No person group ID found. Have you completed section two?";
            }
        }
コード例 #14
0
        private async void CmdUpdateGroup_ClickAsync(object sender, EventArgs e)
        {
            await _options.UpdateMemberVarsFromProperties();

            bool   Internet         = _options.Internet;
            bool   containerMode    = _options.ContainerMode;
            bool   PersonGroupExist = _options.personGroupFound;
            string CSKey            = _options.CSKey;
            string CSEndpoint       = _options.CSEndpoint;
            string PersonGroupId    = _options.PersonGroupId;
            string msgType1         = "First";
            string msgType2         = "PersonGroups";
            string msgType3         = "PersonGroups";
            string msgType4         = "Internet";

            if (!string.IsNullOrWhiteSpace(_options.CSKey) && !string.IsNullOrWhiteSpace(_options.FaceEndpoint))
            {
                string userData    = cmdSetColor.BackColor.ToArgb().ToString();
                string group       = cmbPersonGroups.Text;
                string displayName = txtPersonGroupDisplay.Text;  //cmbPersonGroups.Text;

                string faceKey = _options.CSKey;
                string faceEnd = _options.FaceEndpoint;
                _faceClient = new FaceServiceClient(faceKey, faceEnd);
                await _faceClient.UpdatePersonGroupAsync(group, displayName, userData);

                await _faceClient.TrainPersonGroupAsync(group);
            }
        }
コード例 #15
0
        public async Task <bool> RegisterUser(User user, IEnumerable <byte[]> imgs)
        {
            try
            {
                var persRes = await _client.CreatePersonAsync("users_id", user._id.ToString());

                foreach (var img in imgs)
                {
                    using (var s = new MemoryStream(img))
                    {
                        try
                        {
                            var faceRes = await _client.AddPersonFaceAsync("users_id", persRes.PersonId, s);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"RegisterUser.AddImage: {e.Message}");
                        }
                    }
                }
                await _client.TrainPersonGroupAsync("users_id");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"RegisterUser: {ex.ToString()}");
                return(false);
            }

            return(true);
        }
コード例 #16
0
        public async Task Patch() //TrainMembers
        {
            FaceServiceClient faceClient = new FaceServiceClient(Configuration.Get("AppSettings:OxfordSubscriptionKeyPrimary"));
            await faceClient.TrainPersonGroupAsync(Configuration.Get("AppSettings:OxfordSubscriptionKeyPrimary"));

            return;
        }
コード例 #17
0
        public static async Task ProcessQueueMessage([QueueTrigger("users")] UserFaceUpdateRequest request,
                                                     TextWriter log)
        {
            var cloudAccount =
                CloudStorageAccount.Parse(
                    ConfigurationManager.ConnectionStrings["AzureWebJobsStorage"].ConnectionString);
            var blobClient    = cloudAccount.CreateCloudBlobClient();
            var logTable      = cloudAccount.CreateCloudTableClient().GetTableReference("logs");
            var blobContainer =
                blobClient.GetContainerReference(ConfigurationManager.AppSettings["UsersImagesContainerName"]);

            try
            {
                var blob = blobContainer.GetBlockBlobReference(request.Image);

                var faceClient = new FaceServiceClient(OxfordKey);

                using (var stream = await blob.OpenReadAsync())
                    await faceClient.AddPersonFaceAsync(XebienzaPersonGroup, request.Id, stream);

                await faceClient.TrainPersonGroupAsync(XebienzaPersonGroup);

                await LogAsunc(logTable, "Message processed !", Level.Info);
            }
            catch (Exception e)
            {
                await LogAsunc(logTable, "Error while processing message", Level.Error, e);
            }
        }
コード例 #18
0
        private static async Task TrainAsync()
        {
            List <Stream> streams = new List <Stream>();

            for (int i = 1; i < 7; i++)
            {
                string filesString       = @"ms-appx:///Assets/Training (" + i + ").jpg";
                var    randomAcessStream = await RandomAccessStreamReference.CreateFromUri(new Uri(filesString)).OpenReadAsync();

                var stream = randomAcessStream.AsStream();
                streams.Add(stream);
            }
            var faces = new List <Face>();

            foreach (var item in streams)
            {
                var dfaces = await _Client.DetectAsync(item);

                faces.AddRange(dfaces);
            }

            await _Client.CreatePersonGroupAsync(Constants.OwnerGroupId, Constants.OwnerName);

            await _Client.CreatePersonAsync(Constants.OwnerGroupId, faces.Select(f => f.FaceId).ToArray(), Constants.OwnerName);

            await _Client.TrainPersonGroupAsync(Constants.OwnerGroupId);
        }
コード例 #19
0
ファイル: AzureFaceAdapter.cs プロジェクト: ddangelorb/cg4u
 public async Task TrainPersonGroupAsync(string personGroupId, ConnectionApiData connectionApiData)
 {
     using (var faceClient = new FaceServiceClient(connectionApiData.SubscriptionKey, connectionApiData.Location))
     {
         await faceClient.TrainPersonGroupAsync(personGroupId);
     }
 }
コード例 #20
0
        private async Task <bool> TrainModel()
        {
            bool trainingSucceeded = false;

            TrainingStatus trainingStatus = null;

            try
            {
                await faceServiceClient.TrainPersonGroupAsync(PersonGroup);

                while (true)
                {
                    trainingStatus = await faceServiceClient.GetPersonGroupTrainingStatusAsync(PersonGroup);

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

                    await Task.Delay(1000);
                }
            }
            catch (FaceAPIException ex)
            {
                Debug.WriteLine($"Error training the model: {ex.ErrorMessage}");
            }

            if (trainingStatus.Status == Status.Succeeded)
            {
                trainingSucceeded = true;
            }

            return(trainingSucceeded);
        }
コード例 #21
0
        /// <summary>
        /// 検出した顔から人を認識する。
        /// </summary>
        /// <param name="senderId">LINE Messagin Service の SenderId</param>
        /// <param name="faces">検出した顔(複数)</param>
        /// <returns>認識した <see cref="Person"/> の配列を返す。認識できなかった場合は要素数 0 の配列を返す。</returns>
        public async Task <Person[]> FindPersonByFacesAsync(string senderId, Face[] faces)
        {
            var personGroupId = SenderIdToPersonGroupId(senderId);

            IdentifyResult[] result = null;
            try
            {
                // Cognitive Face Identify API で Face から Person を認識
                Guid[] guids = faces.Select(face => face.FaceId).Take(10).ToArray(); // Identify API の Face は最大 10 個まで
                result = await _faceServiceClient.IdentifyAsync(personGroupId, guids, 0.6f);
            }
            catch (FaceAPIException e)
            {
                if (e.ErrorCode == "PersonGroupNotTrained")
                {
                    // 学習が必要なら学習する
                    await _faceServiceClient.TrainPersonGroupAsync(personGroupId);
                }

                // 例外の場合は見つからなかった結果として返す
                return(new Person[faces.Length]);
            }

            if (result.Count() == 0)
            {
                // 結果が空の場合は見つからなかった結果として返す
                return(new Person[faces.Length]);
            }
            else
            {
                // Face に対応する Person の配列を生成
                var persons = new Person[faces.Length];
                for (int i = 0; i < faces.Length && i < result.Length; i++)
                {
                    if (result[i].Candidates.Count() > 0)
                    {
                        var personId = result[i].Candidates[0].PersonId.ToString();
                        var person   = await _db.Person.Where(p => p.PersonGroupId == personGroupId && p.PersonId == personId).FirstOrDefaultAsync();

                        persons[i] = person;
                    }
                }

                // Person の配列を返す
                return(persons);
            }
        }
コード例 #22
0
        public async Task <string> Post(HttpRequestMessage req)
        {
            Console.WriteLine("Uploading Image...");
            try
            {
                string response = "";

                var createPersonResult = await faceServiceClient.CreatePersonAsync(Request.Query["personGroupId"], Request.Query["personName"]);

                response = $"Person Created: {createPersonResult.PersonId.ToString()}\n";


                Console.WriteLine($"Person Created: {createPersonResult.PersonId.ToString()}\n");



                using (Stream s = Request.Body)
                {
                    var addPersonFaceResult = await faceServiceClient.AddPersonFaceAsync(Request.Query["personGroupId"], createPersonResult.PersonId, s);

                    response = response + $"Face Added: {addPersonFaceResult.PersistedFaceId.ToString()}\n";

                    Console.WriteLine($"Face Added: {addPersonFaceResult.PersistedFaceId.ToString()}\n");

                    CloudTable PersonInfo = await CreateTableAsync("PersonInfo");

                    TableBatchOperation batchOperation = new TableBatchOperation();
                    var personEntity = new PersonInfo()
                    {
                        PersonName = Request.Query["personName"], PersonCompany = Request.Query["personCompany"]
                    };

                    var tableEntity = personEntity as TableEntity;
                    tableEntity.PartitionKey = createPersonResult.PersonId.ToString();
                    tableEntity.RowKey       = addPersonFaceResult.PersistedFaceId.ToString();


                    batchOperation.Insert(personEntity);
                    IList <TableResult> results = await PersonInfo.ExecuteBatchAsync(batchOperation);

                    if (results.Count > 0)
                    {
                        response = response + $"Table Updated\n";
                        Console.WriteLine($"Table Updated\n");
                    }

                    await faceServiceClient.TrainPersonGroupAsync(personGroupId);

                    Console.WriteLine($"Training Started\n");
                }


                return(response);
            }
            catch (Exception ex)
            {
                return(ex.ToString());
            }
        }
コード例 #23
0
        private async void AddFaceToPerson()
        {
            Object userid   = settings.Values["userid"];
            Object personid = settings.Values["personid"];


            if (userid != null && personid != null)
            {
                enroll_progressbar.Visibility = Visibility.Visible;
                NextBtn.IsEnabled             = false;

                if (file != null)
                {
                    using (var fileStream = await file.OpenStreamForReadAsync())
                    {
                        LoggingMsg("Add face for person to server...");
                        try
                        {
                            var faceServiceClient = new FaceServiceClient(subscriptionKey);
                            AddPersistedFaceResult addPersistedFaceResult = await faceServiceClient.AddPersonFaceAsync(PersonGroupId, (Guid)personid, fileStream);

                            Guid FaceId = addPersistedFaceResult.PersistedFaceId;
                            if (FaceId != null)
                            {
                                settings.Values["faceid"] = FaceId;
                                LoggingMsg("Sync face image with server success. FaceId = " + FaceId);
                                NextBtn.IsEnabled = true;
                                AddFaceSuccess    = true;
                            }

                            //start training
                            await faceServiceClient.TrainPersonGroupAsync(PersonGroupId);

                            enroll_progressbar.Visibility = Visibility.Collapsed;
                        }
                        catch (FaceAPIException ex)
                        {
                            enroll_progressbar.Visibility = Visibility.Collapsed;
                            NextBtn.IsEnabled             = false;
                            Debug.WriteLine("Response: {0}. {1}", ex.ErrorCode, ex.ErrorMessage);
                            LoggingMsg("Sync face for person to server failed.");
                        }
                        catch
                        {
                            enroll_progressbar.Visibility = Visibility.Collapsed;
                            NextBtn.IsEnabled             = false;
                            LoggingMsg("Sync face for person to server failed.");
                        }
                    }
                }
                else
                {
                    //  file missing
                    LoggingMsg("Image file missing. Please choose image again.");
                    NextBtn.IsEnabled             = false;
                    enroll_progressbar.Visibility = Visibility.Collapsed;
                }
            }
        }
コード例 #24
0
        public async Task <ActionResult> TrainGroup(string groupId)
        {
            using (var client = new FaceServiceClient(_faceApiKey, _faceApiEndpoint))
            {
                await client.TrainPersonGroupAsync(groupId);
            }

            return(Ok());
        }
コード例 #25
0
        public async Task <bool> Face()
        {
            var test = true;
            FaceServiceClient faceServiceClient = new FaceServiceClient("57dfa44fcc82469fb81ec031ba2f43c6", "https://westcentralus.api.cognitive.microsoft.com/face/v1.0");
            // Create an empty PersonGroup
            string personGroupId = "myfriends";
            //await faceServiceClient.CreatePersonGroupAsync(personGroupId, "MyFriends");

            // Define Anna
            CreatePersonResult friend1 = await faceServiceClient.CreatePersonAsync(
                // Id of the PersonGroup that the person belonged to
                personGroupId,
                // Name of the person
                "toto"
                );

            const string friend1ImageDir = @"C:/Users/Megaport/Pictures/face";

            foreach (string imagePath in Directory.GetFiles(friend1ImageDir, "*.jpg"))
            {
                using (Stream s = File.OpenRead(imagePath))
                {
                    // Detect faces in the image and add to Anna
                    await faceServiceClient.AddPersonFaceAsync(
                        personGroupId, friend1.PersonId, s);
                }
            }
            await faceServiceClient.TrainPersonGroupAsync(personGroupId);

            TrainingStatus trainingStatus = null;

            trainingStatus = await faceServiceClient.GetPersonGroupTrainingStatusAsync(personGroupId);

            string testImageFile = "ftp://35.190.168.129/images/test1.png";

            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)
                {
                    if (identifyResult.Candidates.Length == 0)
                    {
                        test = false;
                    }
                    else
                    {
                        test = true;
                    }
                }
            }
            return(test);
        }
コード例 #26
0
ファイル: FaceService.cs プロジェクト: b3540/HowOldChomado
        private async Task TrainPersonGroupAsync(FaceServiceClient client, string personListId)
        {
            await client.TrainPersonGroupAsync(personListId);

            while ((await client.GetPersonGroupTrainingStatusAsync(personListId)).Status == Status.Running)
            {
                await Task.Delay(1000);
            }
        }
コード例 #27
0
        /// <summary>
        /// 針對人員群組進行訓練的動作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void btnPersonGroupTrain_Click(object sender, EventArgs e)
        {
            string strPersonGroupId = ((Models.CognitiveModels.ListItem)lbxPersonGroup.SelectedItem).Value;
            await face.TrainPersonGroupAsync(strPersonGroupId);

            this.BindPersonGroups();
            btnDeletePersonGroup.Enabled = false;
            btnPersonGroupTrain.Enabled  = false;
        }
コード例 #28
0
ファイル: RecServices.cs プロジェクト: DevLume/whos-that
        /// <summary>
        /// Inserts new image
        /// </summary>
        /// <param name="user"></param>
        /// <returns>boolean</returns>
        public async Task <bool> InsertPersonInToGroup(UserModel user)
        {
            CreatePersonResult result = await _faceServiceClient.CreatePersonAsync(_groupId, user.Id.ToString());

            //Mock error heandling
            if (result == null)
            {
                return(false);
            }

            RecognitionUtils recUtils = new RecognitionUtils();

            await _faceServiceClient.AddPersonFaceAsync(_groupId, result.PersonId, recUtils.GetStream(user.ImageUri));

            await _faceServiceClient.TrainPersonGroupAsync(_groupId);

            return(true);
        }
コード例 #29
0
ファイル: FaceClient.cs プロジェクト: jwkuper/Mobile3
        async Task <Customer> RegisterCustomer(Customer customer, string photoUri, string personGroupId)
        {
            try
            {
                CreatePersonResult personResult = await Service.CreatePersonAsync(personGroupId, customer.RegistrationName());

                customer.PersonId = personResult.PersonId;

                var faceResult = await Service.AddPersonFaceAsync(personGroupId, personResult.PersonId, photoUri);

                customer.FaceId = faceResult.PersistedFaceId;

                await Service.TrainPersonGroupAsync(personGroupId);
            }
            catch (FaceAPIException ex)
            {
                switch (ex.ErrorCode)
                {
                case "PersonGroupNotFound":
                    break;

                case "FaceNotFound":
                    break;

                case "PersonGroupNotTrained":
                    break;

                case "BadArgument":
                    break;

                default:
                    break;
                }
                //errorDescription = "IdentifyFace: " + ex.ErrorCode;
                //currentLog.Info(errorDescription);
            }
            catch (Exception ex)
            {
                // Something bad
                throw ex;
            }

            return(customer);
        }
コード例 #30
0
        async void ExecuteTrainFaceAPI()
        {
            var people = new List <Person>
            {
                new Person {
                    FullName = "Shaun",
                    PhotoUrl = "https://scontent-mxp1-1.xx.fbcdn.net/v/t1.0-9/12038495_916990261703186_7575801463680622880_n.jpg?oh=db07dbdb3f728b56460545d3991d1063&oe=58C4A1BF"
                },
                new Person {
                    FullName = "Katya",
                    PhotoUrl = "https://scontent-mxp1-1.xx.fbcdn.net/v/t1.0-9/13659191_1380711775279518_6270811927728068493_n.jpg?oh=b143f5356842bb338ab05d24b786b748&oe=58C612CD"
                },
                new Person {
                    FullName = "Kevin",
                    PhotoUrl = "https://www.pizza4u.com.mt/media/1005/kevin-farrugia.jpg"
                },
                new Person {
                    FullName = "Gosia",
                    PhotoUrl = "https://scontent-mxp1-1.xx.fbcdn.net/v/t1.0-9/10347484_10201839306779156_4563104125806602622_n.jpg?oh=15fdba843aed15636ae0f5ac209ce6e1&oe=58B664D1"
                },
                new Person {
                    FullName = "Alberto",
                    PhotoUrl = "https://trello-attachments.s3.amazonaws.com/568fc6d9a159da4a0a38a6e5/278x359/6f5b8c4486f5d059b9b6f91a0d2c04c4/4a6f310c-a2a1-49e0-8df5-3c859485207c.jpg"
                }
            };

            try
            {
                IsBusy = true;

                // Step 1 - Create Person Group
                personGroupId = Guid.NewGuid().ToString();
                await faceServiceClient.CreatePersonGroupAsync(personGroupId, "Incredible Web Employees");

                // Step 2 - Add persons (and faces) to person group.
                foreach (var person in people)
                {
                    // Step 2a - Create a new person, identified by their name.
                    var p = await faceServiceClient.CreatePersonAsync(personGroupId, person.FullName);

                    // Step 3a - Add a face for that person.
                    await faceServiceClient.AddPersonFaceAsync(personGroupId, p.PersonId, person.PhotoUrl);
                }

                // Step 3 - Train facial recognition model.
                await faceServiceClient.TrainPersonGroupAsync(personGroupId);

                IsBusy = false;
            }
            catch (Exception ex)
            {
                IsBusy = false;
                UserDialogs.Instance.ShowError("Training has not finished");
            }
        }
コード例 #31
0
        /// <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;
            }



        }
コード例 #32
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>
        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);
                }
            }
        }