【问题标题】:Google Speech API duplicates responsesGoogle Speech API 重复响应
【发布时间】:2014-05-15 15:20:45
【问题描述】:

我正在使用带有 PHP 的 Speech API v2,这是一个代码:

 $file_to_upload = array('myfile'=>'@'.$filename.'.flac');
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, "https://www.google.com/speech-api/v2/recognize?output=json&lang=ru-RU&key=___my_api_key___"); 
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: audio/x-flac; rate=8000"));
 curl_setopt($ch, CURLOPT_POSTFIELDS, $file_to_upload);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 $result=curl_exec ($ch);

带有两个 JSON 对象的 Google 响应,第一个是空的,第二个是我期望的有效响应。这会导致解析和进一步处理的困难。请参阅 HTTP 转储:

我的 POST 请求:

POST /speech-api/v2/recognize?output=json&lang=ru-RU&key=___my_api_key___ HTTP/1.1
Host: www.google.com
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36
Content-Length: 13123
Expect: 100-continue
Content-Type: audio/x-flac; rate=8000; boundary=----------------------------9641e899ac92
------------------------------9641e899ac92
Content-Disposition: form-data; name="myfile"; filename="/tmp/voice/1400157667.6440-in.wav.flac"
Content-Type: application/octet-stream
fLaC..."......e..\......! ..{..!y>..7..............................( ...reference libFLAC 1.2.1 20070917.
...encoded binary data...
------------------------------9641e899ac92--

识别结果重复的响应:

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Disposition: attachment
Cache-Control: no-transform
X-Content-Type-Options: nosniff
Pragma: no-cache
Date: Thu, 15 May 2014 12:41:09 GMT
Server: S3 v1.0
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Alternate-Protocol: 80:quic
Transfer-Encoding: chunked
e
{"result":[]}    <--- first one
f8
{"result":[{"alternative":[{"transcript":"............","confidence":0.73531097},{"transcript":"................"},{"transcript":".............."},{"transcript":"................"},{"transcript":"............ .."}],"final":true}],"result_index":0}   <--- second one
0

为什么会发生?当我使用 API v1 时,它只有一个响应。网上其他v2的例子也只有一个。

非常感谢。

【问题讨论】:

  • 我没有调查这个问题。我不得不用“\n”符号分割整个响应,就像这样:$result=curl_exec ($ch); $result_arr = split("\n", $result); $result = $result_arr[1];
  • 我应该使用哪种类型的 API 密钥,浏览器还是服务器?取决于哪一个,我在创建它时应该使用什么设置?我都试过了,但是我得到一个 403 错误,说我的密钥无效。

标签: php http google-api speech-recognition


【解决方案1】:

首先,请确保您使用的语言提供扬声器分类。例如,对于哥伦比亚的西班牙语,Google 不提供说话者分类,但对于来自西班牙的西班牙语,它提供:

Language Support

此外,有时需要对音频稍作改动,使用ffmpeg 可以实现:

ffmpeg -i input.wav -ac 1 -ab 128k -filter:a volume=0.9 -filter:a equalizer=f=4000:t=h:w=200:g=-2 output.wav

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多