0
所以我最近決定再次重做我的賈維斯AI的基礎設施。現在我正在使用代碼組合(谷歌和微軟)。代碼中的Google部分不會因某種原因而觸發,並在向麥克風說話時顯示任何文字或文字。谷歌語音識別不起作用
public const int DEFAULT_BIT_RATE = 8000;
public const string DEFAULT_LANGUAGE = "en-US";
static string client = "Jarvis";
public class SpeechInputResult
{
static public string ID;
public int status;
public class Hypothesis
{
public string utterance;
public double confidence = -1.0d;//-1 = No Value
public override string ToString()
{
return "'" +utterance + "'" + ((confidence == -1) ? "" : "@" + confidence);
}
public List<Hypothesis> hypotheses = new List<Hypothesis>();
public Hypothesis getBestHypothesis()
{
if (hypotheses.Count() <=0)
return null;
Hypothesis H = hypotheses[0];
foreach (Hypothesis h in hypotheses)
{
if (h.confidence>=H.confidence)
{
H = h;
}
return H;
}
return null;
}
public string json_men = "";
public void FromJSON(String JSON)
{
json_men = JSON;
JSON = JSON.Replace("\n","").Trim();
Match M;
//status
M = new Regex("\\\"status\\\"\\:([0-9]*),", RegexOptions.IgnoreCase).Match(JSON);
//ID
M = new Regex ("\\\"id\\\"\\:\\\"([^\\\"]*)\\\",", RegexOptions.IgnoreCase).Match(JSON);
ID = M.Groups[1].Value;
//Hypotheses
int l1 = JSON.IndexOf("hypotheses");
l1 = JSON.IndexOf("[",l1);
int r1 = JSON.LastIndexOf("]");
string JSON2 = JSON.Substring(l1, r1-l1+1);
MatchCollection m2 = new Regex("{([^\\}]*)}", RegexOptions.IgnoreCase).Matches(JSON2);
foreach (Match g in m2)
{
string s = g.Value;
SpeechInputResult.Hypothesis h = new SpeechInputResult.Hypothesis();
M = new Regex("\\\"utterance\\\"\\:\\\"([^\\\"]*)\\\"", RegexOptions.IgnoreCase).Match(s);
h.utterance = M.Groups[1].Value;
M = new Regex("\\\"confidence\\\"\\:([0-9\\.]*)", RegexOptions.IgnoreCase).Match(s);
string confidence = M.Groups[1].Value;
confidence = confidence.Replace(".", ",");
if (confidence != "")
{
h.confidence = float.Parse(confidence);
}
hypotheses.Add(h);
}
}
}
public static SpeechInputResult ProcessFlacFile(string FlacFileName, int BIT_RATE = DEFAULT_BIT_RATE, string language = DEFAULT_LANGUAGE, uint maxresults = 1)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://www.google.com/speech-api/v1/recognize?xjerr=1" + "&client=" + client + "&lang=" + language + "&maxresults=" + maxresults + "&pfilter=0");
FileStream fStream = new FileStream(FlacFileName, FileMode.Open, FileAccess.Read);
request.Proxy = null;
request.Timeout = 60000;
request.KeepAlive = true;
request.Method = "POST";
request.ContentType = "audio/x-flac; rate=8000";
//bitrate must = .flac file
request.UserAgent = client;
FileInfo fInfo = new FileInfo(FlacFileName);
long numbytes = fInfo.Length;
byte[] data = null;
using (FileStream fstream = new FileStream(FlacFileName, FileMode.Open, FileAccess.Read))
data = new byte[fstream.Length];
fStream.Read(data, 0, Convert.ToInt32(fStream.Length));
fStream.Close();
using (Stream wrStream = request.GetRequestStream())
{
wrStream.Write(data, 0, data.Length);
}
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
dynamic resp = response.GetResponseStream();
if (resp != null)
{
StreamReader sr = new StreamReader(resp);
MessageBox.Show(sr.ReadToEnd());
//resp.Close();
//resp.Dispose();
}
}
catch (System.Exception ee)
{
MessageBox.Show("hi"+ee);
}
return null;
}
}
}
這裏的代碼全部來自this website。
得到它沒有錯誤後,它仍然不會返回或做任何事情,請幫助!
感謝生病看看是否可行 –