Skip to content

Commit 699de15

Browse files
sirtorrybshaffer
authored andcommitted
Speech Sep 2018 GA (GoogleCloudPlatform#718)
1 parent 44fcd05 commit 699de15

15 files changed

+322
-39
lines changed

speech/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ To run the Speech Samples:
5252
transcribe-async Transcribe an audio file asynchronously using Google Cloud Speech API
5353
transcribe-async-gcs Transcribe audio asynchronously from a Storage Object using Google Cloud Speech API
5454
transcribe-async-words Transcribe an audio file asynchronously and print word time offsets using Google Cloud Speech API
55+
transcribe-enhanced Transcribe an audio file with an enhanced model using Google Cloud Speech API
5556
transcribe-gcs Transcribe audio from a Storage Object using Google Cloud Speech API
57+
transcribe-model Transcribe an audio file, with selected model, using Google Cloud Speech API
58+
transcribe-punctuation Transcribe an audio file with proper punctuation, using Google Cloud Speech API
5659
transcribe-stream Transcribe a stream of audio using Google Cloud Speech API
5760
transcribe-words Transcribe an audio file and print word time offsets using Google Cloud Speech API
5861

speech/composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
22
"require": {
3-
"google/cloud-speech": "^0.11",
3+
"google/cloud-speech": "~0.17",
44
"google/cloud-storage": "^1.3.1",
55
"symfony/console": "^3.0"
66
},
77
"require-dev": {
8-
"phpunit/phpunit": "~4.8"
8+
"phpunit/phpunit": "^5",
9+
"google/cloud-tools": "^0.8.5"
910
},
1011
"autoload": {
1112
"psr-4": {
@@ -16,6 +17,9 @@
1617
"src/transcribe_async.php",
1718
"src/transcribe_async_gcs.php",
1819
"src/transcribe_async_words.php",
20+
"src/transcribe_auto_punctuation.php",
21+
"src/transcribe_enhanced_model.php",
22+
"src/transcribe_model_selection.php",
1923
"src/transcribe_sync.php",
2024
"src/transcribe_sync_gcs.php",
2125
"src/transcribe_sync_words.php"

speech/speech.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
$inputDefinition = new InputDefinition([
3030
new InputArgument('audio-file', InputArgument::REQUIRED, 'The audio file to transcribe'),
31+
new InputOption('model', null, InputOption::VALUE_REQUIRED, 'The model to use'),
3132
new InputOption('encoding', null, InputOption::VALUE_REQUIRED,
3233
'The encoding of the audio file. This is required if the encoding is ' .
3334
'unable to be determined. '
@@ -111,6 +112,55 @@
111112
]);
112113
});
113114

115+
$application->add(new Command('transcribe-model'))
116+
->setDefinition($inputDefinition)
117+
->setDescription('Transcribe an audio file, with selected model, using Google Cloud Speech API')
118+
->setHelp(<<
119+
The %command.name% command transcribes audio from a file, with the
120+
selected model, using the Google Cloud Speech API.
121+
122+
php %command.full_name% audio_file.wav model_name
123+
124+
EOF
125+
)
126+
->setCode(function (InputInterface $input, OutputInterface $output) {
127+
$audioFile = $input->getArgument('audio-file');
128+
$modelName = $input->getOption('model');
129+
transcribe_model_selection($audioFile, $modelName);
130+
});
131+
132+
$application->add(new Command('transcribe-enhanced'))
133+
->setDefinition($inputDefinition)
134+
->setDescription('Transcribe an audio file, with an enhanced model, using Google Cloud Speech API')
135+
->setHelp(<<
136+
The %command.name% command transcribes audio from a file, with an enhanced
137+
model, using the Google Cloud Speech API.
138+
139+
php %command.full_name% audio_file.wav model_name
140+
141+
EOF
142+
)
143+
->setCode(function (InputInterface $input, OutputInterface $output) {
144+
$path = $input->getArgument('audio-file');
145+
transcribe_enhanced_model($path);
146+
});
147+
148+
$application->add(new Command('transcribe-punctuation'))
149+
->setDefinition($inputDefinition)
150+
->setDescription('Transcribe an audio file, with proper punctuation, using Google Cloud Speech API')
151+
->setHelp(<<
152+
The %command.name% command transcribes audio from a file, with
153+
proper punctuation, using the Google Cloud Speech API.
154+
155+
php %command.full_name% audio_file.wav
156+
157+
EOF
158+
)
159+
->setCode(function (InputInterface $input, OutputInterface $output) {
160+
$path = $input->getArgument('audio-file');
161+
transcribe_auto_punctuation($path);
162+
});
163+
114164
$application->add(new Command('transcribe-async'))
115165
->setDefinition($inputDefinition)
116166
->setDescription('Transcribe an audio file asynchronously using Google Cloud Speech API')

speech/src/streaming_recognize.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* For instructions on how to run the full sample:
2020
*
21-
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/speech/api/README.md
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/speech/README.md
2222
*/
2323

2424
namespace Google\Cloud\Samples\Speech;
@@ -28,13 +28,13 @@
2828
use Google\Cloud\Speech\V1\RecognitionConfig;
2929
use Google\Cloud\Speech\V1\StreamingRecognitionConfig;
3030
use Google\Cloud\Speech\V1\StreamingRecognizeRequest;
31-
use Google\Cloud\Speech\V1\RecognitionConfig_AudioEncoding;
31+
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
3232

3333
/**
3434
* Transcribe an audio file using Google Cloud Speech API
3535
* Example:
3636
* ```
37-
* $audoEncoding = Google\Cloud\Speech\V1\RecognitionConfig_AudioEncoding::WAV
37+
* $audoEncoding = Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding::WAV
3838
* streaming_recognize('/path/to/audiofile.wav', 'en-US');
3939
* ```.
4040
*
@@ -59,7 +59,7 @@ function streaming_recognize($audioFile, $languageCode, $encoding, $sampleRateHe
5959
$config->setLanguageCode($languageCode);
6060
$config->setSampleRateHertz($sampleRateHertz);
6161
// encoding must be an enum, convert from string
62-
$encodingEnum = constant(RecognitionConfig_AudioEncoding::class . '::' . $encoding);
62+
$encodingEnum = constant(AudioEncoding::class . '::' . $encoding);
6363
$config->setEncoding($encodingEnum);
6464

6565
$strmConfig = new StreamingRecognitionConfig();

speech/src/transcribe_async.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* For instructions on how to run the full sample:
2020
*
21-
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/speech/api/README.md
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/speech/README.md
2222
*/
2323

2424
namespace Google\Cloud\Samples\Speech;

speech/src/transcribe_async_gcs.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* For instructions on how to run the full sample:
2020
*
21-
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/speech/api/README.md
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/speech/README.md
2222
*/
2323

2424
namespace Google\Cloud\Samples\Speech;

speech/src/transcribe_async_words.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* For instructions on how to run the full sample:
2020
*
21-
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/speech/api/README.md
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/speech/README.md
2222
*/
2323

2424
namespace Google\Cloud\Samples\Speech;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
/**
3+
* Copyright 2018 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/speech/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Speech;
25+
26+
# [START speech_transcribe_auto_punctuation]
27+
use Google\Cloud\Speech\V1\SpeechClient;
28+
use Google\Cloud\Speech\V1\RecognitionAudio;
29+
use Google\Cloud\Speech\V1\RecognitionConfig;
30+
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
31+
32+
/**
33+
* Transcribe the given audio file with auto punctuation enabled
34+
*/
35+
function transcribe_auto_punctuation($path)
36+
{
37+
// get contents of a file into a string
38+
$handle = fopen($path, 'r');
39+
$content = fread($handle, filesize($path));
40+
fclose($handle);
41+
42+
// set string as audio content
43+
$audio = (new RecognitionAudio())
44+
->setContent($content);
45+
46+
// set config
47+
$config = (new RecognitionConfig())
48+
->setEncoding(AudioEncoding::LINEAR16)
49+
->setSampleRateHertz(32000)
50+
->setLanguageCode('en-US')
51+
->setEnableAutomaticPunctuation(true);
52+
53+
// create the speech client
54+
$client = new SpeechClient();
55+
56+
// make the API call
57+
$response = $client->recognize($config, $audio);
58+
$results = $response->getResults();
59+
60+
// print results
61+
foreach ($results as $result) {
62+
$alternatives = $result->getAlternatives();
63+
$mostLikely = $alternatives[0];
64+
$transcript = $mostLikely->getTranscript();
65+
$confidence = $mostLikely->getConfidence();
66+
printf('Transcript: %s' . PHP_EOL, $transcript);
67+
printf('Confidence: %s' . PHP_EOL, $confidence);
68+
}
69+
70+
$client->close();
71+
}
72+
# [END speech_transcribe_auto_punctuation]
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
/**
3+
* Copyright 2018 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/speech/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Speech;
25+
26+
# [START speech_transcribe_enhanced_model]
27+
use Google\Cloud\Speech\V1\SpeechClient;
28+
use Google\Cloud\Speech\V1\RecognitionAudio;
29+
use Google\Cloud\Speech\V1\RecognitionConfig;
30+
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
31+
32+
/**
33+
* Transcribe the given audio file using an enhanced model
34+
*/
35+
function transcribe_enhanced_model($path)
36+
{
37+
// get contents of a file into a string
38+
$handle = fopen($path, 'r');
39+
$content = fread($handle, filesize($path));
40+
fclose($handle);
41+
42+
// set string as audio content
43+
$audio = (new RecognitionAudio())
44+
->setContent($content);
45+
46+
// set config
47+
$config = (new RecognitionConfig())
48+
->setEncoding(AudioEncoding::LINEAR16)
49+
->setSampleRateHertz(8000)
50+
->setLanguageCode('en-US')
51+
->setUseEnhanced(true)
52+
->setModel('phone_call');
53+
54+
// create the speech client
55+
$client = new SpeechClient();
56+
57+
// make the API call
58+
$response = $client->recognize($config, $audio);
59+
$results = $response->getResults();
60+
61+
// print results
62+
foreach ($results as $result) {
63+
$alternatives = $result->getAlternatives();
64+
$mostLikely = $alternatives[0];
65+
$transcript = $mostLikely->getTranscript();
66+
$confidence = $mostLikely->getConfidence();
67+
printf('Transcript: %s' . PHP_EOL, $transcript);
68+
printf('Confidence: %s' . PHP_EOL, $confidence);
69+
}
70+
71+
$client->close();
72+
}
73+
# [END speech_transcribe_enhanced_model]
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
/**
3+
* Copyright 2018 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/speech/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Speech;
25+
26+
# [START speech_transcribe_model_selection]
27+
use Google\Cloud\Speech\V1\SpeechClient;
28+
use Google\Cloud\Speech\V1\RecognitionAudio;
29+
use Google\Cloud\Speech\V1\RecognitionConfig;
30+
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
31+
32+
/**
33+
* Transcribe the given audio file synchronously with the selected model
34+
*/
35+
function transcribe_model_selection($speechFile, $model)
36+
{
37+
// get contents of a file into a string
38+
$handle = fopen($speechFile, 'r');
39+
$content = fread($handle, filesize($speechFile));
40+
fclose($handle);
41+
42+
// set string as audio content
43+
$audio = (new RecognitionAudio())
44+
->setContent($content);
45+
46+
// set config
47+
$config = (new RecognitionConfig())
48+
->setEncoding(AudioEncoding::LINEAR16)
49+
->setSampleRateHertz(32000)
50+
->setLanguageCode('en-US')
51+
->setModel($model);
52+
53+
// create the speech client
54+
$client = new SpeechClient();
55+
56+
// make the API call
57+
$response = $client->recognize($config, $audio);
58+
$results = $response->getResults();
59+
60+
// print results
61+
foreach ($results as $result) {
62+
$alternatives = $result->getAlternatives();
63+
$mostLikely = $alternatives[0];
64+
$transcript = $mostLikely->getTranscript();
65+
$confidence = $mostLikely->getConfidence();
66+
printf('Transcript: %s' . PHP_EOL, $transcript);
67+
printf('Confidence: %s' . PHP_EOL, $confidence);
68+
}
69+
70+
$client->close();
71+
}
72+
# [END speech_transcribe_model_selection]

speech/src/transcribe_sync.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* For instructions on how to run the full sample:
2020
*
21-
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/speech/api/README.md
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/speech/README.md
2222
*/
2323

2424
namespace Google\Cloud\Samples\Speech;

speech/src/transcribe_sync_gcs.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* For instructions on how to run the full sample:
2020
*
21-
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/speech/api/README.md
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/speech/README.md
2222
*/
2323

2424
namespace Google\Cloud\Samples\Speech;

0 commit comments

Comments
 (0)