Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microsoft CognitiveServices Speech class SpeechRecognizer, cannot gather a resulting text #55032

Closed
dubbySwords opened this issue May 18, 2020 · 3 comments

Comments

@dubbySwords
Copy link

I have taken the exampIe from Microsoft pages. It does not work.

I made slight modifications to stor the dataa. I cannot get the the text from the wav file using SpeechRecognizer class.

When I debug the code under I see that when I delay I get text but it eventually crashes.

Is the code incorrect?

What am I missing in order to wait on all the results and collect them in totalText which is a field variable.

using (var audioInput = AudioConfig.FromWavFileInput(wavFile))
{
	using (var recognizer = new SpeechRecognizer(configuration, audioInput))
	{
		recognizer.Recognized += (s, e) =>
		{
			if (e.Result.Reason == ResultReason.RecognizedSpeech)
			{
				System.Diagnostics.Debug.WriteLine($"RECOGNIZED: Text={e.Result.Text}");
				totalText += e.Result.Text;
			}
			else if (e.Result.Reason == ResultReason.NoMatch)
			{
				System.Diagnostics.Debug.WriteLine($"NOMATCH: Speech could not be recognized.");
			}
		};

		recognizer.Canceled += (s, e) =>
		{
			System.Diagnostics.Debug.WriteLine($"CANCELED: Reason={e.Reason}");

			if (e.Reason == CancellationReason.Error)
			{
				System.Diagnostics.Debug.WriteLine($"CANCELED: ErrorCode={e.ErrorCode}");
				System.Diagnostics.Debug.WriteLine($"CANCELED: ErrorDetails={e.ErrorDetails}");
				System.Diagnostics.Debug.WriteLine($"CANCELED: Did you update the subscription info?");
			}

			stopRecognition.TrySetResult(0);
		};

		recognizer.SessionStarted += (s, e) =>
		{
			System.Diagnostics.Debug.WriteLine("\n    Session started event.");
		};

		recognizer.SessionStopped += (s, e) =>
		{
			System.Diagnostics.Debug.WriteLine("\n    Session stopped event.");
			System.Diagnostics.Debug.WriteLine("\nStop recognition.");
			stopRecognition.TrySetResult(0);
		};

		recognizer.SpeechEndDetected += (s, e) =>
		{
			System.Diagnostics.Debug.WriteLine($"SpeechEndDetected: Did you update the subscription info?");
			SaveFile(totalText);
			stopRecognition.TrySetResult(0);
		};

		// Starts continuous recognition. Uses StopContinuousRecognitionAsync() to stop recognition.
		await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);

		// Waits for completion.
		// Use Task.WaitAny to keep the task rooted.
		Task.WaitAny(new[] { stopRecognition.Task });

		// Stops recognition.
		await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
		if (totalText != string.Empty)
		{
			SaveFile(totalText);
		}
	}
}

One of the final results I got was:
The program '[9312] testhost.exe' has exited with code 0 (0x0).


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@SumanthMarigowda-MSFT-zz
Copy link
Contributor

@dubbySwords Thanks for your question. We are checking on this and will respond to you soon.

@ram-msft
Copy link
Contributor

ram-msft commented May 22, 2020

@dubbySwords Please follow the below sanpshots for the text. You can save the final recognized text to the file in the Speech Recognized event instead of writing to the console. you can use the saveFile in Recognized event.
We are able to execute the Speech Recognizer Sample.
image

image

@ram-msft
Copy link
Contributor

@dubbySwords We will now proceed to close this thread. If there are further questions regarding this matter, please tag me in your reply. We will gladly continue the discussion and we will reopen the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants