How to use System.Speech on non-English Win Vista Business - c#

I´d like to try Speech recognition to controlling program. I wrote test program in C# and when I´m debugging this, an error occurred every time -
System.Runtime.InteropServices.COMException (0x80004005): Calling part of COM return error HRESULT E_FAIL.*
in System.Speech.Recognition.RecognizerBase.Initialize(SapiRecognizer recognizer, Boolean inproc)
in System.Speech.Recognition.SpeechRecognitionEngine.get_RecoBase()
in System.Speech.Recognition.SpeechRecognitionEngine.LoadGrammar(Grammar grammar)
It looks the error is caused by engine.LoadGrammar(new DictationGrammar());
On my notebook I installed CZECH OS Vista, and maybe this is the problem that speech recognition language is not the same as OS language.
Is there a way how to developing with system.speech in non english OS, or am I wrong in some step? There is no problem in language, I´d like use english for speech recognizing, but, I cannot get english Vista or MUI language pack.
Full code is below.
Thanks a lot!
using System;
using System.Windows;
using System.Speech.Recognition;
namespace rozpoznani_reci_WPF
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
SpeechRecognitionEngine engine = new SpeechRecognitionEngine();
try
{
engine.LoadGrammar(new DictationGrammar());
engine.SetInputToDefaultAudioDevice();
engine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
}
catch(Exception e)
{
//MessageBox.Show(e.ToString());
textBox1.Text = e.ToString();
}
}
void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
if (e.Result != null)
{
textBox1.Text = e.Result.Text + " ";
}
}
}
}

According to the MSDN documentation on DictationGrammar, the argument-free constructor
Initializes a new instance of the DictationGrammar class for the default dictation grammar provided by Windows Desktop Speech Technology.
Is there a Czech language DicationGrammar class available on your machine? If not, you need to create one and use the other constructor DictationGrammar(String) and load one from a URI. You can also use GrammarBuilder to construct your own and load it instead using SpeechRecognizer.LoadGrammar().
You might also find this link useful; it's from 2008, but you did ask about Vista. :-)

Related

NullReferenceException while creating a SpeechRecognitionEngine object

I'm currently trying to implement some speech recognition in one of my c# project, and so I found this library :
https://learn.microsoft.com/en-us/dotnet/api/system.speech.recognition.speechrecognitionengine?view=netframework-4.8
Providing this code as an example :
using System;
using System.Speech.Recognition;
namespace SpeechRecognitionApp
{
class Program
{
static void Main(string[] args)
{
// Create an in-process speech recognizer for the en-US locale.
using (
SpeechRecognitionEngine recognizer =
new SpeechRecognitionEngine(
new System.Globalization.CultureInfo("en-US")))
{
// Create and load a dictation grammar.
recognizer.LoadGrammar(new DictationGrammar());
// Add a handler for the speech recognized event.
recognizer.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
// Configure input to the speech recognizer.
recognizer.SetInputToDefaultAudioDevice();
// Start asynchronous, continuous speech recognition.
recognizer.RecognizeAsync(RecognizeMode.Multiple);
// Keep the console window open.
while (true)
{
Console.ReadLine();
}
}
}
// Handle the SpeechRecognized event.
static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine("Recognized text: " + e.Result.Text);
}
}
}
Which should do exactly what I need.
So I created a new project in Visual Studio, copy-pasted the code, and run it.
There is no compilation error, but the constructor of SpeechRecognitionEngine, taking a non-null CultureInfo object as an argument, throws a System.NullReferenceException in System.Speech.dll.
To try to debug this, I made sure
new System.Globalization.CultureInfo("en-US")
returns a non-null object, and that this culture was installed.
I also updated my framework to 4.8, and run the project both as administrator and normal user.
I'm also using a x64 CPU platform, as the build fails with an ANY CPU platform.
It seems to me like I misconfigured something somewhere, as the code itself shouldn't be wrong.
Do you have any idea how to solve my problem ?
Thank you for your help.
EDIT : this may be linked to this problem, though I don't know if it's of any help :
NullReferenceException when starting recognizer with RecognizeAsync
I had the same error, but found the issue on my end. If you have the same issue as me, it may be that your project is using System.Speech.dll from an older framework, which I believe was causing my error. Or it may have been incompatibility between my target framework the dll?
I used NuGet to add System.Speech by Microsoft to my project. It added System.Speech (6.0.0) to the project references. This removed the null exception I was getting.
For information only, I initially added a reference to v3.0 framework, and that was causing my issue.

need guidance for speech recognition engine with custom keywords c#

I have an windows form app. I need to add speech recognition into it in order to shorten the processing time for product entry. we need this speech recognition in Turkish. if it was in english, because of wrong pronunciation it would give a lot of false results. So we need in Turkish. But windows offline speech recognition engine doesnt support Turkish.
Actually We need max 100 keywords in order to succeed this. we don't need whole language in process. So if I can create a language by adding a word and train the engine for that with a kind of training as speech training in windows, it would be great.
So I need guidance to start or move forward for this task. I have looked at the cmusphnfix but it doesnt have turkish language also. But I dont know if I can create a custom language for 100 words with correct pronunciation. if so how can ı do it in c#.
note: we dont want to use google and microsoft online services. we are looking other options.
Thanks in advance.
Windows desktop versions have built in APIs for speech recognition. These include grammar support for identifying the words or meaning of what was spoken. I don't know if Turkish is supported.
Perhaps https://stackoverflow.com/a/5473407/90236 or https://learn.microsoft.com/en-us/previous-versions/office/developer/speech-technologies/hh361633(v%3doffice.14) can help you get started
https://www.sestek.com has good Turkish speech recognition. As for 100 keywords, on that scale it is easier to recognize whole speech and just look for keywords in transcription. That will give you better accuracy because speech recognition uses more context. When you just look for keywords you do not have context so the recognition is actually less accurate.
this is script for C# console app
if you want this in windows forms, just copy static void recognize and all in this and paste it in class Form : Form1, or where you want. write recognize(); in button1_click, or where you want. in windows forms do not copy anything else!
write this:
using System;
using System.Runtime.CompilerServices;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.IO;
using System.Linq.Expressions;
namespace speechrecognition
{
class Program
{
static string a { get; set; }
static void Main(string[] args)
{
recognize();
}
static void recognize()
{
SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine();
Choices colorChoice = new Choices(new string[] { "gugl", "noutpad" });
GrammarBuilder colorElement = new GrammarBuilder(colorChoice);
Choices bothChoices = new Choices(new GrammarBuilder[] { colorElement });
Grammar grammar = new Grammar((GrammarBuilder)bothChoices);
recognizer.LoadGrammar(grammar);
try
{
recognizer.SetInputToDefaultAudioDevice();
RecognitionResult result = recognizer.Recognize();
try
{
if (result.Text != null)
{
switch (result.Text.ToString())
{
// Here you add keywords like other two
// and write the into choices color choice too
case "noutpad":
Process.Start(#"notepad.exe");
Console.WriteLine("Notepad opened!");
recognize();
break;
case "gugl":
Process.Start(#"chrome.exe");
Console.WriteLine("Google opened!");
recognize();
break;
}
}
else
{
Console.WriteLine("I dont hear you!");
recognize();
}
}
catch (System.NullReferenceException)
{
recognize();
}
}
catch (InvalidOperationException exception)
{
Console.WriteLine("I dont hear you!");
Console.ReadLine();
recognize();
}
finally
{
recognizer.UnloadAllGrammars();
recognize();
}
}
}
}

System.InvalidOperationException "The language for the grammar does not match the language of the speech recognizer"

I am currently recreating a tutorial project that I saw o recently but I am facing certain problems. Firstly I am using Windows 7 Home Premium and my OS is Turkish.
Because of this, System.Speech only works when I do text to speech, otherwise it throws an exception saying no speech recognizer installed. I checked this from the control panel as well and it says speech recognition is not available in this language. Since I do not have the ultimate version of windows I can't use language packs to change the language totally.
After a bit of research in stackoverflow I found out that installing micosoft speech platform and changing System.Speech to Microsoft.Speech works. So I followed the instructions on this web site(http://msdn.microsoft.com/en-us/library/hh362873.aspx) and installed the components including the en-US language pack.
I changed my code to reflect the changes and now I am getting different errors on different lines. Here is my code :
using System;
using System.Globalization;
using Microsoft.Speech.Recognition;
using Microsoft.Speech.Synthesis;
using System.Windows.Forms;
namespace SpeechRecognitionTest
{
public partial class Form1 : Form
{
private SpeechRecognitionEngine _speechRecognitionEngine = new SpeechRecognitionEngine(new CultureInfo("en-US"));
private SpeechSynthesizer _speechSynthesizer = new SpeechSynthesizer();
private PromptBuilder _promptBuilder = new PromptBuilder();
public Form1()
{
InitializeComponent();
}
private void btnSpeakText_Click(object sender, EventArgs e)
{
_promptBuilder.ClearContent();
_promptBuilder.AppendText(txtSpeech.Text);
_speechSynthesizer.SetOutputToDefaultAudioDevice();
_speechSynthesizer.Speak(_promptBuilder);
}
private void btnStart_Click(object sender, EventArgs e)
{
btnStart.Enabled = false;
btnEnd.Enabled = true;
var choicesList = new Choices();
choicesList.Add(new string[]{"hello","yes"});
var grammar = new Grammar(new GrammarBuilder(choicesList));
_speechRecognitionEngine.RequestRecognizerUpdate();
_speechRecognitionEngine.LoadGrammar(grammar);
_speechRecognitionEngine.SpeechRecognized += _speechRecognitionEngine_SpeechRecognized;
_speechRecognitionEngine.SetInputToDefaultAudioDevice();
_speechRecognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
}
void _speechRecognitionEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
MessageBox.Show(e.Result.Text);
}
private void btnEnd_Click(object sender, EventArgs e)
{
btnStart.Enabled = true;
btnEnd.Enabled = false;
}
}
}
Firstly, when I try text to speech I get the error "An unhandled exception of type 'System.IO.FileNotFoundException' occurred in Microsoft.Speech.dll" on this line:
_speechSynthesizer.Speak(_promptBuilder);
Secondly when I try to do voice recognition I get the following exception:
An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.Speech.dll
Additional information: The language for the grammar does not match the language of the speech recognizer.
On the line:
_speechRecognitionEngine.LoadGrammar(grammar);
I did search the internet and found mixed responses to this problem. Some could use System.Speech without a problem since they had english language installed, some non-english OS owners solved the problem through Microsoft.Speech. But there is no definitive answer on this. I am currently out of options and would really like if someone can explain what's wrong or even if I can run this code on my machine because of native OS language.
The exception in the synthesis engine is caused by the synthesis engine trying to find a default voice and failing. If you explicitly specify a voice (using SelectVoiceByHints or GetInstalledVoices(CultureInfo)), synthesis will succeed.
Second, GrammarBuilder objects have a Culture property (that defaults to the current UI culture). You will need to set it to the Culture of the recognizer before recognitions will work.

ApplicationView does not contain a definition for GetForCurrentView?

I'm currently following a tutorial from the MSDN which isn't very clear on somethings the issue that i am having is that the method that they are suggesting that i use is apparently not available to that class
Here is the link to the tutorial : http://msdn.microsoft.com/en-us/library/windows/apps/dn495655.aspx
Here is the code that i am using
In my App.Xaml.cs not my Main page i have an event handler
public App()
{
this.InitializeComponent();
Window.Current.SizeChanged += Current_SizeChanged;
this.Suspending += OnSuspending;
}
Underneath this the stub method
void Current_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
{
// Get the new view state but its not allowing me to use getforcurrent state
// almost like it doesn't exist
string CurrentViewState = ApplicationView.GetForCurrentView().Orientation.ToString();
// Trigger the Visual State Manager
VisualStateManager.GoToState(this, CurrentViewState, true);
}
If anyone else has followed this tutorial can they tell me what is going wrong ?
Have i put the code in the wrong page
Am i missing a library
I am following this microsoft tutorial word for word and it giving me the error which is the title of my post i have done research and i am using the latest version of visual studio and it's still not letting me use this method because it do not exist apparently
Try using Windows.UI.ViewManagement; at the top of the program.
The minimum supported version of this API is Windows 8.1. So you can't use it with the Windows 8 API.
MSDN reference

learning monodevelop and am having trouble getting a messgae box to appear

I am working in monodevelop and learning c#....
I am trying to get a message box to appear but I can't get it to function correctly ...
Here is my code:
using System;
using Gtk;
using GtkSharp;
public partial class MainWindow : Gtk.Window
{
public MainWindow () : base(Gtk.WindowType.Toplevel)
{
Build ();
}
protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
Application.Quit ();
a.RetVal = true;
}
protected virtual void OnButton11ResizeChecked (object sender, System.EventArgs e)
{
System.Windows.Forms.MessageBox.Show("Hello World Again!!");
}
}
What am i missing?
You cannot mix the GTK# and System.Windows.Forms UI toolkits. You need to use a GTK dialog, something like this:
void ShowMessage (Window parent, string title, string message)
{
Dialog dialog = null;
try {
dialog = new Dialog (title, parent,
DialogFlags.DestroyWithParent | DialogFlags.Modal,
ResponseType.Ok);
dialog.VBox.Add (new Label (message));
dialog.ShowAll ();
dialog.Run ();
} finally {
if (dialog != null)
dialog.Destroy ();
}
}
See also this question.
You are referencing GTK which is the bundled graphical toolkit in mono but are trying to use Windows.Forms which, although included in mono too, is a different toolkit:
System.Windows.Forms: This is the toolkit used in windows, the implementation on mono "emulates" how this controls are drawn and behave under platforms that mono runs on.
Gtk: This is a toolkit used in many OpenSource applications (Firefox, Pidgin, etc) and GTKSharp is simply the implementation of this same library but exposed to the .Net languages available on mono although you could use it directly with Visual Studio or a Microsoft compiler too.
So summarizing, as Mike said, you cannot use them both, you have to choose either one. If you are just learning .Net I would greatly advice to learn GTK instead of Windows Forms. Windows forms is kind of poor and basic toolkit, and soon you'll find that you'll need to learn a new API from a third party to do stuff that windows forms cant do (DevExpress, Infragistics) and Gtk can be easily extended and adjusted to your needs.

Categories