我们仅在移动浏览器中将您的Google语音搜索API用于网络应用。
我的问题是Google的重新处理非常缓慢。
我正在使用PHP和jQuery库来渲染Google的语音搜索。 用户点击前端的语音图标,然后用麦克风说出任何单词,然后与Google语音API相连,然后将语音转换为文本,然后从网站上搜索该文本。 Google的这一过程要花很多时间。
让我知道是否有人可以提出解决方案以加快处理速度?
是正常现象还是我们做错了事?
<?php
header('Access-Control-Allow-Origin: *');
/**
* Created by PhpStorm.
* User: evg
* Date: 23.05.18
* Time: 2:53
*/
# Includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';
# Imports the Google Cloud client library
use Google\Cloud\Speech\SpeechClient;
//logdata('---------'.date("Y-m-d H:i:s").'---------');
//logdata(var_export($_SERVER, true));
if (isset($_POST['sound'])) {
$data = $_POST['sound'];
try {
$ret = request($data);
echo json_encode(['error' => 0, 'data' => $ret ]);
}
catch (Exception $e) {
logdata('error: '. $e->getMessage());
echo json_encode(['error' => 1, 'message' => $e->getMessage()]);
}
} else {
logdata('error: '. 'Data missed');
echo json_encode(['error' => 1, 'message' => 'Data missed']);
}
function logdata($data = 'no data was sent')
{
$fname = 'requestlog.txt';
file_put_contents($fname, $data."\r\n", FILE_APPEND);
}
function request($data)
{
# Your Google Cloud Platform project ID
$projectId = '';
# Instantiates a client
$speech = new SpeechClient([
'projectId' => $projectId,
'languageCode' => 'en-IN',
'keyFilePath' => 'keyfile.json'
]);
# The audio file's encoding and sample rate
$options = ['encoding' => 'LINEAR16'];
# Detects speech in the audio file
// file_put_contents('123.wav', base64_decode($data));
# Detects speech in the audio file
$results = $speech->recognize(base64_decode($data), $options);
//$e = var_export($results, true);
// logdata('---results---: ');
// logdata($e);
if ( ! empty($results)) { return $results[0]->alternatives()[0]['transcript']; }
throw new Exception('Unable to transcript');
}
processor.php
Displaying processor.php.
?>