1

I am trying to make my existing console based Java program which is a speech recognition project using sphinx, into a GUI based program. I tried altering the code, but the existing program does not run if I alter it.

My existing code for recognition (full code) and it works perfectly without GUI:

public class HelloWorld {
    /**
     * Main method for running the HelloWorld demo.
     */
    static int i=1;
    static String resultText;public static void main(String[] args) {
        try {
            URL url;
           if (args.length > 0) {
               url = new File(args[0]).toURI().toURL();
            } else {
                url = HelloWorld.class.getResource("helloworld.config.xml");
            }
            System.out.println("Loading...");
            ConfigurationManager cm = new ConfigurationManager(url);
            Recognizer recognizer = (Recognizer) cm.lookup("recognizer");
            Microphone microphone = (Microphone) cm.lookup("microphone");
            /* allocate the resource necessary for the recognizer */
            recognizer.allocate();
            /* the microphone will keep recording until the program exits */
            if (microphone.startRecording())
            {   
                System.out.println("Say: (Command | Program| Browser | Bluetooth |  Device Manager |Power Options |Cal | Control | Player |task manager | Windows Security Center)");
                System.out.println("Say: ( open word | open phot oshop|open Access|start Excel|start nero |start fire wall| open Pad |open Paint)");
            while (true) 
            {
            System.out.println("Start speaking. Press Ctrl-C to quit.\n");
                    /*
                     * This method will return when the end of speech
                     * is reached. Note that the endpointer will determine
                     * the end of speech.
                     */ 
            Result result = recognizer.recognize();
            if (result != null)
            {
                    System.out.println("Enter your choise"+ "\n");
             resultText = result.getBestFinalResultNoFiller();
            System.out.println("You said: " + resultText + "\n");
//          Applications*********************************************
            if(resultText.equalsIgnoreCase("Command Prompt"))
            {
                try{
                    Runtime.getRuntime().exec("cmd /c start cmd");
                    }
                catch(Exception er){    
                }
            }
//          Simulate action commands by importing the robot class above
            if(resultText.equalsIgnoreCase("scroll up"))
            {
                try {
                    Robot r = new Robot();
                    r.keyPress(KeyEvent.VK_UP);
                    r.delay(500);
                    r.keyPress(KeyEvent.VK_UP);
                    r.delay(500);
                    r.keyPress(KeyEvent.VK_UP);
                } catch (AWTException e) {
                    e.printStackTrace();
                }
            }
//          Program Action Command ABOUT
            else if(resultText.equalsIgnoreCase("recognition stop"))
            {
                try{

                    //recognizer.wait();
                    System.out.println("See you later!");
                    System.exit(0);}
                catch(Exception estop ){}
            }                       
            else
            {
                        System.out.println("I can't hear what you said.\n");
            }
       }
       } 
        else
        {
            System.out.println("Cannot start microphone.");
            recognizer.deallocate();
            System.exit(1);
        }

        } catch (IOException e) {
            System.err.println("Problem when loading HelloWorld: " + e);
            e.printStackTrace();
        } catch (PropertyException e) {
            System.err.println("Problem configuring HelloWorld: " + e);
            e.printStackTrace();
        } catch (InstantiationException e) {
            System.err.println("Problem creating HelloWorld: " + e);
            e.printStackTrace();
        }

    }
}

This is the code for Gui where I want the existing program to start (full code):

    JButton btnNewButton = new JButton("Start Recognizing");
    btnNewButton.setBackground(UIManager.getColor("Button.background"));
    btnNewButton.setForeground(new Color(34, 139, 34));
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            state.setText("Listening");
            System.out.println("Started Listening");
            state.setBackground(new Color(51, 204, 0));

            // Start recognizing from the existing program

        }
    });

And the part where I want to pause/stop recording:

    JButton btnNewButton_1 = new JButton("Stop Recognizing");
    btnNewButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            state.setText("Not listening");
            state.setBackground(new Color(204, 0, 51));
            System.out.println("Stopped Listening");

            // Pause/Stop recognition

        }
    })

How do I do it?

halfer
  • 19,824
  • 17
  • 99
  • 186
Robin
  • 5,366
  • 17
  • 57
  • 87
  • What is it that doesnt work? – Clyde D'Cruz Mar 10 '15 at 18:55
  • I tried running the GUI code .It works fine – Clyde D'Cruz Mar 10 '15 at 19:01
  • @ClydeD'Cruz Yes, the GUI is a separe class file inside the same eclipse project, and it works fine. What I want to achieve is, add the GUI to the existing program file, so that when I press the "start recognition" in the GUI, it should start the recognition process from the existing program. I hope I was clear, if not please ask. Thank you. – Robin Mar 10 '15 at 19:10

3 Answers3

1

You need to run recognition in background thread. That way you will be able to interact with GUI and recognize in the same time.

You can check the following tutorial to get understanding of how this can be implemented:

http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html

Nikolay Shmyrev
  • 24,897
  • 5
  • 43
  • 87
  • Yes, I think this will work. Can you please show it how to implement it with my project. – Robin Mar 10 '15 at 19:28
1

Using SwingWorker, run the recognizer in your implementation of doInBackground() using ProcessBuilder as shown here. You can publish() interim results and append() them to a JTextArea in your implementation of process().

Addendum: Looking at the API, it should be possible to skip ProcessBuilder and instantiate a LiveSpeechRecognizer directly, as shown here. Your implementation of publish() could then iterate over the List<WordResult> returned by SpeechResult#getWords() via getResult().

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

You cannot have 2 classes containing main functions if you want to use them together.You must use the main function in frame1 class , create an object of hello world class and call the functions of hello world using this object.The calling of these functions must be done from the event handlers of frame1.

Clyde D'Cruz
  • 1,915
  • 1
  • 14
  • 36