/// <summary> /// Recreates the Azure Speaker Recognition API profile for an existing user. /// Used when API profile experiece. /// /// Also updates the profile GUID for the corresponding record in the DiScribe database. /// </summary> /// <param name="client"></param> /// <param name="locale"></param> /// <returns></returns> private async Task <Guid> RefreshAPIProfile(User user, string locale = "en-us") { var taskComplete = new TaskCompletionSource <Guid>(); Task <CreateProfileResponse> profileTask = null; try { profileTask = EnrollmentClient.CreateProfileAsync(locale); await profileTask; user.ProfileGUID = profileTask.Result.ProfileId; } catch (AggregateException ex) { Console.Error.WriteLine("Error creating user profile with Azure Speaker Recognition endpoint\n" + ex.InnerException.Message); var failGUID = new Guid(); taskComplete.SetResult(failGUID); return(failGUID); } /*Synchronize object with DiScribe database. Note that if this operation fails, it is not fatal. * The db record will not be consistent with the User object in memory, but application * execution will continue normally otherwise regardless. */ try { user.Update(); } catch (Exception ex) { Console.Error.WriteLine("Unable to update User record in database:" + ex.ToString()); } taskComplete.SetResult(profileTask.Result.ProfileId); return(user.ProfileGUID); }
public ActionResult EnrollCre(EnrollmentViewModel pro) { EnrollmentClient CC = new EnrollmentClient(); CC.EnrollCre(pro.enro); return(RedirectToAction("HomePage", "Home")); }
// GET: Enrollment public ActionResult EnrollDet() { EnrollmentClient enro = new EnrollmentClient(); ViewBag.listenrollment = enro.EnrollfindAll(); return(View()); }
/// <summary> /// Delete a profile from the Azure Speaker Recognition endpoint and delete /// the matching record in the DiScribe database. /// </summary> /// <param name="email"></param> /// <returns>True on success, false on fail</Boolean></returns> private async Task <Boolean> DeleteProfile(string email) { List <int> removeIndexes = new List <int>(); for (int i = 0; i < UserProfiles.Count; i++) { /*Remove the User from the list of user profiles managed by this instance */ if (UserProfiles[i].Email == email) { removeIndexes.Add(i); } } foreach (var index in removeIndexes) { UserProfiles.RemoveAt(index); } var taskComplete = new TaskCompletionSource <Boolean>(); var user = CheckProfileExists(email).Result; if (user == null) { taskComplete.SetResult(false); return(false); } /*Delete profile Azure Spekaer Recognition endpoint */ try { await EnrollmentClient.DeleteProfileAsync(user.ProfileGUID); } catch (Exception ex) { Console.Error.WriteLine($"Unable delete user from Azure Speaker Recognition endpoint. Continuing with DB profile delete {ex.Message}"); } /*Delete user profile from DiScribe database */ Boolean dbDelete = user.Delete(); taskComplete.SetResult(dbDelete); return(dbDelete); }
/// <summary> /// Creates a new user profile for a User in the DiScribe database. /// /// Also creates a corresponding profile with the Azure Speaker Recognition /// endpoint and returns the GUID for that profile on success. /// /// /// </summary> /// <param name="client"></param> /// <param name="locale"></param> /// <returns>Created profile GUID or GUID {00000000-0000-0000-0000-000000000000} on fail</returns> public async Task <Guid> CreateUserProfile(UserParams userParams) { foreach (var profile in UserProfiles) { /*Profile has already been enrolled */ if (profile.Email == userParams.Email) { return(profile.ProfileGUID); } } var taskComplete = new TaskCompletionSource <Guid>(); Task <CreateProfileResponse> profileTask = null; Guid failGuid = new Guid(); try { profileTask = EnrollmentClient.CreateProfileAsync(EnrollmentLocale); await profileTask; } catch (AggregateException ex) { Console.Error.WriteLine("Error creating user profile with Azure Speaker Recognition endpoint\n" + ex.InnerException.Message); taskComplete.SetResult(failGuid); return(failGuid); } userParams.ProfileGUID = profileTask.Result.ProfileId; /*Attempt to Create user profile in DB and add to list of user profiles */ User registeredUser = DatabaseController.CreateUser(userParams); if (registeredUser == null) { taskComplete.SetResult(failGuid); return(failGuid); } UserProfiles.Add(registeredUser); //Add profile to list of profiles managed by this instance taskComplete.SetResult(profileTask.Result.ProfileId); return(profileTask.Result.ProfileId); }
public EnrollmentClient CreatQXEnrollmentServiceClient() { //Switch from certificate encryption mode to username/password mode. bool isCertificateEncrypted = true; EnrollmentClient webService = null; if (isCertificateEncrypted) { Uri uri = new Uri(System.Configuration.ConfigurationManager.AppSettings["QXEnrollmentServiceCertificateURL"]); var serverCertificate = GetSelerixCertificate(); string certSubject = BindingHelper.GetCertificateCN(serverCertificate); EndpointAddress endpointAddress = new EndpointAddress(uri, EndpointIdentity.CreateDnsIdentity(certSubject), new AddressHeaderCollection()); //var bind = BindingHelper.CreateCustomBinding(true, uri.Scheme == "https"); webService = new BSTIntegrationExample.QXEnrollmentService.EnrollmentClient(new CustomBinding("EncryptCertificateBinding"), endpointAddress); webService.ClientCredentials.ClientCertificate.Certificate = GetVendorCertificate(); webService.ClientCredentials.ServiceCertificate.DefaultCertificate = serverCertificate; } else { Uri uri = new Uri(System.Configuration.ConfigurationManager.AppSettings["QXEnrollmentServiceURL"]); EndpointAddress endpointAddress = new EndpointAddress(uri); var bind = BindingHelper.CreateCustomBinding(false, uri.Scheme == "https"); webService = new BSTIntegrationExample.QXEnrollmentService.EnrollmentClient(bind, endpointAddress); webService.ClientCredentials.UserName.UserName = "******"; webService.ClientCredentials.UserName.Password = "******"; } return(webService); }
private async Task EnrollVoiceSamples() { var taskTuples = new List <Tuple <User, Task <OperationLocation> > >(); List <Task <OperationLocation> > enrollmentTasks = new List <Task <OperationLocation> >(); /*Start enrollment tasks for all user voiceprints. Use enrollmentTaskTuples * to give access to all info associated with each task later. Also add task to * enrollmentTasks for purpose of awaiting each task.*/ for (int i = 0; i < UserProfiles.Count; i++) { await Task.Delay(SPEAKER_RECOGNITION_API_INTERVAL); //Do not exceed max requests per second. if (UserProfiles[i].AudioStream is null) { continue; } var curTask = EnrollmentClient.EnrollAsync(UserProfiles[i].AudioStream, UserProfiles[i].ProfileGUID, true); taskTuples.Add(new Tuple <User, Task <OperationLocation> >(UserProfiles[i], curTask)); enrollmentTasks.Add(curTask); } await Task.WhenAll(enrollmentTasks.ToArray()); //Await all enrollment tasks to complete. /*Confirm that enrollment was successful for all the profiles * associated with the enrollment tasks in enrollmentOps. */ for (int i = 0; i < taskTuples.Count; i++) { Boolean done = false; Boolean reAttempt = true; /*Keep checking this enrollment for successful completion. If fail, * then attempt enrollment again */ do { await Task.Delay(SPEAKER_RECOGNITION_API_INTERVAL); var enrollmentCheck = EnrollmentClient.CheckEnrollmentStatusAsync(taskTuples[i].Item2.Result); await enrollmentCheck; var reqStatus = enrollmentCheck.Result.Status; /*If second request fails, do not try again */ if (reqStatus == Status.Failed && !reAttempt) { done = true; } /*If enrollment has failed, try one more time by first resetting enrollments * for this profile and then performing enrollment task again. */ else if (reqStatus == Status.Failed) { reAttempt = false; //Do not attempt again await EnrollmentClient.ResetEnrollmentsAsync(taskTuples[i].Item1.ProfileGUID); taskTuples[i].Item1.AudioStream.Position = 0; //Ensure audio stream is at beginning. var reEnrollmentTask = EnrollmentClient.EnrollAsync(taskTuples[i].Item1.AudioStream, taskTuples[i].Item1.ProfileGUID, true); /*Replace curElem with an element representing the new enrollment task*/ taskTuples[i] = new Tuple <User, Task <OperationLocation> >(taskTuples[i].Item1, reEnrollmentTask); } /*Check that that result is ready and that profile is enrolled */ else if (!done) { var enrollmentResult = enrollmentCheck.Result.ProcessingResult; if (enrollmentResult != null && enrollmentResult.EnrollmentStatus == EnrollmentStatus.Enrolled) { done = true; } } } while (!done); } }
/// <summary> /// /// </summary> /// <returns></returns> private async Task ConfirmProfiles() { Profile[] existingProfiles = null; try { /*First check that all profiles in the voiceprint objects actually exist*/ existingProfiles = await EnrollmentClient.GetProfilesAsync(); } catch (Exception ex) { existingProfiles = null; Console.Error.WriteLine(">\tFetching profiles failed. Executing fallback to recreate profiles: " + ex.ToString()); } if (existingProfiles != null) { for (int i = 0; i < UserProfiles.Count; i++) { Boolean profileExists = false; int j = 0; while (!profileExists && j < existingProfiles.Length) { /*Check that the profile is in a usable state and that * it matches with the GUID of this voiceprint */ if (UserProfiles[i].AudioStream != null && existingProfiles[j].EnrollmentStatus != EnrollmentStatus.Unknown && UserProfiles[i].ProfileGUID == existingProfiles[j].ProfileId) { profileExists = true; } else { j++; } } /*Create an Azure profile if the profile doesn't actually exist. Also change the * profile ID in the voiceprint object to the new ID*/ if (!profileExists) { await Task.Delay(SPEAKER_RECOGNITION_API_INTERVAL); var profileCreateTask = RefreshAPIProfile(UserProfiles[i]); await profileCreateTask; UserProfiles[i].ProfileGUID = profileCreateTask.Result; } } //End-for } //End-if /*Fallback for when profiles could not be fetched from through Speaker API.*/ else { foreach (var curVoiceprint in UserProfiles) { await Task.Delay(SPEAKER_RECOGNITION_API_INTERVAL); var profileCreateTask = RefreshAPIProfile(curVoiceprint); await profileCreateTask; curVoiceprint.ProfileGUID = profileCreateTask.Result; } } //End else }