【问题标题】:SpeechRecognitionEngine stops recognizing when computer is lockedSpeechRecognitionEngine 在计算机锁定时停止识别
【发布时间】:2016-07-05 18:25:17
【问题描述】:

我正在尝试创建一个语音识别程序,该程序需要在锁定的 Windows 计算机上运行,​​作为家庭自动化项目的一部分。但似乎 SpeechRecognitionEngine 在计算机锁定时停止识别(并在计算机解锁时继续识别)。

我当前的测试程序如下所示:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Speech.Recognition;
using System.Globalization;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        SpeechRecognitionEngine sre;

        public Form1()
        {
            InitializeComponent();
            CultureInfo ci = new CultureInfo("en-us");
            sre = new SpeechRecognitionEngine(ci);
            sre.SetInputToDefaultAudioDevice();
            GrammarBuilder gb = new GrammarBuilder("Hello");
            sre.LoadGrammarAsync(new Grammar(gb));
            sre.SpeechRecognized += sre_SpeechRecognized;
            sre.RecognizeAsync(RecognizeMode.Multiple);
        }

        void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            listBox1.Items.Add(DateTime.Now.ToString() + " " + e.Result.Text);
        }
    }
}

我想知道是否可以将 SpeechRecognitionEngine 的输入(可能使用SetInputToAudioStreamSetInputToWaveStream 方法)更改为麦克风输入的实时音频流,这样可以解决问题。因为电脑的时候好像没有关闭麦克风(用录音机试过)。

很遗憾,我无法找到一种方法来获取麦克风输入的实时流。

【问题讨论】:

  • 我曾尝试使用录音机在计算机被锁定的情况下进行录音,但没有问题。我还做了一个小程序,使用 NAudio 将麦克风循环到扬声器。这也适用于计算机锁定。不幸的是,我找不到让 NAudio 提供可以与 SpeechRecognitionEngine 一起使用的流的方法。因此,计算机锁定时似乎可以使用麦克风。而关于使用服务,那么似乎系统服务无法访问麦克风和扬声器。

标签: c# speech-recognition audio-recording


【解决方案1】:

我从这个 StackOverflow 答案 (https://stackoverflow.com/a/11813276/2950065) 中找到了使用 NAudio (http://naudio.codeplex.com/) 和 SpeechStreamer 类的解决方法。

更新的测试程序在计算机被锁定时继续识别,如下所示:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Speech.Recognition;
using System.Globalization;
using NAudio.Wave;
using System.IO;
using System.IO.Pipes;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        SpeechRecognitionEngine sre;
        WaveIn wi;
        SpeechStreamer ss;

        public Form1()
        {
            InitializeComponent();

            WaveCallbackInfo callbackInfo = WaveCallbackInfo.FunctionCallback();
            wi = new WaveIn(callbackInfo);
            ss = new SpeechStreamer(100000);
            wi.DataAvailable += wi_DataAvailable;
            wi.StartRecording();

            CultureInfo ci = new CultureInfo("en-us");
            sre = new SpeechRecognitionEngine(ci);
            // The default format for WaveIn is 8000 samples/sec, 16 bit, 1 channel
            Microsoft.Speech.AudioFormat.SpeechAudioFormatInfo safi = new Microsoft.Speech.AudioFormat.SpeechAudioFormatInfo(8000, Microsoft.Speech.AudioFormat.AudioBitsPerSample.Sixteen, Microsoft.Speech.AudioFormat.AudioChannel.Mono);
            sre.SetInputToAudioStream(ss, safi);
            GrammarBuilder gb = new GrammarBuilder("Hello");
            sre.LoadGrammarAsync(new Grammar(gb));
            sre.SpeechRecognized += sre_SpeechRecognized;
            sre.RecognizeAsync(RecognizeMode.Multiple);
        }

        void wi_DataAvailable(object sender, WaveInEventArgs e)
        {
            ss.Write(e.Buffer, 0, e.BytesRecorded);
        }

        void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            listBox1.Items.Add(DateTime.Now.ToString() + " " + e.Result.Text);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多