Google 云语音错误 500(内部服务器错误)
Google Cloud Speech Error 500 (Internal server error)
我在 xampp apache 中有一个简单的 JS 代码 运行。
var rawFile;
var allText;
var byteArray = [];
rawFile = new XMLHttpRequest();
rawFile.open("GET", "brooklyn.flac", false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
allText = rawFile.responseText;
//alert(allText);
}
}
if (rawFile.status != 200) return byteArray;
for (var i = 0; i < rawFile.responseText.length; ++i) {
byteArray.push(rawFile.responseText.charCodeAt(i) & 0xff)
}
}
rawFile.send(null);
function send(){
var oAjaxReq = new XMLHttpRequest();
var payload = {
config:{
encoding: "FLAC",
sampleRateHertz: 16000,
languageCode:"en-US"
},
audio: {
content: rawFile
}
};
oAjaxReq.open("post", "https://speech.googleapis.com/v1/speech:recognize?key=???", true);
oAjaxReq.setRequestHeader("Content-Type", "application/json");
//object ot json
const jsonPayload = JSON.stringify(payload);
//Length of the jsonPayload
const payLoadLength= jsonPayload.length;
oAjaxReq.setRequestHeader("Content-Length", payLoadLength);
oAjaxReq.withCredentials = true;
//Send Json to Google Cloud Speech Service
oAjaxReq.send(jsonPayload);
}
我正在尝试使用 Google 云语音 API。
我正在加载名称为 "brooklyn.flac" 的本地音频文件,我通过 xmlHTTPRequest 从“https://storage.googleapis.com/cloud-samples-tests/speech/brooklyn.flac”下载了该文件。
但是,我总是收到以下错误:
{
"error": {
"code": 500,
"message": "Internal error encountered.",
"status": "INTERNAL"
}
}
当我将 payLoad 对象的音频部分从 "content: rawFile" 更改为 "uri: "gs://cloud-samples-tests/speech/brooklyn.flac" 时,它工作正常。
是因为"rawFile"的错误吗?
如果是,如何正确加载此本地文件以将其发送到云服务?
TL;DR - 除了你的音频内容,其余的有效负载对我来说看起来不错。您很可能没有使用 base64
对音频进行编码(因为您没有提及任何相关内容),因此遇到了服务器错误。传递音频内容的 base64
编码版本将解决问题。
API 音频内容的类型和来源
云语音主要有两种使用方式API:
它们都支持来自以下任何一个的音频内容:
- Google 云存储
- 您直接在请求中发送的原始文件。
使用 Google 云存储是 IMO 两种方法中最简单的一种。您将文件上传到 GCS 并传递文件的 URI(例如 gs://cloud-samples-tests/speech/brooklyn.flac
)。
编码音频内容
如果您将原始文件作为请求的一部分发送,根据您使用的API,您需要相应地处理内容:
JSON 基于 REST API requires that the audio content be base64
encoded。 link 也有关于如何在 Python、Node.js 和 Java.
中执行 base64 编码的示例
基于gRPC的API支持二进制文件上传,因此您可以按原样传递原始文件,无需任何额外处理。
Google 云客户端库
我不确定 Node.js
是否是一个选项,但如果是 - 您可以 use the Google Cloud client library(仅在撰写本文时支持 Alpha 中的语音识别)。此库可以自动 base64
为您编码文件并提供其他方便的功能。
我在 xampp apache 中有一个简单的 JS 代码 运行。
var rawFile;
var allText;
var byteArray = [];
rawFile = new XMLHttpRequest();
rawFile.open("GET", "brooklyn.flac", false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
allText = rawFile.responseText;
//alert(allText);
}
}
if (rawFile.status != 200) return byteArray;
for (var i = 0; i < rawFile.responseText.length; ++i) {
byteArray.push(rawFile.responseText.charCodeAt(i) & 0xff)
}
}
rawFile.send(null);
function send(){
var oAjaxReq = new XMLHttpRequest();
var payload = {
config:{
encoding: "FLAC",
sampleRateHertz: 16000,
languageCode:"en-US"
},
audio: {
content: rawFile
}
};
oAjaxReq.open("post", "https://speech.googleapis.com/v1/speech:recognize?key=???", true);
oAjaxReq.setRequestHeader("Content-Type", "application/json");
//object ot json
const jsonPayload = JSON.stringify(payload);
//Length of the jsonPayload
const payLoadLength= jsonPayload.length;
oAjaxReq.setRequestHeader("Content-Length", payLoadLength);
oAjaxReq.withCredentials = true;
//Send Json to Google Cloud Speech Service
oAjaxReq.send(jsonPayload);
}
我正在尝试使用 Google 云语音 API。 我正在加载名称为 "brooklyn.flac" 的本地音频文件,我通过 xmlHTTPRequest 从“https://storage.googleapis.com/cloud-samples-tests/speech/brooklyn.flac”下载了该文件。
但是,我总是收到以下错误:
{
"error": {
"code": 500,
"message": "Internal error encountered.",
"status": "INTERNAL"
}
}
当我将 payLoad 对象的音频部分从 "content: rawFile" 更改为 "uri: "gs://cloud-samples-tests/speech/brooklyn.flac" 时,它工作正常。
是因为"rawFile"的错误吗? 如果是,如何正确加载此本地文件以将其发送到云服务?
TL;DR - 除了你的音频内容,其余的有效负载对我来说看起来不错。您很可能没有使用 base64
对音频进行编码(因为您没有提及任何相关内容),因此遇到了服务器错误。传递音频内容的 base64
编码版本将解决问题。
API 音频内容的类型和来源
云语音主要有两种使用方式API:
它们都支持来自以下任何一个的音频内容:
- Google 云存储
- 您直接在请求中发送的原始文件。
使用 Google 云存储是 IMO 两种方法中最简单的一种。您将文件上传到 GCS 并传递文件的 URI(例如 gs://cloud-samples-tests/speech/brooklyn.flac
)。
编码音频内容
如果您将原始文件作为请求的一部分发送,根据您使用的API,您需要相应地处理内容:
JSON 基于 REST API requires that the audio content be
base64
encoded。 link 也有关于如何在 Python、Node.js 和 Java. 中执行 base64 编码的示例
基于gRPC的API支持二进制文件上传,因此您可以按原样传递原始文件,无需任何额外处理。
Google 云客户端库
我不确定 Node.js
是否是一个选项,但如果是 - 您可以 use the Google Cloud client library(仅在撰写本文时支持 Alpha 中的语音识别)。此库可以自动 base64
为您编码文件并提供其他方便的功能。