What is cognition?
The mental action or process of acquiring knowledge and understanding through thought, experience, and the senses.
What are Cognitive Services?
Cognitive Services are a set of APIs that are designed to democratize artificial intelligence by enabling systems to see, hear, speak, understand, and interpret our needs using natural methods of communication.
What these services generally do is to bring structured semantic data to human knowledge I/O with a degree of confidence.
Vision | Speech | Language | Knowledge | Search |
Computer VisionContent ModeratorEmotionFaceVideo | Bing SpeechCustom Speech ServiceSpeaker Recognition | Bing Spell CheckLanguage UnderstandingLinguistic AnalysisText AnalyticsTranslatorWebLM | AcademicEntity LinkingKnowledge ExplorationQnA MakerRecommendations | Bing AutosuggestBing Image SearchBing News SearchBing Video SearchBing Web Search |
Content Credit
Microsoft / Technical Community Content
Prerequisites
In this tutorial, we will cover Bing Speech API for speech to text (STT) using WPF application in Visual Studio 2017. First, we will start with how to get keys for Bing Speech API.
To register for Bing Speech API, perform the following steps.
- Navigate here.
- Navigate to Speech tab and select "Bing Speech API". Then, hit "Get API Key".
- Select "I agree" and your region, as shown in the image.
- Login with Microsoft account to get your API Key.
- Save your key.
Now, it’s time to create WPF application using Visual Studio 2017. Please follow the steps described below.
- Open Visual Studio 2017. In my case, I am using Community Edition.
- Go to File >> New >> Project.
- Select WPF App (.NET Framework) from, expand Installed > Templates > Visual C# > Windows Classic Desktop > and select WPF App (.NET Framework).
- Then, inside Solution Explorer, right click your project and click "Manage NuGet Packages".
- Inside the NuGet Package Manager window, navigate to Browse tab and search for Microsoft.ProjectOxford.Speech. When found, install both x-86 and x-64 versions.
- In Solution Explorer, check your references. Speech Client will be added automatically.
- Now, almost all the set up is done. Let’s dive into app design. Copy the following code to your MainWindow.xaml file and replace the code between <Grid></Grid>.
- <Grid>
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto"></RowDefinition>
- <RowDefinition Height="Auto"></RowDefinition>
- <RowDefinition Height="Auto"></RowDefinition>
- <RowDefinition Height="Auto"></RowDefinition>
- <RowDefinition Height="*"></RowDefinition>
- </Grid.RowDefinitions>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="Auto"></ColumnDefinition>
- <ColumnDefinition Width="*"></ColumnDefinition>
- <ColumnDefinition Width="3*"></ColumnDefinition>
- </Grid.ColumnDefinitions>
- <TextBlock FontSize="24" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" Margin="4">Bing Speech API Demo</TextBlock>
- <Button x:Name="button" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Click="button_Click"
- Grid.Row="1" Grid.Column="0" Grid.RowSpan="3" Margin="4" Height="75">
- <Button.Content>
- <StackPanel Orientation="Vertical">
- <TextBlock FontSize="16">Speak</TextBlock>
- </StackPanel>
- </Button.Content>
- </Button>
-
- <TextBlock x:Name="status" TextWrapping="Wrap" Text="Not Listening" VerticalAlignment="Center" FontSize="16" Visibility="Collapsed"
- Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" Margin="4"/>
-
- <StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2">
- <TextBlock x:Name="MySpeechResponse" FontSize="20" Margin="4" TextWrapping="Wrap" VerticalAlignment="Center" />
- <TextBlock x:Name="MySpeechResponseConfidence" FontSize="12" Margin="4" TextWrapping="Wrap" VerticalAlignment="Center" />
- </StackPanel>
-
-
- <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="RecordingBar" Visibility="Collapsed"
- Grid.Row="0" Grid.Column="0" Grid.RowSpan="5" Grid.ColumnSpan="3">
-
- <ProgressBar HorizontalAlignment="Left" Width="207" Margin="0,16,0,0" IsIndeterminate="True" />
- </StackPanel>
-
-
-
- </Grid>
- Paste the following code to MainWindow.xaml.cs file.
- namespace BingSpeechApp
- {
- using System.Media;
- using System.Threading;
- using System.Windows.Threading;
- using CognitiveServicesTTS;
- using Microsoft.CognitiveServices.SpeechRecognition;
-
-
-
- public partial class MainWindow : Window
- {
- private MicrophoneRecognitionClient micClient;
- public MainWindow()
- {
- InitializeComponent();
- this.micClient = SpeechRecognitionServiceFactory.CreateMicrophoneClient(
- SpeechRecognitionMode.ShortPhrase,
- "en-US",
- "Your_Key_Here");
- this.micClient.OnMicrophoneStatus += MicClient_OnMicrophoneStatus;
- this.micClient.OnResponseReceived += MicClient_OnResponseReceived;
- }
-
- private void button_Click(object sender, RoutedEventArgs e)
- {
- this.MySpeechResponse.Text = string.Empty;
- this.MySpeechResponseConfidence.Text = string.Empty;
- this.micClient.StartMicAndRecognition();
- }
-
- private void MicClient_OnMicrophoneStatus(object sender, MicrophoneEventArgs e)
- {
- Application.Current.Dispatcher.BeginInvoke(
- DispatcherPriority.Normal,
- new Action(
- () =>
- {
- if (e.Recording)
- {
- this.status.Text = "Listening";
- this.RecordingBar.Visibility = Visibility.Visible;
- }
- else
- {
- this.status.Text = "Not Listening";
- this.RecordingBar.Visibility = Visibility.Collapsed;
- }
- }));
- }
-
- private async void MicClient_OnResponseReceived(object sender, SpeechResponseEventArgs e)
- {
- if (e.PhraseResponse.Results.Length > 0)
- {
- await Application.Current.Dispatcher.BeginInvoke(
- DispatcherPriority.Normal, new Action(() =>
- {
- this.MySpeechResponse.Text = $"'{e.PhraseResponse.Results[0].DisplayText}',";
- this.MySpeechResponseConfidence.Text = $"confidence: { e.PhraseResponse.Results[0].Confidence}";
-
-
- }));
- }
- }
- }
- }
Download Here