使用谷歌语音API [英] Using Google Speech API
问题描述
什么是code实现基于应用程序在我的C#的谷歌语音API?我发现,它可以创建一个音频文件,并把它交给 HTTP://slides.html5rocks。 COM /#语音输入,并接受它作为文本。你能解释一下如何做到这一点还是我提供code,如果你以前尝试呢?现在已经困在这里了一会儿
What is the code for implementing the Google Speech API in my C# based application? I found out that it is possible to create an audio file and sent it to http://slides.html5rocks.com/#speech-input and receive it as text. Could you please explain how to do this or provide me with the code if you have attempted this before? Been stuck here for a while now
大部分AP preciated。
Much appreciated.
code到目前为止:
Code So far:
SpeechRecognitionEngine rec = new SpeechRecognitionEngine();
SpeechSynthesizer dummy = new SpeechSynthesizer();
public Form1()
{
InitializeComponent();
Choices searching = new Choices("Porsche");
GrammarBuilder searchService = new GrammarBuilder("Search");
searchService.Append(searching);
// Create a Grammar object from the GrammarBuilder and load it to the recognizer.
Grammar googleGrammar = new Grammar(searchService); ;
rec.RequestRecognizerUpdate();
rec.LoadGrammar(googleGrammar);
// Add a handler for the speech recognized event.
rec.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(_recognizer_SpeechRecognized);
// Configure the input to the speech recognizer.
rec.SetInputToDefaultAudioDevice();
// Start asynchronous, continuous speech recognition.
rec.RecognizeAsync(RecognizeMode.Multiple);
}
private void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
try
{
FileStream FS_Audiofile = new FileStream("temp.flac", FileMode.Open, FileAccess.Read);
BinaryReader BR_Audiofile = new BinaryReader(FS_Audiofile);
byte[] BA_AudioFile = BR_Audiofile.ReadBytes((Int32)FS_Audiofile.Length);
FS_Audiofile.Close();
BR_Audiofile.Close();
HttpWebRequest _HWR_SpeechToText = null;
_HWR_SpeechToText = (HttpWebRequest)WebRequest.Create("http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=de-DE&maxresults=1&pfilter=0");
_HWR_SpeechToText.Method = "POST";
_HWR_SpeechToText.ContentType = "audio/x-flac; rate=44100";
_HWR_SpeechToText.ContentLength = BA_AudioFile.Length;
_HWR_SpeechToText.GetRequestStream().Write(BA_AudioFile, 0, BA_AudioFile.Length);
HttpWebResponse HWR_Response = (HttpWebResponse)_HWR_SpeechToText.GetResponse();
if (HWR_Response.StatusCode == HttpStatusCode.OK)
{
StreamReader SR_Response = new StreamReader(HWR_Response.GetResponseStream());
textBox1.Text = SR_Response.ToString();
}
}
catch (Exception ex)
{
}
}
这不会从谷歌返回任何值。
This does not return any value from Google.
推荐答案
在袅袅以下工作只要发送的文件是不是太长了......下5秒。
the following works in curl as long as the file sent is not too long... under 5 seconds.
卷曲-X POST -H内容类型:音频/ X-后手;率= 16000\\
-T seg_1.flac https://www.google.com/speech-api/v1 /认识 \\ xjerr = 1&安培;客户端= speech2text&放大器;的maxResults = 1&安培; LANG = EN-US&放大器;关键= ... 48593
curl -X POST -H "Content-Type: audio/x-flac; rate=16000" \ -T seg_1.flac "https://www.google.com/speech-api/v1/recognize? \ xjerr=1&client=speech2text&maxresults=1&lang=en-US&key=...48593"
{身份:0,ID:,假设:[{话语:现在它是
最喜欢的消遣,信心:0.95148802}]}
{"status":0,"id":"","hypotheses":[{"utterance":"now it was the favorite pastime","confidence":0.95148802}]}
所以,EN code到speechX或FLAC
So, encode to speechX or flac
包括PARM从您的采样率记录
include a parm with your sample rate from the recording
包括您键
保持文件持续时间短(你将不得不之前的API访问拆分文件)
keep the file short in duration ( you will have to split files prior to API access )
这篇关于使用谷歌语音API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!