private async void _enrollBtn_Click(object sender, RoutedEventArgs e) { MainWindow window = (MainWindow)Application.Current.MainWindow; try { if (_selectedFile == "") { throw new Exception("No File Selected."); } window.Log("Enrolling Speaker..."); Profile[] selectedProfiles = SpeakersListPage.SpeakersList.GetSelectedProfiles(); OperationLocation processPollingLocation; using (Stream audioStream = File.OpenRead(_selectedFile)) { _selectedFile = ""; processPollingLocation = await _serviceClient.EnrollAsync(audioStream, selectedProfiles[0].ProfileId, ((sender as Button) == _enrollShortAudioBtn)); } EnrollmentOperation enrollmentResult; int numOfRetries = 10; TimeSpan timeBetweenRetries = TimeSpan.FromSeconds(5.0); while (numOfRetries > 0) { await Task.Delay(timeBetweenRetries); enrollmentResult = await _serviceClient.CheckEnrollmentStatusAsync(processPollingLocation); if (enrollmentResult.Status == Status.Succeeded) { break; } else if (enrollmentResult.Status == Status.Failed) { throw new EnrollmentException(enrollmentResult.Message); } numOfRetries--; } if (numOfRetries <= 0) { throw new EnrollmentException("Enrollment operation timeout."); } window.Log("Enrollment Done."); await SpeakersListPage.SpeakersList.UpdateAllSpeakersAsync(); } catch (EnrollmentException ex) { window.Log("Enrollment Error: " + ex.Message); } catch (Exception ex) { window.Log("Error: " + ex.Message); } }
private void EnrollSpeaker(Stream stream) { // Reset pointer stream.Seek(0, SeekOrigin.Begin); SpeakerIdentificationServiceClient speakerIDClient = new SpeakerIdentificationServiceClient("c6b005dcf13e45b6a91485d38763277b"); //Creating Speaker Profile... CreateProfileResponse creationResponse = speakerIDClient.CreateProfileAsync("en-US").Result; //Speaker Profile Created. //Retrieving The Created Profile... Profile profile = speakerIDClient.GetProfileAsync(creationResponse.ProfileId).Result; //Speaker Profile Retrieved." //Enrolling Speaker OperationLocation processPollingLocation = speakerIDClient.EnrollAsync(stream, profile.ProfileId, false).Result; EnrollmentOperation enrollmentResult; int numOfRetries = 10; TimeSpan timeBetweenRetries = TimeSpan.FromSeconds(5.0); while (numOfRetries > 0) { Task.Delay(timeBetweenRetries); enrollmentResult = speakerIDClient.CheckEnrollmentStatusAsync(processPollingLocation).Result; if (enrollmentResult.Status == Status.Succeeded) { break; } else if (enrollmentResult.Status == Status.Failed) { throw new EnrollmentException(enrollmentResult.Message); } numOfRetries--; } if (numOfRetries <= 0) { throw new EnrollmentException("Enrollment operation timeout."); } //Enrollment Done. // Store profile in memory cache ObjectCache memCache = MemoryCache.Default; var profiles = memCache.Get("SpeakerProfiles") != null?memCache.Get("SpeakerProfiles") as List <Profile> : new List <Profile>(); memCache.Remove("SpeakerProfiles"); memCache.Add("SpeakerProfiles", profiles, DateTimeOffset.UtcNow.AddHours(2)); }
public async void voicetoprofile(byte[] data, string filename) { try { SpeakerIdentificationServiceClient _serviceClient = new SpeakerIdentificationServiceClient("xxxxxxxxxxxxxxxxxxxxxxx"); CreateProfileResponse creationResponse = await _serviceClient.CreateProfileAsync(name.Text.ToString()); Profile profile = await _serviceClient.GetProfileAsync(creationResponse.ProfileId); //SpeakersListPage.SpeakersList.AddSpeaker(profile); OperationLocation processPollingLocation; using (Stream audioStream = new MemoryStream(data)) { //_selectedFile = ""; processPollingLocation = await _serviceClient.EnrollAsync(audioStream, profile.ProfileId); } EnrollmentOperation enrollmentResult; int numOfRetries = 10; TimeSpan timeBetweenRetries = TimeSpan.FromSeconds(5.0); while (numOfRetries > 0) { await Task.Delay(timeBetweenRetries); enrollmentResult = await _serviceClient.CheckEnrollmentStatusAsync(processPollingLocation); if (enrollmentResult.Status == Status.Succeeded) { break; } else if (enrollmentResult.Status == Status.Failed) { throw new EnrollmentException(enrollmentResult.Message); } numOfRetries--; } if (numOfRetries <= 0) { throw new EnrollmentException("Enrollment operation timeout."); } } catch (Exception ex) { throw ex; } }
/// <summary> /// Takes the uploaded attachment and uses that to enroll the current profile. /// </summary> /// <param name="attachmentContentUrl"></param> /// <param name="profileId"></param> /// <param name="dc"></param> /// <returns></returns> private async Task EnrollWavFile(string attachmentContentUrl, Guid profileId, DialogContext dc) { try { var stream = await _httpClient.GetStreamAsync(attachmentContentUrl); using (stream) { var processPollingLocation = await _client.EnrollAsync(stream, profileId); int numberOfRetries = 10; var timeBetweenRetries = TimeSpan.FromSeconds(5.0); while (numberOfRetries > 0) { await Task.Delay(timeBetweenRetries); var enrollmentResult = await _client.CheckEnrollmentStatusAsync(processPollingLocation); if (enrollmentResult.Status == Status.Succeeded) { break; } if (enrollmentResult.Status == Status.Failed) { throw new EnrollmentException(enrollmentResult.Message); } numberOfRetries--; } if (numberOfRetries <= 0) { throw new EnrollmentException("Enrollment operation timeout."); } } } catch (Exception ex) { await dc.Context.SendActivity($"Enrollment failed with error '{ex.Message}'."); } }
/// <summary> /// Confirms that enrollment was successful for all the profiles /// associated with the enrollment tasks in enrollmentOps. /// </summary> /// <returns></returns> private static async Task ConfirmEnrollment(List <Task <OperationLocation> > enrollmentTasks, SpeakerIdentificationServiceClient enrollmentClient) { foreach (var curTask in enrollmentTasks) { Boolean done = false; do { await Task.Delay(SPEAKER_RECOGNITION_API_INTERVAL); var enrollmentCheck = enrollmentClient.CheckEnrollmentStatusAsync(curTask.Result); await enrollmentCheck; /*Check that this profile is enrolled */ if (enrollmentCheck.Result.ProcessingResult != null && enrollmentCheck.Result.ProcessingResult.EnrollmentStatus == EnrollmentStatus.Enrolled) { done = true; } } while (!done); } }
async Task finishEnrollment() { if (btnRecordEnroll.IsEnabled == false) { return; // if user clicks and then comes timer event } btnRecordEnroll.Content = "Start record enrollment"; btnRecordEnroll.IsEnabled = false; await CaptureMedia.StopRecordAsync(); Stream str = AudioStream.AsStream(); str.Seek(0, SeekOrigin.Begin); _speakerId = Guid.Parse((lbProfiles.SelectedItem as ListBoxItem).Content.ToString()); OperationLocation processPollingLocation; try { processPollingLocation = await _serviceClient.EnrollAsync(str, _speakerId); } catch (EnrollmentException vx) { txtInfo.Text = vx.Message; CleanAfter(); return; } catch (Exception vx) { txtInfo.Text = vx.Message; CleanAfter(); return; } EnrollmentOperation enrollmentResult = null; int numOfRetries = 10; TimeSpan timeBetweenRetries = TimeSpan.FromSeconds(5.0); while (numOfRetries > 0) { await Task.Delay(timeBetweenRetries); enrollmentResult = await _serviceClient.CheckEnrollmentStatusAsync(processPollingLocation); if (enrollmentResult.Status == Status.Succeeded) { break; } else if (enrollmentResult.Status == Status.Failed) { txtInfo.Text = enrollmentResult.Message; CleanAfter(); return; } numOfRetries--; } if (numOfRetries <= 0) { txtInfo.Text = "Identification operation timeout"; } else { txtInfo.Text = "Enrollment done. " + enrollmentResult.Status + Environment.NewLine + " Remaining Speech Time " + enrollmentResult.ProcessingResult.RemainingEnrollmentSpeechTime; } CleanAfter(); }
//This functionality will used the passed audio stream to enroll a speaker with the passed GUID //INPUT: enrollment audio sample, Speaker ID, Sample Length //OUTPUT: FunctionResult public async Task <functionResult> enrollSpeaker(Stream ms, Guid speakerID, double sampleLength) { functionResult result = new functionResult(); //Ensure the stream isnot null if (ms != null) { //Ensure we have a sample lenght of more than 5 seconds to enroll if (sampleLength > 5) { try { { OperationLocation processPollingLocation; using (Stream audioStream = ms) { audioStream.Position = 0; processPollingLocation = await _serviceClient.EnrollAsync(audioStream, speakerID, true); } EnrollmentOperation enrollmentResult; int numOfRetries = 10; TimeSpan timeBetweenRetries = TimeSpan.FromSeconds(5.0); while (numOfRetries > 0) { await Task.Delay(timeBetweenRetries); enrollmentResult = await _serviceClient.CheckEnrollmentStatusAsync(processPollingLocation); if (enrollmentResult.Status == Status.Succeeded) { break; } else if (enrollmentResult.Status == Status.Failed) { throw new EnrollmentException(enrollmentResult.Message); } numOfRetries--; } if (numOfRetries <= 0) { result.Result = false; result.Message = ("Enrollment Error: Enrollment operation timeout."); } result.Result = true; result.Message = ("User has been enrolled successfully"); } } catch (EnrollmentException ex) { result.Result = false; result.Message = ("Enrollment Error: " + ex.Message); } catch (Exception ex) { result.Result = false; result.Message = ("Error: " + ex.Message); } } else { result.Result = false; result.Message = ("Enroll: Audio sample need to be > 5 seconds"); } } else { result.Result = false; result.Message = ("Enroll: No valid sample recorded"); } return(result); }
private async void stopRecordBtn_Click(object sender, RoutedEventArgs e) { try { //Guid aakashId = Guid.Parse("b6dc2382-1657-4ffe-9092-f98a28509573"); Title = string.Format("Stopping....."); SpeakerIdentificationServiceClient _serviceClient = new SpeakerIdentificationServiceClient(speakerAPISubscriptionKey); if (_waveIn != null) { _waveIn.StopRecording(); recordBtn.IsEnabled = false; stopRecordBtn.IsEnabled = false; Title = String.Format("Recording Stopped"); speechSynthesizer.SpeakAsync("Wait we are registering your voice."); await Task.Delay(5000); Console.WriteLine("Speaker Id : {0}", creationResponse.ProfileId); Title = String.Format("Enrolling...."); try { if (_selectedFile == "") { throw new Exception("No File Selected."); } Title = String.Format("Enrolling Speaker..."); OperationLocation processPollingLocation; using (Stream audioStream = File.OpenRead(_selectedFile)) { processPollingLocation = await _serviceClient.EnrollAsync(audioStream, creationResponse.ProfileId, true); //processPollingLocation = await _serviceClient.EnrollAsync(audioStream, aakashId, true); } EnrollmentOperation enrollmentResult; int numOfRetries = 10; TimeSpan timeBetweenRetries = TimeSpan.FromSeconds(5.0); while (numOfRetries > 0) { await Task.Delay(timeBetweenRetries); enrollmentResult = await _serviceClient.CheckEnrollmentStatusAsync(processPollingLocation); if (enrollmentResult.Status == Microsoft.ProjectOxford.SpeakerRecognition.Contract.Identification.Status.Succeeded) { break; } else if (enrollmentResult.Status == Microsoft.ProjectOxford.SpeakerRecognition.Contract.Identification.Status.Failed) { throw new EnrollmentException(enrollmentResult.Message); } numOfRetries--; } if (numOfRetries <= 0) { throw new EnrollmentException("Enrollment operation timeout."); } Console.WriteLine("Guid is : {0}", creationResponse.ProfileId); voiceid = creationResponse.ProfileId.ToString(); conn.Open(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandType = System.Data.CommandType.Text; cmd.CommandText = "update AuthenticationDetails set VoiceId = '" + voiceid + "' where AccountNo = '" + accountNo + "'"; cmd.ExecuteNonQuery(); conn.Close(); MessageBox.Show("Record Updated Successfully."); enrollVoiceList.Add(Guid.Parse(creationResponse.ProfileId.ToString()), userName); speechSynthesizer.SpeakAsync("Thank you. You have successfully registered your voice."); speechSynthesizer.SpeakAsync("From now onward you can use your voice to make your transactions secure."); faceIdentifyBtn.IsEnabled = true; Title = String.Format("Enrollment Done"); Console.WriteLine("Enrollment Done"); //window.Log("Enrollment Done."); await UpdateAllSpeakersAsync(); GC.Collect(); } catch (EnrollmentException ex) { //window.Log("Enrollment Error: " + ex.Message); //File.Delete(_selectedFile); recordBtn.IsEnabled = true; stopRecordBtn.IsEnabled = false; speechSynthesizer.SpeakAsync("Sorry, Try again."); Title = String.Format("Enrollment Error: " + ex.Message); GC.Collect(); } catch (Exception ex) { //File.Delete(_selectedFile); recordBtn.IsEnabled = true; stopRecordBtn.IsEnabled = false; //window.Log("Error: " + ex.Message); Title = String.Format("Error: " + ex.Message); GC.Collect(); } } GC.Collect(); } catch (Exception ex) { Console.WriteLine(ex.Message); GC.Collect(); } }