google-cloud-php icon indicating copy to clipboard operation
google-cloud-php copied to clipboard

Speech-to-text API returns empty response

Open marvelefe opened this issue 4 years ago • 0 comments

Environment details

  • OS: MAC OS BIG SUR 11.0 BETA
  • PHP version: 7.4
  • Package name and version: "google/cloud-speech": "^1.4",

Steps to reproduce

  1. Setup a laravel project and install package version above
  2. Setup a controller method to send request to google api using the code sample from the docs here
  3. I'm trying to use the speech-to-text api on a file with a webm encoding so I'm using the V1p1beta1 package as the documentation states support for webm is still in beta mode.
  4. What I expect - A json object containing the transcription and confidence. What I get - An empty object (No errors or anything)
  5. I'm confident I followed all the required steps including authenticating with my key file and specifying the right uri to the audio file in my cloud storage bucket.
  6. The confusing part is the speech-to-text api requests count show up on my cloud console dashboard but I always get an empty response.

Code example

use Google\Cloud\Speech\V1p1beta1\SpeechClient;
use Google\Cloud\Speech\V1p1beta1\RecognitionAudio;
use Google\Cloud\Speech\V1p1beta1\RecognitionConfig\AudioEncoding;
use Google\Cloud\Speech\V1p1beta1\RecognitionConfig;
use Google\Cloud\Speech\V1p1beta1\StreamingRecognitionConfig;

class GoogleCloudSpeechToText {
    /**
     * Handle Conversion of Speech-To-Text
     *
     * @return void
    */
    public function convertSpeechToText($audioFilePath) {  
    $config = [
        'keyFilePath' => $serviceAccountPath,
        'projectId' => $projectId,
    ];
   $speechClient = new SpeechClient($config);
try {
    $encoding = AudioEncoding::ENCODING_UNSPECIFIED;
    $sampleRateHertz = 48000;
    $languageCode = 'en-NG';
    $config = new RecognitionConfig();
    $config->setEncoding($encoding);
    $config->setSampleRateHertz($sampleRateHertz);
    $config->setLanguageCode($languageCode);
    $uri = 'gs://bucket_name/name-of-my-file-.webm';
    $audio = new RecognitionAudio();
    $audio->setUri($uri);
    $response = $speechClient->recognize($config, $audio);
    return $response;
} finally {
    $speechClient->close();
}
    } 
}

Lest I forget, the file is just about a minute long and less than 4 mb and the audio recording is very clear.

I'd really appreciate some help. Thanks!

marvelefe avatar Sep 16 '21 21:09 marvelefe