使用UI在后台运行sphinx4识别器 [英] Run sphinx4 recognizer in background with UI

查看:118
本文介绍了使用UI在后台运行sphinx4识别器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 sphinx制作现有的基于控制台的Java程序,这是一个语音识别项目. a>,插入基于GUI的程序.我尝试更改代码,但是如果更改现有代码,则现有程序将无法运行.

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.

我现有的识别代码(完整代码),并且在没有GUI的情况下也可以完美运行:

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();
        }

    }
}

这是Gui的代码,我要在其中启动现有程序(完整代码):

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

        }
    })

我该怎么做?

推荐答案

使用此处.您可以在执行process()的过程中publish()临时结果并将append()转换为JTextArea.

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().

附录:查看 API ,应该可以跳过ProcessBuilder并直接实例化LiveSpeechRecognizer,如此处所示.然后,您实现的publish()可以通过getResult()遍历SpeechResult#getWords()返回的List<WordResult>.

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().

这篇关于使用UI在后台运行sphinx4识别器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆