我想制作一个使用谷歌语音识别的简单脚本,我尝试了各种方法,以便我可以使用请求正文访问API,但总是失败。
示例网址:https : //content-speech.googleapis.com/v1/speech : recognize?prettyPrint=true&alt=json&key=AIzaSyD-XXXXXXXXXXXXXXXXXX-Al7hLQDbugrDcw
JSON 示例:
{
"audio": {
"content": "ZkxhQwAAACIEg....more......FzSi6SngVApeE="
},
"config": {
"encoding": "FLAC",
"sampleRateHertz": 16000,
"languageCode": "en-US"
}
}
Run Code Online (Sandbox Code Playgroud)
文档:https : //cloud.google.com/speech-to-text/docs/basics
代码示例:https : //cloud.google.com/speech-to-text/docs/samples
考虑使用库:
谷歌云客户端库 PHP(也有代码示例):https : //googlecloudplatform.github.io/google-cloud-php/#/docs/google-cloud/v0.61.0/speech/speechclient
Speech-to-Text 客户端库 https://cloud.google.com/speech-to-text/docs/reference/libraries#client-libraries-install-php
扬声器是您可以使用的 3.party 库:https : //github.com/duncan3dc/speaker
来自 google/cloud-speech 的快速示例:
安装:
composer require google/cloud-speech
Run Code Online (Sandbox Code Playgroud)
样本:
# Includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';
# Imports the Google Cloud client library
use Google\Cloud\Speech\SpeechClient;
# Your Google Cloud Platform project ID
$projectId = 'YOUR_PROJECT_ID';
# Instantiates a client
$speech = new SpeechClient([
'projectId' => $projectId,
'languageCode' => 'en-US',
]);
# The name of the audio file to transcribe
$fileName = __DIR__ . '/resources/audio.raw';
# The audio file's encoding and sample rate
$options = [
'encoding' => 'LINEAR16',
'sampleRateHertz' => 16000,
];
# Detects speech in the audio file
$results = $speech->recognize(fopen($fileName, 'r'), $options);
foreach ($results as $result) {
echo 'Transcription: ' . $result->alternatives()[0]['transcript'] . PHP_EOL;
}
Run Code Online (Sandbox Code Playgroud)