gpt4 book ai didi

c# - 使用 C# 的 Google 语音识别 REST API 的错误请求错误

转载 作者:行者123 更新时间:2023-11-30 17:31:24 28 4
gpt4 key购买 nike

我想在 C# 中使用 Google 语音识别 API Rest。我想使用 API Rest 而不是客户端库的原因是因为我想使用 Unity3D,但它目前不支持客户端库。

我使用 HttpClient 进行连接,使用 Newtonsoft Json 序列化 json(我现在正在使用 Windows 窗体,当它在 Winforms 中工作时将迁移到 Unity)。

我总是从 Google 收到错误的请求响应,它没有提供更多详细信息,但我注意到,如果我将 API key 值更改为无效值,我会得到相同的结果。

这是我的代码:

类:

class Speech
{
public RecognitionConfig config { get; set; }
public RecognitionAudio audio { get; set; }

public bool sendToApi(string baseUri, string url, ref string apiResponse)
{
try
{
HttpClient client = new HttpClient();

// Update port # in the following line.
client.BaseAddress = new Uri(baseUri);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

//var speechJson = new JavaScriptSerializer().Serialize(certificado);
string speechJson = JsonConvert.SerializeObject(this);
var contenido = new StringContent(speechJson.ToString(), Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsync(url, contenido).Result;

if (response.IsSuccessStatusCode)
{
string responseJson = response.Content.ReadAsStringAsync().Result;

apiResponse = responseJson;
}
else
{
apiResponse = "ERROR " + JsonConvert.DeserializeObject(JsonConvert.SerializeObject(response));
}

return true;

}

catch (Exception e)
{
apiResponse = e.Message;
return false;
}
}
}

class RecognitionConfig
{
public string encoding { get; set; }
public int sampleRateHertz { get; set; }
public string languageCode { get; set; }
// public int maxAlternatives { get; set; }
// public bool profanityFilter { get; set; }
// public List<SpeechContext> speechContexts { get; set; }
// public bool enableWordTimeOffsets { get; set; }

}

class SpeechContext
{
public List<string> phrases { get; set; }

}
class RecognitionAudio
{
public string content { get; set; }
// public string uri { get; set; }

public bool setContentBase64FromAudio(string path)
{
try
{
FileStream fileStream = File.OpenRead(path);

MemoryStream memoryStream = new MemoryStream();
memoryStream.SetLength(fileStream.Length);
fileStream.Read(memoryStream.GetBuffer(), 0, (int)fileStream.Length);
byte[] BA_AudioFile = memoryStream.GetBuffer();
this.content = System.Convert.ToBase64String(BA_AudioFile);

return true;
}
catch(Exception e)
{
return false;
}

}
}

电话:

   private void button1_Click(object sender, EventArgs e)
{
Speech speech = new Speech();

speech.config = new RecognitionConfig();
speech.audio = new RecognitionAudio();
speech.config.encoding = "FLAC";
speech.config.sampleRateHertz = 44100;
speech.config.languageCode = "en-US";

RecognitionAudio audio = new RecognitionAudio();
audio.setContentBase64FromAudio("C:\\Users\\Manena\\Downloads\\good-morning-google.flac");

speech.audio = audio;

string response = "";
speech.sendToApi("https://speech.googleapis.com/", "v1/speech:recognize?key=<mykey>", ref response);
textBox1.Text = response;

}
}

编辑:这是我发送的 Json:

{  
"config":{
"encoding":"FLAC",
"sampleRateHertz":44100,
"languageCode":"en-US"
},
"audio":{
"content":"base64 audio"
}
}

以及我收到的:

    {
"Version": {
"Major": 1,
"Minor": 1,
"Build": -1,
"Revision": -1,
"MajorRevision": -1,
"MinorRevision": -1
},
"Content": {
"Headers": [
{
"Key": "Content-Type",
"Value": [
"application/json; charset=UTF-8"
]
}
]
},
"StatusCode": 400,
"ReasonPhrase": "Bad Request",
"Headers": [
{
"Key": "Vary",
"Value": [
"X-Origin",
"Referer",
"Origin",
"Accept-Encoding"
]
},
{
"Key": "X-XSS-Protection",
"Value": [
"1; mode=block"
]
},
{
"Key": "X-Frame-Options",
"Value": [
"SAMEORIGIN"
]
},
{
"Key": "X-Content-Type-Options",
"Value": [
"nosniff"
]
},
{
"Key": "Alt-Svc",
"Value": [
"hq=\":443\"; ma=2592000; quic=51303431; quic=51303339; quic=51303338; quic=51303337; quic=51303335,quic=\":443\"; ma=2592000; v=\"41,39,38,37,35\""
]
},
{
"Key": "Transfer-Encoding",
"Value": [
"chunked"
]
},
{
"Key": "Accept-Ranges",
"Value": [
"none"
]
},
{
"Key": "Cache-Control",
"Value": [
"private"
]
},
{
"Key": "Date",
"Value": [
"Sat, 30 Dec 2017 09:06:19 GMT"
]
},
{
"Key": "Server",
"Value": [
"ESF"
]
}
],
"RequestMessage": {
"Version": {
"Major": 1,
"Minor": 1,
"Build": -1,
"Revision": -1,
"MajorRevision": -1,
"MinorRevision": -1
},
"Content": {
"Headers": [
{
"Key": "Content-Type",
"Value": [
"application/json; charset=utf-8"
]
},
{
"Key": "Content-Length",
"Value": [
"106"
]
}
]
},
"Method": {
"Method": "POST"
},
"RequestUri": "https://speech.googleapis.com/v1/speech:recognize?key=mykey",
"Headers": [
{
"Key": "Accept",
"Value": [
"application/json"
]
}
],
"Properties": {}
},
"IsSuccessStatusCode": false
}

我知道我的代码可能不是最优雅的,但现在我只对从 Google API 获得好的响应感兴趣。有什么线索吗?

最佳答案

我已经解决了这个问题。

问题是我使用的是 2 声道音频文件,而 Google Speech API 目前只接受单声道音频。

所以问题中的代码适用于单声道音频,它可能对某些人有用

谢谢

关于c# - 使用 C# 的 Google 语音识别 REST API 的错误请求错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48023218/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com