Example #1
0
        public async Task <IActionResult> GetSASAccessToken(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "azure/{container}/uri")] HttpRequest req, string container)
        {
            var validateStatus = base.AuthorizationStatus(req);

            if (validateStatus != System.Net.HttpStatusCode.Accepted)
            {
                return(new BadRequestObjectResult(validateStatus));
            }

            var response = await _blobService.GetSasUri(container);

            return(new OkObjectResult(response));
        }
        /// <summary>
        /// The TrainStudentModel.
        /// </summary>
        /// <param name="queueDataMessage">The queueDataMessage<see cref="QueueDataMessage"/>.</param>
        /// <param name="_log">The _log<see cref="ILogger"/>.</param>
        /// <returns>The <see cref="Task"/>.</returns>
        public async Task TrainStudentModel(QueueDataMessage queueDataMessage, ILogger _log)
        {
            //Create school group
            _log.LogInformation($"TrainStudentModel: {queueDataMessage.StudentId}");
            CreateGroup(queueDataMessage.SchoolId, _log);

            var pictureURLs = new List <string>();

            try
            {
                // Define school specific student
                _log.LogInformation($"Define school specific student");
                Person student = await _faceClient.PersonGroupPerson.CreateAsync(
                    // Id of the PersonGroup that the person belonged to
                    queueDataMessage.SchoolId,
                    // studentId
                    queueDataMessage.StudentId
                    ).ConfigureAwait(false);

                BlobStorageRequest blobStorage = await _azureBlobService.GetSasUri("students");

                _log.LogInformation($"BlobStorageRequest {blobStorage.StorageUri}");

                foreach (var blobUri in queueDataMessage.PictureURLs)
                {
                    var blobUriBuilder = new System.UriBuilder($"{blobStorage.StorageUri}students/{blobUri}")
                    {
                        Query = blobStorage.StorageAccessToken
                    };
                    _log.LogInformation($"blobUriBuilder { blobUriBuilder.Uri.AbsoluteUri}");

                    try
                    {
                        await _faceClient.PersonGroupPerson.AddFaceFromUrlAsync(queueDataMessage.SchoolId, student.PersonId, blobUriBuilder.Uri.AbsoluteUri, detectionModel : DetectionModel.Detection01);

                        pictureURLs.Add(blobUri);
                        _log.LogInformation($"AddFaceFromUrlAsync Done");
                    }
                    catch (Exception ex)
                    {
                        _log.LogError(ex, $"AddFaceFromUrlAsync APIErrorException: {queueDataMessage.StudentId} URI: {blobUriBuilder.Uri.AbsoluteUri}");
                    }
                }

                _log.LogInformation($"Train the PersonGroup Start {queueDataMessage.SchoolId}");

                // Train the PersonGroup
                await _faceClient.PersonGroup.TrainAsync(queueDataMessage.SchoolId).ConfigureAwait(false);

                //while (true)
                //{
                //    Task.Delay(1000).Wait();
                //    var status = await _faceClient.LargeFaceList.GetTrainingStatusAsync(queueDataMessage.SchoolId);

                //    if (status.Status == TrainingStatusType.Running)
                //    {
                //        _log.LogInformation($"Training Running status ({queueDataMessage.StudentId}): {status.Status}");
                //        continue;
                //    }
                //    else if (status.Status == TrainingStatusType.Succeeded)
                //    {
                //        _log.LogInformation($"Training Succeeded status ({queueDataMessage.StudentId}): {status.Status}");
                //        break;
                //    }
                //}

                Task.Delay(1000).Wait();

                await _studentService.UpdateStudentProfile(queueDataMessage.StudentId, pictureURLs);

                _log.LogInformation($"Train the PersonGroup Done {queueDataMessage.SchoolId}: {queueDataMessage.StudentId}");
            }
            // Catch and display Face API errors.
            catch (APIErrorException ex)
            {
                _log.LogError(ex, $"TrainStudentModel APIErrorException: {queueDataMessage.StudentId}");
                throw new AppException("APIErrorException: ", ex.InnerException);
            }
            // Catch and display all other errors.
            catch (Exception ex)
            {
                _log.LogError(ex, $"TrainStudentModel Error: {queueDataMessage.StudentId}");
                throw new AppException("Train Student Cognitive Service Error: ", ex.InnerException);
            }
        }