Esempio n. 1
0
        /// <summary> Function which submits a frame to the Emotion API. </summary>
        /// <param name="frame"> The video frame to submit. </param>
        /// <returns> A <see cref="Task{LiveCameraResult}"/> representing the asynchronous API call,
        ///     and containing the emotions returned by the API. </returns>
        private async Task TrainCognitiveServicesAsync()
        {
            _faceClient = new FaceAPI.FaceServiceClient(Properties.Settings.Default.FaceAPIKey, Properties.Settings.Default.FaceAPIHost);

            var pgexists = await _faceClient.GetPersonGroupAsync(personGroupId);

            if (pgexists == null)
            {
                await _faceClient.CreatePersonGroupAsync(personGroupId, "My Friends");
            }

            // Define Jose
            CreatePersonResult Josefriend = await _faceClient.CreatePersonAsync(
                // Id of the PersonGroup that the person belonged to
                personGroupId,
                // Name of the person
                "Jose"
                );

            // Define Jose
            CreatePersonResult Aaronfriend = await _faceClient.CreatePersonAsync(
                // Id of the PersonGroup that the person belonged to
                personGroupId,
                // Name of the person
                "Aaron"
                );

            // Directory contains image files of Anna
            const string JoseImageDir = @"C:\Users\joseo\source\repos\FutureofWork-ServTechAPJ2018\FutureofWork-ServTech2018APJ\LiveCameraSample\Pictures\Jose\";

            foreach (string imagePath in Directory.GetFiles(JoseImageDir, "*.jpg"))
            {
                using (Stream s = File.OpenRead(imagePath))
                {
                    // Detect faces in the image and add to Anna
                    await _faceClient.AddPersonFaceAsync(
                        personGroupId, Josefriend.PersonId, s);
                }
            }
            // Do the same for Bill and Clare

            const string AaronImageDir = @"C:\Users\joseo\source\repos\FutureofWork-ServTechAPJ2018\FutureofWork-ServTech2018APJ\LiveCameraSample\Pictures\Aaron\";

            foreach (string imagePath in Directory.GetFiles(AaronImageDir, "*.jpg"))
            {
                using (Stream s = File.OpenRead(imagePath))
                {
                    // Detect faces in the image and add to Anna
                    await _faceClient.AddPersonFaceAsync(
                        personGroupId, Aaronfriend.PersonId, s);
                }
            }
            // Do the same for Bill and Clare


            await _faceClient.TrainPersonGroupAsync(personGroupId);

            TrainingStatus trainingStatus = null;

            while (true)
            {
                trainingStatus = await _faceClient.GetPersonGroupTrainingStatusAsync(personGroupId);

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

                await Task.Delay(1000);
            }

            Console.Write("Training successfully completed");
        }