PHP Class Google\Cloud\Speech\SpeechClient

Find more information at the Google Cloud Speech docs. To enable better detection of encoding/sample rate values it is recommended to install the getID3() library by James Heinrich. Please note that using the library will require files to be temporarily stored on disk. To install add james-heinrich/getid3 to your composer require section or run the following: sh $ composer require james-heinrich/getid3 Example: use Google\Cloud\ServiceBuilder; $cloud = new ServiceBuilder(); $speech = $cloud->speech(); SpeechClient can be instantiated directly. use Google\Cloud\Speech\SpeechClient; $speech = new SpeechClient();
Inheritance: use trait Google\Cloud\ClientTrait
Datei anzeigen Open project: GoogleCloudPlatform/gcloud-php

Protected Properties

Property Type Description
$connection Google\Cloud\Speech\Connection\ConnectionInterface

Public Methods

Method Description
__construct ( array $config = [] ) Create a Speech client.
beginRecognizeOperation ( resource | string | StorageObject $audio, array $options = [] ) : Operation Runs a recognize request as an operation. Ideal when working with audio longer than approximately one minute. Requires polling of the returned operation in order to fetch results.
operation ( string $name ) : Operation Lazily instantiates an operation. There are no network requests made at this point. To see the operations that can be performed on an operation please see {@see Google\Cloud\Speech\Operation}.
recognize ( resource | string | StorageObject $audio, array $options = [] ) : array Runs a recognize request and returns the results immediately. Ideal when working with audio up to approximately one minute in length.

Private Methods

Method Description
analyzeAudio ( resource | string | StorageObject $audio ) : array | null Analyzes the provided audio using the getid3() library.
determineEncoding ( string $fileFormat ) : string | null Attempts to determine the encoding based on the file format.
determineSampleRate ( string $encoding ) : integer | null Attempts to determine the sample rate based on the encoding.
formatRequest ( resource | string | StorageObject $audio, array $options ) : array Formats the request for the API.
getTempResource ( resource | string $audio ) : resource Takes in a resource or string and makes sure it is available as a local file in order for the getID3 library to be able to analzye it.
isRemoteResource ( resource $audio ) : boolean Determines if the resource provided is remote.

Method Details

__construct() public method

Create a Speech client.
public __construct ( array $config = [] )
$config array [optional] { Configuration Options. @type string $projectId The project ID from the Google Developer's Console. @type CacheItemPoolInterface $authCache A cache for storing access tokens. **Defaults to** a simple in memory implementation. @type array $authCacheOptions Cache configuration options. @type callable $authHttpHandler A handler used to deliver Psr7 requests specifically for authentication. @type callable $httpHandler A handler used to deliver Psr7 requests. Only valid for requests sent over REST. @type string $keyFile The contents of the service account credentials .json file retrieved from the Google Developers Console. @type string $keyFilePath The full path to your service account credentials .json file retrieved from the Google Developers Console. @type int $retries Number of retries for a failed request. **Defaults to** `3`. @type array $scopes Scopes to be used for the request. }

beginRecognizeOperation() public method

The Google Cloud Client Library will attempt to infer the sample rate and encoding used by the provided audio file for you. This feature is recommended only if you are unsure of what the values may be and is currently limited to .flac, .amr, and .awb file types. The sample rate cannot be inferred from audio provided from a Google Storage object. For longer audio, up to approximately 80 minutes, you must use Google Cloud Storage objects as input. In addition to this restriction, only LINEAR16 audio encoding can be used for long audio inputs. Example: $operation = $speech->beginRecognizeOperation( fopen(__DIR__ . '/audio.flac', 'r') ); $isComplete = $operation->isComplete(); while (!$isComplete) { sleep(1); // let's wait for a moment... $operation->reload(); $isComplete = $operation->isComplete(); } print_r($operation->results()); Run with speech context, sample rate, and encoding provided $operation = $speech->beginRecognizeOperation( fopen(__DIR__ . '/audio.flac', 'r'), [ 'encoding' => 'FLAC', 'sampleRate' => 16000, 'speechContext' => [ 'phrases' => [ 'The Google Cloud Platform', 'Speech API' ] ] ]); $isComplete = $operation->isComplete(); while (!$isComplete) { sleep(1); // let's wait for a moment... $operation->reload(); $isComplete = $operation->isComplete(); } print_r($operation->results());
See also: https://cloud.google.com/speech/reference/rest/v1beta1/operations Operations
See also: https://cloud.google.com/speech/reference/rest/v1beta1/speech/asyncrecognize AsyncRecognize API documentation
See also: https://cloud.google.com/speech/reference/rest/v1beta1/RecognitionConfig#AudioEncoding AudioEncoding types
See also: https://cloud.google.com/speech/docs/best-practices Speech API best practices
public beginRecognizeOperation ( resource | string | StorageObject $audio, array $options = [] ) : Operation
$audio resource | string | Google\Cloud\Storage\StorageObject The audio to recognize. May be a resource, string of bytes, or Google Cloud Storage object.
$options array [optional] { Configuration options. @type int $sampleRate Sample rate in Hertz of the provided audio. Valid values are: 8000-48000. 16000 is optimal. For best results, set the sampling rate of the audio source to 16000 Hz. If that's not possible, use the native sample rate of the audio source (instead of re-sampling). **Defaults to** `8000` with .amr files and `16000` with .awb files. If the getID3 library has been installed this value will default to the value read from the file's headers (if it exists). @type string $encoding Encoding of the provided audio. May be one of `"LINEAR16"`, `"FLAC"`, `"MULAW"`, `"AMR"`, `"AMR_WB"`. **Defaults to** `"FLAC"` with .flac files, `"AMR"` with .amr files and `"AMR_WB"` with .awb files. @type int $maxAlternatives Maximum number of alternatives to be returned. Valid values are 1-30. **Defaults to** `1`. @type string $languageCode The language of the content. BCP-47 (e.g., `"en-US"`, `"es-ES"`) language codes are accepted. **Defaults to** `"en"` (English). @type bool $profanityFilter If set to `true`, the server will attempt to filter out profanities, replacing all but the initial character in each filtered word with asterisks, e.g. \"f***\". **Defaults to** `false`. @type array $speechContext Must contain a key `phrases` which is to be an array of strings which provide "hints" to the speech recognizer to favor specific words and phrases in the results. Please see [SpeechContext](https://cloud.google.com/speech/reference/rest/v1beta1/RecognitionConfig#SpeechContext) for more information. }
return Operation

operation() public method

Example: Access an existing operation by its server generated name. $operation = $speech->operation($operationName);
public operation ( string $name ) : Operation
$name string The name of the operation to request.
return Operation

recognize() public method

The Google Cloud Client Library will attempt to infer the sample rate and encoding used by the provided audio file for you. This feature is recommended only if you are unsure of what the values may be and is currently limited to .flac, .amr, and .awb file types. The sample rate cannot be inferred from audio provided from a Google Storage object. Example: $results = $speech->recognize( fopen(__DIR__ . '/audio.flac', 'r') ); foreach ($results as $result) { echo $result['transcript']; } Run with speech context, sample rate, and encoding provided $results = $speech->recognize( fopen(__DIR__ . '/audio.flac', 'r'), [ 'encoding' => 'FLAC', 'sampleRate' => 16000, 'speechContext' => [ 'phrases' => [ 'The Google Cloud Platform', 'Speech API' ] ] ]); foreach ($results as $result) { echo $result['transcript']; }
See also: https://cloud.google.com/speech/reference/rest/v1beta1/speech/syncrecognize#SpeechRecognitionAlternative SpeechRecognitionAlternative
See also: https://cloud.google.com/speech/reference/rest/v1beta1/speech/syncrecognize SyncRecognize API documentation
See also: https://cloud.google.com/speech/reference/rest/v1beta1/RecognitionConfig#AudioEncoding AudioEncoding types
See also: https://cloud.google.com/speech/docs/best-practices Speech API best practices
public recognize ( resource | string | StorageObject $audio, array $options = [] ) : array
$audio resource | string | Google\Cloud\Storage\StorageObject The audio to recognize. May be a resource, string of bytes, or Google Cloud Storage object.
$options array [optional] { Configuration options. @type int $sampleRate Sample rate in Hertz of the provided audio. Valid values are: 8000-48000. 16000 is optimal. For best results, set the sampling rate of the audio source to 16000 Hz. If that's not possible, use the native sample rate of the audio source (instead of re-sampling). **Defaults to** `8000` with .amr files and `16000` with .awb files. If the getID3 library has been installed this value will **default to** the value read from the file's headers (if they exists). @type string $encoding Encoding of the provided audio. May be one of `"LINEAR16"`, `"FLAC"`, `"MULAW"`, `"AMR"`, `"AMR_WB"`. **Defaults to** `"FLAC"` with .flac files, `"AMR"` with .amr files and `"AMR_WB"` with .awb files. @type int $maxAlternatives Maximum number of alternatives to be returned. Valid values are 1-30. **Defaults to** `1`. @type string $languageCode The language of the content. BCP-47 (e.g., `"en-US"`, `"es-ES"`) language codes are accepted. **Defaults to** `"en-US"` (English). @type bool $profanityFilter If set to `true`, the server will attempt to filter out profanities, replacing all but the initial character in each filtered word with asterisks, e.g. \"f***\". **Defaults to** `false`. @type array $speechContext Must contain a key `phrases` which is to be an array of strings which provide "hints" to the speech recognizer to favor specific words and phrases in the results. Please see [SpeechContext](https://cloud.google.com/speech/reference/rest/v1beta1/RecognitionConfig#SpeechContext) for more information. }
return array The transcribed results. Each element of the array contains a `transcript` key which holds the transcribed text. Optionally a `confidence` key holding the confidence estimate ranging from 0.0 to 1.0 may be present. `confidence` is typically provided only for the top hypothesis.

Property Details

$connection protected_oe property

protected ConnectionInterface,Google\Cloud\Speech\Connection $connection
return Google\Cloud\Speech\Connection\ConnectionInterface