Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion speech/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ API using the transcribe command:

```sh
php src/transcribe_sync.php test/data/audio32KHz.raw
php src/transcribe_async.php test/data/audio32KHz.raw
php src/transcribe_async_words.php test/data/audio32KHz.raw
```
## Troubleshooting
Expand Down
4 changes: 2 additions & 2 deletions speech/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"require": {
"google/cloud-speech": "^1.0.0",
"google/cloud-storage": "^1.20.1"
"google/cloud-speech": "^2.2",
"google/cloud-storage": "^1.36"
}
}
70 changes: 51 additions & 19 deletions speech/quickstart.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016 Google Inc.
* Copyright 2023 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,30 +20,64 @@
require __DIR__ . '/vendor/autoload.php';

# Imports the Google Cloud client library
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;

use Google\Cloud\Speech\V2\Client\SpeechClient;
use Google\Cloud\Speech\V2\CreateRecognizerRequest;
use Google\Cloud\Speech\V2\ExplicitDecodingConfig;
use Google\Cloud\Speech\V2\ExplicitDecodingConfig\AudioEncoding;
use Google\Cloud\Speech\V2\RecognitionConfig;
use Google\Cloud\Speech\V2\Recognizer;
use Google\Cloud\Speech\V2\RecognizeRequest;

# The name of the audio file to transcribe
$gcsURI = 'gs://cloud-samples-data/speech/brooklyn_bridge.raw';

# set string as audio content
$audio = (new RecognitionAudio())
->setUri($gcsURI);
# Your Google Cloud Project ID and location
$projectId = 'YOUR_PROJECT_ID';
$location = 'global';

# Instantiates a client
$speech = new SpeechClient();

# The audio file's encoding, sample rate and language
$config = new RecognitionConfig([
'encoding' => AudioEncoding::LINEAR16,
'sample_rate_hertz' => 16000,
'language_code' => 'en-US'
// Create a Recognizer
$createRecognizerRequest = new CreateRecognizerRequest([
'parent' => SpeechClient::locationName($projectId, $location),
'recognizer_id' => $recognizerId = 'quickstart-recognizer-' . uniqid(),
'recognizer' => new Recognizer([
'language_codes' => ['en-US'],
'model' => 'latest_short'
])
]);

# Instantiates a client
$client = new SpeechClient();
$operation = $speech->createRecognizer($createRecognizerRequest);

// Wait for the operation to complete
$operation->pollUntilComplete();
if ($operation->operationSucceeded()) {
$result = $operation->getResult();
printf('Created Recognizer: %s' . PHP_EOL, $result->getName());
} else {
print_r($operation->getError());
}

$config = (new RecognitionConfig())
// Can also use {@see Google\Cloud\Speech\V2\AutoDetectDecodingConfig}
// ->setAutoDecodingConfig(new AutoDetectDecodingConfig());

->setExplicitDecodingConfig(new ExplicitDecodingConfig([
'encoding' => AudioEncoding::LINEAR16,
'sample_rate_hertz' => 16000,
'audio_channel_count' => 1,
]));

$recognizerName = SpeechClient::recognizerName($projectId, $location, $recognizerId);
$request = (new RecognizeRequest())
->setRecognizer($recognizerName)
->setConfig($config)
->setUri($gcsURI);

# Detects speech in the audio file
$response = $client->recognize($config, $audio);
$response = $speech->recognize($request);

# Print most likely transcription
foreach ($response->getResults() as $result) {
Expand All @@ -53,6 +87,4 @@
printf('Transcript: %s' . PHP_EOL, $transcript);
}

$client->close();

# [END speech_quickstart]
$speech->close();
72 changes: 72 additions & 0 deletions speech/src/create_recognizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Copyright 2023 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Samples\Speech;

use Google\Cloud\Speech\V2\Client\SpeechClient;
use Google\Cloud\Speech\V2\CreateRecognizerRequest;
use Google\Cloud\Speech\V2\Recognizer;

/**
* Create a new recognizer.
*
* @param string $projectId The Google Cloud project ID.
* @param string $location The location of the recognizer.
* @param string $recognizerId The ID of the recognizer to create.
* @param string $model The recognizer model. Use "chirp_3" for diarization.
*/
function create_recognizer(
string $projectId,
string $location,
string $recognizerId,
string $model = "latest_short"
): void {
$apiEndpoint = $location === 'global' ? null : sprintf('%s-speech.googleapis.com', $location);
$speech = new SpeechClient(['apiEndpoint' => $apiEndpoint]);

// Create a Recognizer
$recognizer = new Recognizer([
'language_codes' => ['en-US'],
'model' => $model,
]);

// Create the CreateRecognizerRequest
$createRecognizerRequest = new CreateRecognizerRequest([
'parent' => SpeechClient::locationName($projectId, $location),
'recognizer_id' => $recognizerId,
'recognizer' => $recognizer
]);

// Call the createRecognizer method
$operation = $speech->createRecognizer($createRecognizerRequest);

// Wait for the operation to complete
$operation->pollUntilComplete();

if ($operation->operationSucceeded()) {
$result = $operation->getResult();
printf('Created Recognizer: %s' . PHP_EOL, $result->getName());
} else {
print_r($operation->getError());
}

$speech->close();
}

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
57 changes: 57 additions & 0 deletions speech/src/delete_recognizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright 2023 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Samples\Speech;

use Google\Cloud\Speech\V2\Client\SpeechClient;
use Google\Cloud\Speech\V2\DeleteRecognizerRequest;

/**
* Delete a recognizer.
*
* @param string $projectId The Google Cloud project ID.
* @param string $location The location of the recognizer.
* @param string $recognizerId The ID of the recognizer to delete.
*/
function delete_recognizer(string $projectId, string $location, string $recognizerId): void
{
$apiEndpoint = $location === 'global' ? null : sprintf('%s-speech.googleapis.com', $location);
$speech = new SpeechClient(['apiEndpoint' => $apiEndpoint]);

// Create the DeleteRecognizerRequest
$deleteRecognizerRequest = new DeleteRecognizerRequest([
'name' => SpeechClient::recognizerName($projectId, $location, $recognizerId)
]);

// Call the deleteRecognizer method
$operation = $speech->deleteRecognizer($deleteRecognizerRequest);

// Wait for the operation to complete
$operation->pollUntilComplete();

if ($operation->operationSucceeded()) {
printf('Deleted Recognizer: %s' . PHP_EOL, $deleteRecognizerRequest->getName());
} else {
print_r($operation->getError());
}

$speech->close();
}

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
50 changes: 27 additions & 23 deletions speech/src/multi_region_gcs.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2021 Google Inc.
* Copyright 2023 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,37 +18,43 @@
namespace Google\Cloud\Samples\Speech;

# [START speech_transcribe_with_multi_region_gcs]
# Imports the Google Cloud client library
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;
use Google\Cloud\Speech\V2\Client\SpeechClient;
use Google\Cloud\Speech\V2\RecognitionConfig;
use Google\Cloud\Speech\V2\ExplicitDecodingConfig;
use Google\Cloud\Speech\V2\ExplicitDecodingConfig\AudioEncoding;
use Google\Cloud\Speech\V2\RecognizeRequest;

/**
* @param string $projectId The Google Cloud project ID.
* @param string $location The location of the recognizer.
* @param string $recognizerId The ID of the recognizer to use (other than global).
* @param string $uri The Cloud Storage object to transcribe
* e.x. gs://cloud-samples-data/speech/brooklyn_bridge.raw
*/
function multi_region_gcs(string $uri)
function multi_region_gcs(string $projectId, string $location, string $recognizerId, string $uri)
{
# set string as audio content
$audio = (new RecognitionAudio())
->setUri($uri);
$options = ['apiEndpoint' => sprintf('%s-speech.googleapis.com', $location)];
$speech = new SpeechClient($options);

$recognizerName = SpeechClient::recognizerName($projectId, $location, $recognizerId);

# The audio file's encoding, sample rate and language
$config = new RecognitionConfig([
'encoding' => AudioEncoding::LINEAR16,
'sample_rate_hertz' => 16000,
'language_code' => 'en-US'
]);
$config = (new RecognitionConfig())
// Can also use {@see Google\Cloud\Speech\V2\AutoDetectDecodingConfig}
// ->setAutoDecodingConfig(new AutoDetectDecodingConfig());

# Specify a new endpoint.
$options = ['apiEndpoint' => 'eu-speech.googleapis.com'];
->setExplicitDecodingConfig(new ExplicitDecodingConfig([
'encoding' => AudioEncoding::LINEAR16,
'sample_rate_hertz' => 16000,
'audio_channel_count' => 1,
]));

# Instantiates a client
$client = new SpeechClient($options);
$request = (new RecognizeRequest())
->setRecognizer($recognizerName)
->setConfig($config)
->setUri($uri);

# Detects speech in the audio file
$response = $client->recognize($config, $audio);
$response = $speech->recognize($request);

# Print most likely transcription
foreach ($response->getResults() as $result) {
Expand All @@ -57,8 +63,6 @@ function multi_region_gcs(string $uri)
$transcript = $mostLikely->getTranscript();
printf('Transcript: %s' . PHP_EOL, $transcript);
}

$client->close();
}
# [END speech_transcribe_with_multi_region_gcs]

Expand Down
Loading
Loading