地址:http://blog.csdn.net/dlangu0393/article/details/7214728

 

 

最近在使用Qt编写一个客户端程序的时候需要增加语音识别输入的功能。起初尝试使用SAPI来完成这个任务,但是发现SAPI不仅使用起来超级复杂,而且识别效果也很惨烈。于是就需要寻找一个更加便捷优秀的方案。

自从Chrome 11开始,Chrome开始支持HTML5的语音输入API,QQ紧接着也推出了语音识别输入(可以看做跟风么:D)。显然这些识别操作不可能在本地完成,那么我们就有直接利用接口的可能。


对Chromium的repo进行搜索之后,终于找到了Chromium对语音识别的实现代码:


http://src.chromium.org/viewvc/chrome/trunk/src/content/browser/speech/


分析过程就算了,直接上成果。

Chromium先从mic获取音频,然后使用flac或者speex进行编码,直接通过HTTPS POST到服务器。接口地址如下:


https://www.google.com/speech-api/v1/recognize


Chromium在请求时还会拼上很多参数:


xjerr=1&client=chromium&lang=en-US&maxresults=1


注:参数解释

xjerr=1 # 不详,猜测为错误的标准

client=chromium # 客户端类型,这里是Chromium,猜测Chrome也应该可行,估计是作为统计用的。

lang=en-US # 语言类型,这里是英文,中文为zh-CN,其余语言代码参考:http://msdn.microsoft.com/en-us/library/ms533052(v=vs.85).aspx

maxresults=1 # 最大返回结果数量,多个结果在hypotheses列表中保存。


参数很明了,这给我们提供了很多便利。我们对参数进行调整,得到如下的接口地址:


http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN&maxresults=1


接下来祭出wget,对已经发现的接口进行测试:

  1. flac.exe -8 -f --sample-rate=16000 speechInput.wav

  1. wget -O "GoogleSpeechAPI.txt" --user-agent="Mozilla/5.0" --post-file=test.flac --header="Content-Type: audio/x-flac; rate=16000" "http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN&maxresults=1"

结果如下:

[javascript] view plain copy print ?
  1. {
  2. "status":0, /* 结果代码,详细见本文结尾 */
  3. "id":"c421dee91abe31d9b8457f2a80ebca91-1", /* 识别编号 */
  4. "hypotheses": /* 假设,即结果 */
  5. [
  6. {
  7. "utterance":"下午好", /* 话语 */
  8. "confidence":0.2507637 /* 信心,即准确度 */
  9. }
  10. ]
  11. }


注:注释后为手工添加的结果解释

返回结果太明了了!直接就能拿来用了不是~ 返回的编码是UTF-8

对于编码格式,在测试中使用了FLAC编码,采样率为16kHz,经测试其他采样率同样可用,但一定要保证Header里的rate与实际数据相符。(关于其他格式的实验请看本文底部。)




总结:

1、基本流程:

一、从音频输入设备获取原始数据。
二、对原始数据进行包装、编码。
三、将编码后的音频POST至接口地址。
四、分析处理接口返回的JSON并得出结果。

2、请求接口

请求方式:HTTP POST
头部信息:Content-Type: audio/x-flac; rate=16000 (注:Content-Type根据所使用的编码格式不同而不同,详见文章底部。rate为音频采样率。)
请求数据:编码后的音频数据

3、音频编码格式:

FLAC或WAV或SPEEX


下面是我写的Qt(C++)中的请求:

  1. void Protocol::Request_SPEECH(QByteArray & audioData)
  2. {
  3. if (!Nt_SPEECH)
  4. {
  5. QNetworkRequest request;
  6. QString speechAPI = "http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN&maxresults=1";
  7. request.setUrl(speechAPI);
  8. request.setRawHeader("User-Agent", "Mozilla/5.0");
  9. request.setRawHeader("Content-Type", "audio/x-flac; rate=16000");
  10. Nt_SPEECH = NetworkMGR.post(request, audioData);
  11. connect(Nt_SPEECH, SIGNAL(readyRead()), this, SLOT(Read_SPEECH()));
  12. }
  13. }


至于读取函数,就不贴在这里了,具体见:

Protocol: http://pastebin.com/6G6wggfF

AudioInput:

speechInput.h: http://pastebin.com/qdMPeWZD

speechInput.cpp: http://pastebin.com/567B47qF

main:

mainwidget: http://pastebin.com/c8bk7zd2


在翻阅Chromium源码的过程之中,还发现了其他有用的东西:

Speech Input API Specification http://www.w3.org/2005/Incubator/htmlspeech/2010/10/google-api-draft.html

到目前为止,Google好像还没有公开这个API,使用许可依旧不详,请求也没有用到任何认证。但它确实能用,而且十分方便,对于编写非商业程序的人来说,这个东西真的是再好不过了(因为它有着高的爆表的识别率)。


参考:

Chromium Repository http://src.chromium.org/viewvc/chrome/trunk/src/content/browser/speech/

Accessing Google Speech API / Chrome 11 http://mikepultz.com/2011/03/accessing-google-speech-api-chrome-11/




附:

1、SpeechInputError interface 错误信息

  1. // This enumeration follows the values described here:
  2. // http://www.w3.org/2005/Incubator/htmlspeech/2010/10/google-api-draft.html#speech-input-error
  3. enum SpeechInputError {
  4. // There was no error.
  5. SPEECH_INPUT_ERROR_NONE = 0,
  6. // The user or a script aborted speech input.
  7. SPEECH_INPUT_ERROR_ABORTED,
  8. // There was an error with recording audio.
  9. SPEECH_INPUT_ERROR_AUDIO,
  10. // There was a network error.
  11. SPEECH_INPUT_ERROR_NETWORK,
  12. // No speech heard before timeout.
  13. SPEECH_INPUT_ERROR_NO_SPEECH,
  14. // Speech was heard, but could not be interpreted.
  15. SPEECH_INPUT_ERROR_NO_MATCH,
  16. // There was an error in the speech recognition grammar.
  17. SPEECH_INPUT_ERROR_BAD_GRAMMAR,
  18. };


2、多种音频格式的测试

收到朋友的邮件说使用flac实在是很不方便,问我有没有更好的解决方法,于是我尝试将其他编码格式应用于Google Speech API。以下为结果:

1、WAV格式

请求Header:Content-Type: audio/L16; rate=16000

返回结果:识别成功

2、MP3格式

请求Header:Content-Type: audio/mpeg; rate=16000

返回结果:无法识别的编码

请求Header:Content-Type: audio/mpeg3; rate=16000

返回结果:无法识别的编码

请求Header:Content-Type: audio/x-mpeg; rate=16000

返回结果:无法识别的编码

请求Header:Content-Type: audio/x-mpeg-3; rate=16000

返回结果:无法识别的编码

请求Header:Content-Type: audio/mp3; rate=16000

返回结果:无法识别的编码

3、PCM格式

请求Header:Content-Type: audio/x-ogg-pcm; rate=16000

返回结果:无法识别的编码

请求Header:Content-Type: audio/pcm; rate=16000

返回结果:无法识别的编码

4、SPEEX格式

请求Header:Content-Type: audio/x-speex-with-header-byte; rate=16000

返回结果:识别成功

请求Header:Content-Type: audio/speex; rate=16000

返回结果:识别成功


由于识别接口并不开放,所以无法得知具体的支持格式,如果哪位朋友发现了新的支持格式,请一定要留言哦!

 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐