我正在尝试转录用户从计算引擎实例上运行的webapp上传的音频文件,该实例可以访问使用composer安装的所有云API和客户端库。然而,在尝试使用PHP发送我的请求后,我没有从云语音API获得任何响应。谁能帮我弄清楚什么是am missing.Here是我的PHP代码,音频文件被定向到一个带有文件选择器的表单。我也试着从云存储中转录音频文件,但仍然没有得到任何东西
<?php
require __DIR__ . '/vendor/autoload.php';
use Google\Cloud\Speech\V1\SpeechClient;
use Google\Cloud\Speech\V1\RecognitionAudio;
use Google\Cloud\Speech\V1\RecognitionConfig;
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
$audioFile = $_FILES["my_audio"]; //from file picker
$encoding = AudioEncoding::LINEAR16;
$sampleRateHertz = 32000;
$languageCode = 'en-US';
$content = file_get_contents($audioFile);
$audio = (new RecognitionAudio())
->setContent($content);
$config = (new RecognitionConfig())
->setEncoding($encoding)
->setSampleRateHertz($sampleRateHertz)
->setLanguageCode($languageCode);
$client = new SpeechClient();
$operation = $client->longRunningRecognize($config, $audio);
$operation->pollUntilComplete();
if ($operation->operationSucceeded()) {
$response = $operation->getResult();
// each result is for a consecutive portion of the audio. iterate
// through them to get the transcripts for the entire audio file.
foreach ($response->getResults() as $result) {
$alternatives = $result->getAlternatives();
$mostLikely = $alternatives[0];
$transcript = $mostLikely->getTranscript();
$confidence = $mostLikely->getConfidence();
printf('Transcript: %s' . PHP_EOL, $transcript);
printf('Confidence: %s' . PHP_EOL, $confidence);
}
} else {
print_r($operation->getError());
}
$client->close();
?>
发布于 2020-07-11 08:43:29
$_FILES["my_audio"]
中没有绝对路径。既然你提到文件已经上传了,我想知道你是不是设置了这样的东西:
$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
另一方面,如果您没有收到来自Speech API服务的任何响应,这对我来说可能意味着请求没有启动/触发。您可以在file upload期间执行验证,或者使用$_FILES["pictures"]["error"]
检查错误,以查看是否一切正常。
如果问题仍然存在,即使使用$uri = 'gs://your-bucket-name/your-audio-file'
的全球控制系统,问题可能不是代码,在这种情况下,请详细说明您的用例中涉及的其他组件。你提到了作曲家,我想知道你是不是指云作曲家。
更新
我不是PHP专家,但我用this example解决了这个问题
在比较了你的文件之后,我发现你有:
$uri = "gs://speech-text-audio/h_ana1.flac";
$content = file_get_contents($uri);
$audio = (new RecognitionAudio())
->setContent($content);
当它对我起作用的时候:
$uri = 'gs://cloud-samples-tests/speech/brooklyn.flac';
$audio = (new RecognitionAudio())
->setUri($uri);
https://stackoverflow.com/questions/62017773
复制相似问题