0626 fix speech to intent (#146)
* fix speech-to-text * add links to readme for speech services
This commit is contained in:
Родитель
bccba69b1d
Коммит
1e9bd8e0a3
|
@ -17,7 +17,8 @@ namespace MicrosoftSpeechSDKSamples
|
|||
|
||||
public static async Task RecognitionWithLUIS()
|
||||
{
|
||||
// Create a speech factory associated with your LUIS subscription
|
||||
// Create a LUIS endpoint key in the Azure portal, add the key on
|
||||
// the LUIS publish page, and use again here. Do not use starter key!
|
||||
var luisSubscriptionKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
|
||||
var luisRegion = "westus";
|
||||
var luisAppId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
|
||||
|
|
|
@ -14,14 +14,7 @@ namespace MicrosoftSpeechSDKSamples
|
|||
static void Main(string[] args)
|
||||
{
|
||||
|
||||
Console.WriteLine("1. Speech recognition with microphone input.");
|
||||
Console.WriteLine("2. Speech recognition in the specified language.");
|
||||
Console.WriteLine("3. Speech recognition with file input.");
|
||||
Console.WriteLine("4. Speech recognition using customized model.");
|
||||
Console.WriteLine("5. Speech continuous recognition using events.");
|
||||
Console.WriteLine("6. Translation with microphone input.");
|
||||
Console.WriteLine("7. Translation with file input.");
|
||||
Console.WriteLine("8. Speech recognition of LUIS intent.");
|
||||
Console.WriteLine("1. Speech recognition of LUIS intent.");
|
||||
Console.WriteLine("0. Stop.");
|
||||
|
||||
Console.Write("Your choice: ");
|
||||
|
@ -34,27 +27,6 @@ namespace MicrosoftSpeechSDKSamples
|
|||
switch (x.Key)
|
||||
{
|
||||
case ConsoleKey.D1:
|
||||
SpeechRecognitionSamples.RecognitionWithMicrophoneAsync().Wait();
|
||||
break;
|
||||
case ConsoleKey.D2:
|
||||
SpeechRecognitionSamples.RecognitionWithLanguageAsync().Wait();
|
||||
break;
|
||||
case ConsoleKey.D3:
|
||||
SpeechRecognitionSamples.RecognitionWithFileAsync().Wait();
|
||||
break;
|
||||
case ConsoleKey.D4:
|
||||
SpeechRecognitionSamples.RecognitionUsingCustomizedModelAsync().Wait();
|
||||
break;
|
||||
case ConsoleKey.D5:
|
||||
SpeechRecognitionSamples.ContinuousRecognitionAsync().Wait();
|
||||
break;
|
||||
case ConsoleKey.D6:
|
||||
TranslationSamples.TranslationWithMicrophoneAsync().Wait();
|
||||
break;
|
||||
case ConsoleKey.D7:
|
||||
TranslationSamples.TranslationWithFileAsync().Wait();
|
||||
break;
|
||||
case ConsoleKey.D8:
|
||||
Console.WriteLine("LUIS...");
|
||||
LuisSamples.RecognitionWithLUIS().Wait();
|
||||
break;
|
||||
|
|
|
@ -72,8 +72,6 @@
|
|||
<Compile Include="LUIS_samples.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="speech_recognition_samples.cs" />
|
||||
<Compile Include="translation_samples.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
|
|
|
@ -1,189 +0,0 @@
|
|||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
|
||||
//
|
||||
|
||||
// <toplevel>
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.CognitiveServices.Speech;
|
||||
|
||||
// </toplevel>
|
||||
|
||||
namespace MicrosoftSpeechSDKSamples
|
||||
{
|
||||
public class SpeechRecognitionSamples
|
||||
{
|
||||
// Speech recognition from microphone.
|
||||
public static async Task RecognitionWithMicrophoneAsync()
|
||||
{
|
||||
// <recognitionWithMicrophone>
|
||||
// Creates an instance of a speech factory with specified
|
||||
// subscription key and service region. Replace with your own subscription key
|
||||
// and service region (e.g., "westus").
|
||||
var factory = SpeechFactory.FromSubscription("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "westus");
|
||||
|
||||
// Creates a speech recognizer using microphone as audio input. The default language is "en-us".
|
||||
using (var recognizer = factory.CreateSpeechRecognizer())
|
||||
{
|
||||
// Starts recognizing.
|
||||
Console.WriteLine("Say something...");
|
||||
|
||||
// Starts recognition. It returns when the first utterance has been recognized.
|
||||
var result = await recognizer.RecognizeAsync().ConfigureAwait(false);
|
||||
|
||||
// Checks result.
|
||||
if (result.RecognitionStatus != RecognitionStatus.Recognized)
|
||||
{
|
||||
Console.WriteLine($"There was an error. Status:{result.RecognitionStatus.ToString()}, Reason:{result.RecognitionFailureReason}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"We recognized: {result.RecognizedText}");
|
||||
}
|
||||
}
|
||||
// </recognitionWithMicrophone>
|
||||
}
|
||||
|
||||
// Speech recognition in the specified spoken language.
|
||||
public static async Task RecognitionWithLanguageAsync()
|
||||
{
|
||||
// <recognitionWithLanguage>
|
||||
// Creates an instance of a speech factory with specified
|
||||
// subscription key and service region. Replace with your own subscription key
|
||||
// and service region (e.g., "westus").
|
||||
var factory = SpeechFactory.FromSubscription("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "westus");
|
||||
|
||||
// Creates a speech recognizer for the specified language, using microphone as audio input.
|
||||
var lang = "en-us";
|
||||
using (var recognizer = factory.CreateSpeechRecognizer(lang))
|
||||
{
|
||||
// Starts recognizing.
|
||||
Console.WriteLine($"Say something in {lang} ...");
|
||||
|
||||
// Starts recognition. It returns when the first utterance has been recognized.
|
||||
var result = await recognizer.RecognizeAsync().ConfigureAwait(false);
|
||||
|
||||
// Checks result.
|
||||
if (result.RecognitionStatus != RecognitionStatus.Recognized)
|
||||
{
|
||||
Console.WriteLine($"There was an error. Status:{result.RecognitionStatus.ToString()}, Reason:{result.RecognitionFailureReason}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"We recognized: {result.RecognizedText}");
|
||||
}
|
||||
}
|
||||
// </recognitionWithLanguage>
|
||||
}
|
||||
// Speech recognition from file.
|
||||
public static async Task RecognitionWithFileAsync()
|
||||
{
|
||||
// <recognitionFromFile>
|
||||
// Creates an instance of a speech factory with specified
|
||||
// subscription key and service region. Replace with your own subscription key
|
||||
// and service region (e.g., "westus").
|
||||
var factory = SpeechFactory.FromSubscription("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "westus");
|
||||
|
||||
// Creates a speech recognizer using file as audio input.
|
||||
// Replace with your own audio file name.
|
||||
using (var recognizer = factory.CreateSpeechRecognizerWithFileInput(@"YourAudioFile.wav"))
|
||||
{
|
||||
// Starts recognition. It returns when the first utterance is recognized.
|
||||
var result = await recognizer.RecognizeAsync().ConfigureAwait(false);
|
||||
|
||||
// Checks result.
|
||||
if (result.RecognitionStatus != RecognitionStatus.Recognized)
|
||||
{
|
||||
Console.WriteLine($"There was an error. Status:{result.RecognitionStatus.ToString()}, Reason:{result.RecognitionFailureReason}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"We recognized: {result.RecognizedText}");
|
||||
}
|
||||
}
|
||||
// </recognitionFromFile>
|
||||
}
|
||||
|
||||
// <recognitionCustomized>
|
||||
// Speech recognition using a customized model.
|
||||
public static async Task RecognitionUsingCustomizedModelAsync()
|
||||
{
|
||||
// Creates an instance of a speech factory with specified
|
||||
// subscription key and service region. Replace with your own subscription key
|
||||
// and service region (e.g., "westus").
|
||||
var factory = SpeechFactory.FromSubscription("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "westus");
|
||||
|
||||
// Creates a speech recognizer using microphone as audio input.
|
||||
using (var recognizer = factory.CreateSpeechRecognizer())
|
||||
{
|
||||
// Replace with the CRIS deployment id of your customized model.
|
||||
recognizer.DeploymentId = "YourDeploymentId";
|
||||
|
||||
Console.WriteLine("Say something...");
|
||||
|
||||
// Starts recognition. It returns when the first utterance has been recognized.
|
||||
var result = await recognizer.RecognizeAsync().ConfigureAwait(false);
|
||||
|
||||
// Checks results.
|
||||
if (result.RecognitionStatus != RecognitionStatus.Recognized)
|
||||
{
|
||||
Console.WriteLine($"There was an error. Status:{result.RecognitionStatus.ToString()}, Reason:{result.RecognitionFailureReason}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"We recognized: {result.RecognizedText}");
|
||||
}
|
||||
}
|
||||
}
|
||||
// </recognitionCustomized>
|
||||
|
||||
// <recognitionContinuous>
|
||||
// Speech recognition with events
|
||||
public static async Task ContinuousRecognitionAsync()
|
||||
{
|
||||
// Creates an instance of a speech factory with specified
|
||||
// subscription key and service region. Replace with your own subscription key
|
||||
// and service region (e.g., "westus").
|
||||
var factory = SpeechFactory.FromSubscription("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "westus");
|
||||
|
||||
// Creates a speech recognizer using microphone as audio input.
|
||||
using (var recognizer = factory.CreateSpeechRecognizer())
|
||||
{
|
||||
// Subscribes to events.
|
||||
recognizer.IntermediateResultReceived += (s, e) => {
|
||||
Console.WriteLine($"\n Partial result: {e.Result.RecognizedText}.");
|
||||
};
|
||||
|
||||
recognizer.FinalResultReceived += (s, e) => {
|
||||
if (e.Result.RecognitionStatus == RecognitionStatus.Recognized)
|
||||
{
|
||||
Console.WriteLine($"\n Final result: Status: {e.Result.RecognitionStatus.ToString()}, Text: {e.Result.RecognizedText}.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"\n Final result: Status: {e.Result.RecognitionStatus.ToString()}, FailureReason: {e.Result.RecognitionFailureReason}.");
|
||||
}
|
||||
};
|
||||
|
||||
recognizer.RecognitionErrorRaised += (s, e) => {
|
||||
Console.WriteLine($"\n An error occurred. Status: {e.Status.ToString()}, FailureReason: {e.FailureReason}");
|
||||
};
|
||||
|
||||
recognizer.OnSessionEvent += (s, e) => {
|
||||
Console.WriteLine($"\n Session event. Event: {e.EventType.ToString()}.");
|
||||
};
|
||||
|
||||
// Starts continuos recognition. Uses StopContinuousRecognitionAsync() to stop recognition.
|
||||
Console.WriteLine("Say something...");
|
||||
await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);
|
||||
|
||||
Console.WriteLine("Press any key to stop");
|
||||
Console.ReadKey();
|
||||
|
||||
await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
// </recognitionContinuous>
|
||||
}
|
||||
}
|
|
@ -1,206 +0,0 @@
|
|||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
|
||||
//
|
||||
|
||||
// <toplevel>
|
||||
using System;
|
||||
using System.Media;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.CognitiveServices.Speech;
|
||||
using Microsoft.CognitiveServices.Speech.Translation;
|
||||
// </toplevel>
|
||||
|
||||
namespace MicrosoftSpeechSDKSamples
|
||||
{
|
||||
public class TranslationSamples
|
||||
{
|
||||
// Translation from microphone.
|
||||
// <TranslationWithMicrophoneAsync>
|
||||
public static async Task TranslationWithMicrophoneAsync()
|
||||
{
|
||||
// Creates an instance of a speech factory with specified
|
||||
// subscription key and service region. Replace with your own subscription key
|
||||
// and service region (e.g., "westus").
|
||||
var factory = SpeechFactory.FromSubscription("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "westus");
|
||||
|
||||
// Sets source and target languages
|
||||
string fromLanguage = "en-US";
|
||||
List<string> toLanguages = new List<string>() { "de" };
|
||||
|
||||
// Sets voice name of synthesis output.
|
||||
const string GermanVoice = "de-DE-Hedda";
|
||||
|
||||
// Creates a translation recognizer using microphone as audio input, and requires voice output.
|
||||
using (var recognizer = factory.CreateTranslationRecognizer(fromLanguage, toLanguages, GermanVoice))
|
||||
{
|
||||
// Subscribes to events.
|
||||
recognizer.IntermediateResultReceived += (s, e) => {
|
||||
Console.WriteLine($"\nPartial result: recognized in {fromLanguage}: {e.Result.RecognizedText}.");
|
||||
if (e.Result.TranslationStatus == TranslationStatus.Success)
|
||||
{
|
||||
foreach (var element in e.Result.Translations)
|
||||
{
|
||||
Console.WriteLine($" Translated into {element.Key}: {element.Value}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($" Translation failed. Status: {e.Result.TranslationStatus.ToString()}, FailureReason: {e.Result.FailureReason}");
|
||||
}
|
||||
};
|
||||
|
||||
recognizer.FinalResultReceived += (s, e) => {
|
||||
if (e.Result.RecognitionStatus != RecognitionStatus.Recognized)
|
||||
{
|
||||
Console.WriteLine($"\nFinal result: Status: {e.Result.RecognitionStatus.ToString()}, FailureReason: {e.Result.RecognitionFailureReason}.");
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"\nFinal result: Status: {e.Result.RecognitionStatus.ToString()}, recognized text in {fromLanguage}: {e.Result.RecognizedText}.");
|
||||
if (e.Result.TranslationStatus == TranslationStatus.Success)
|
||||
{
|
||||
foreach (var element in e.Result.Translations)
|
||||
{
|
||||
Console.WriteLine($" Translated into {element.Key}: {element.Value}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($" Translation failed. Status: {e.Result.TranslationStatus.ToString()}, FailureReason: {e.Result.FailureReason}");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
recognizer.SynthesisResultReceived += (s, e) =>
|
||||
{
|
||||
if (e.Result.Status == SynthesisStatus.Success)
|
||||
{
|
||||
Console.WriteLine($"Synthesis result received. Size of audio data: {e.Result.Audio.Length}");
|
||||
using (var m = new MemoryStream(e.Result.Audio))
|
||||
{
|
||||
SoundPlayer simpleSound = new SoundPlayer(m);
|
||||
simpleSound.PlaySync();
|
||||
}
|
||||
}
|
||||
else if (e.Result.Status == SynthesisStatus.SynthesisEnd)
|
||||
{
|
||||
Console.WriteLine($"Synthesis result: end of synthesis result.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Synthesis error. Status: {e.Result.Status.ToString()}, Failure reason: {e.Result.FailureReason}");
|
||||
}
|
||||
};
|
||||
|
||||
recognizer.RecognitionErrorRaised += (s, e) => {
|
||||
Console.WriteLine($"\nAn error occurred. Status: {e.Status.ToString()}");
|
||||
};
|
||||
|
||||
recognizer.OnSessionEvent += (s, e) => {
|
||||
Console.WriteLine($"\nSession event. Event: {e.EventType.ToString()}.");
|
||||
};
|
||||
|
||||
// Starts continuous recognition. Uses StopContinuousRecognitionAsync() to stop recognition.
|
||||
Console.WriteLine("Say something...");
|
||||
await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);
|
||||
|
||||
do
|
||||
{
|
||||
Console.WriteLine("Press Enter to stop");
|
||||
} while (Console.ReadKey().Key != ConsoleKey.Enter);
|
||||
|
||||
await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
// </TranslationWithMicrophoneAsync>
|
||||
|
||||
// Translation using file input.
|
||||
// <TranslationWithFileAsync>
|
||||
public static async Task TranslationWithFileAsync()
|
||||
{
|
||||
// Creates an instance of a speech factory with specified
|
||||
// subscription key and service region. Replace with your own subscription key
|
||||
// and service region (e.g., "westus").
|
||||
var factory = SpeechFactory.FromSubscription("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "westus");
|
||||
|
||||
// Sets source and target languages
|
||||
string fromLanguage = "en-US";
|
||||
List<string> toLanguages = new List<string>() { "de", "fr" };
|
||||
|
||||
var translationEndTaskCompletionSource = new TaskCompletionSource<int>();
|
||||
|
||||
// Creates a translation recognizer using file as audio input.
|
||||
// Replace with your own audio file name.
|
||||
using (var recognizer = factory.CreateTranslationRecognizerWithFileInput(@"YourAudioFile.wav", fromLanguage, toLanguages))
|
||||
{
|
||||
// Subscribes to events.
|
||||
recognizer.IntermediateResultReceived += (s, e) => {
|
||||
Console.WriteLine($"\nPartial result: recognized in {fromLanguage}: {e.Result.RecognizedText}.");
|
||||
if (e.Result.TranslationStatus == TranslationStatus.Success)
|
||||
{
|
||||
foreach (var element in e.Result.Translations)
|
||||
{
|
||||
Console.WriteLine($" Translated into {element.Key}: {element.Value}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($" Translation failed. Status: {e.Result.TranslationStatus.ToString()}, FailureReason: {e.Result.FailureReason}");
|
||||
}
|
||||
};
|
||||
|
||||
recognizer.FinalResultReceived += (s, e) => {
|
||||
if (e.Result.RecognitionStatus != RecognitionStatus.Recognized)
|
||||
{
|
||||
Console.WriteLine($"\nFinal result: Status: {e.Result.RecognitionStatus.ToString()}, FailureReason: {e.Result.RecognitionFailureReason}.");
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"\nFinal result: Status: {e.Result.RecognitionStatus.ToString()}, recognized text in {fromLanguage}: {e.Result.RecognizedText}.");
|
||||
if (e.Result.TranslationStatus == TranslationStatus.Success)
|
||||
{
|
||||
foreach (var element in e.Result.Translations)
|
||||
{
|
||||
Console.WriteLine($" Translated into {element.Key}: {element.Value}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($" Translation failed. Status: {e.Result.TranslationStatus.ToString()}, FailureReason: {e.Result.FailureReason}");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
recognizer.RecognitionErrorRaised += (s, e) => {
|
||||
Console.WriteLine($"\nAn error occurred. Status: {e.Status.ToString()}");
|
||||
};
|
||||
|
||||
recognizer.OnSpeechDetectedEvent += (s, e) => {
|
||||
Console.WriteLine($"\nSpeech detected event. Event: {e.EventType.ToString()}.");
|
||||
// Stops translation when speech end is detected.
|
||||
if (e.EventType == RecognitionEventType.SpeechEndDetectedEvent)
|
||||
{
|
||||
Console.WriteLine($"\nStop translation.");
|
||||
translationEndTaskCompletionSource.TrySetResult(0);
|
||||
}
|
||||
};
|
||||
|
||||
// Starts continuous recognition. Uses StopContinuousRecognitionAsync() to stop recognition.
|
||||
Console.WriteLine("Start translation...");
|
||||
await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);
|
||||
|
||||
// Waits for completion.
|
||||
await translationEndTaskCompletionSource.Task.ConfigureAwait(false);
|
||||
|
||||
// Stops translation.
|
||||
await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
// </TranslationWithFileAsync>
|
||||
}
|
||||
}
|
|
@ -5,5 +5,6 @@ Before using this code, import the [Human Resources app](../../quickstarts/Human
|
|||
|
||||
The key piece of LUIS code is found at ./csharp/LUIS.samples.cs.
|
||||
|
||||
Instead of using the Speech subscription key, you need to create the speech factory with the LUIS endpoint key.
|
||||
Instead of using the Speech subscription key, you need to create the speech factory with the LUIS endpoint key. Do not use the free starter key.
|
||||
|
||||
If you are interested in more information about the Speech services, read through the [documentation](https://docs.microsoft.com/en-us/azure/cognitive-services/speech-service/) and download the [SDK](https://github.com/Azure-Samples/cognitive-services-speech-sdk).
|
Загрузка…
Ссылка в новой задаче