Sunday, November 9, 2014

Voice Recognition with Java and C#

In previous post, I mentioned AIML. It's an AI ChatBot to digest natural language chat. I am always think it's cool to combine with Voice Recognition. Thus, people can be not only asking the ChatBot quesion for the answer but also asking AI ChatBot to "do something". Here is an example that the Voice activation with system (US Pizza Papa John’s allows to order pizza via Voice System, same with their supply chain system :http://www.papajohns.com/ordering/mobile.shtm, http://www.mmh.com/article/papa_johns_fresh_take_on_wms_and_voice_technology )

I spent my weekend and try a little bit voice recognition on demo with Java and C#, here are the details.
(PS: It’s better you try this in Meeting Room by alone or at your home, otherwise when you test, you will look like idiot )    J

Java Version
---------------

Preparation: 
----------------
JDK 8 ( J2EE )
Eclipse SDK ( LUNA )
JSAPI ( Included in Sphinx 4 : jsapi.jar)

1.       Create a java project and add jars

2.       Test HelloWorld.jar

3.       Try Hello World Source Code

a.       HelloWorld.java
b.      hello.gram ( grammer file )
c.       helloworld.config.xml ( config file )

4.       I added new recognition grammer “Hey Johnny” in *.gram


Modified *.java showing in command line.


5.       I don’t wanna change config.xml now so I follow whatever in manifest/config first. ( add one more jar into UserLib ). Now it’s working


Last: I can duplicate this voice recognition to trigger “Check Vagrant Version” with my Java ChatBot. Then it will trigger check vagrant version cli.
-----------------------------------------------------------------------------------
C# version, here has full example.

add  System.Speech reference



Here is form1.cs example:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Recognition;

namespace CSharpVoiceRecognition
{
    public partial class Form1 : Form
    {
        SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Choices commands = new Choices();
            commands.Add(new string[] { "Say Hello""Print My Name""Love You""Please Check My System" });
            GrammarBuilder gBuilder = new GrammarBuilder();
            gBuilder.Append(commands);
            Grammar grammer = new Grammar(gBuilder);

            recEngine.LoadGrammarAsync(grammer);
            recEngine.SetInputToDefaultAudioDevice();
            recEngine.SpeechRecognized +=recEngine_SpeechRecognized;
           
        }

        private void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            switch (e.Result.Text)
            {
                case "Say Hello":
                    MessageBox.Show("Hello. How are you ?");
                    break;
                case "Print My Name":
                    richTextBox1.Text+="\nJohnny";
                    break;
                case "Love You":
                    richTextBox1.Text += "\nLove You Too!";
                    break;
                case "Please Check My System":
                    richTextBox1.Text += "\nNo Problem, I'm checking vagrant.";
                    break;
                default:
                    richTextBox1.Text = e.Result.Text;
                    break;
            }
            //throw new NotImplementedException();
        }

        private void btnEnable_Click(object sender, EventArgs e)
        {
            recEngine.RecognizeAsync(RecognizeMode.Multiple);
            richTextBox1.Text += "\nEnable Voice Recognition";
        }

        private void btnDisable_Click(object sender, EventArgs e)
        {
            recEngine.RecognizeAsyncStop();
            richTextBox1.Text += "\nDisable Voice Recognition";
        }
    }

}

No comments:

Post a Comment