|
16 | 16 | */ |
17 | 17 |
|
18 | 18 | # [START speech_quickstart] |
19 | | -# Includes the autoloader for libraries installed with composer |
| 19 | +// Includes the autoloader for libraries installed with composer |
20 | 20 | require __DIR__ . '/vendor/autoload.php'; |
21 | 21 |
|
22 | | -# Imports the Google Cloud client library |
| 22 | +// Imports the Google Cloud client library |
23 | 23 | use Google\Cloud\Speech\V2\Client\SpeechClient; |
24 | | -use Google\Cloud\Speech\V2\ExplicitDecodingConfig\AudioEncoding; |
25 | | -use Google\Cloud\Speech\V2\RecognitionAudio; |
26 | 24 | use Google\Cloud\Speech\V2\RecognitionConfig; |
| 25 | +use Google\Cloud\Speech\V2\ExplicitDecodingConfig; |
| 26 | +use Google\Cloud\Speech\V2\Recognizer; |
27 | 27 | use Google\Cloud\Speech\V2\RecognizeRequest; |
| 28 | +use Google\Cloud\Speech\V2\CreateRecognizerRequest; |
| 29 | +use Google\Cloud\Speech\V2\GetRecognizerRequest; |
| 30 | +use Google\ApiCore\ApiException; |
28 | 31 |
|
29 | | -# The name of the audio file to transcribe |
30 | | -$gcsURI = 'gs://cloud-samples-data/speech/brooklyn_bridge.raw'; |
| 32 | +// The name of the audio file to transcribe |
| 33 | +$gcsUri = 'gs://cloud-samples-data/speech/brooklyn_bridge.raw'; |
31 | 34 |
|
32 | | -# set string as audio content |
33 | | -$audio = (new RecognitionAudio()) |
34 | | - ->setUri($gcsURI); |
| 35 | +// Populate these variables with your own values |
| 36 | +$projectId = 'YOUR_PROJECT_ID'; |
| 37 | +$recognizerId = 'speech-v2-recognizer-php-quickstart'; |
| 38 | +$location = 'global'; |
35 | 39 |
|
36 | | -# The audio file's encoding, sample rate and language |
37 | | -$config = new RecognitionConfig([ |
38 | | - 'encoding' => AudioEncoding::LINEAR16, |
39 | | - 'sample_rate_hertz' => 16000, |
40 | | - 'language_code' => 'en-US' |
41 | | -]); |
42 | | - |
43 | | -# Instantiates a client |
| 40 | +// Instantiates a client |
44 | 41 | $client = new SpeechClient(); |
45 | 42 |
|
46 | | -# Detects speech in the audio file |
| 43 | +// The name of the recognizer to create |
| 44 | +$recognizerName = $client->recognizerName($projectId, $location, $recognizerId); |
| 45 | +$getRecognizerRequest = (new GetRecognizerRequest())->setName($recognizerName); |
| 46 | + |
| 47 | +try { |
| 48 | + $recognizer = $client->getRecognizer($getRecognizerRequest); |
| 49 | +} catch (ApiException $e) { |
| 50 | + if ($e->getStatus() === 'NOT_FOUND') { |
| 51 | + // If the recognizer does not exist, create it. |
| 52 | + |
| 53 | + // Create an explicit decoding config because .raw files have no header. |
| 54 | + $explicitConfig = (new ExplicitDecodingConfig()) |
| 55 | + ->setEncoding(ExplicitDecodingConfig\AudioEncoding::LINEAR16) |
| 56 | + ->setSampleRateHertz(16000) |
| 57 | + ->setAudioChannelCount(1); // The brooklyn_bridge audio is single-channel (mono) |
| 58 | + |
| 59 | + $config = (new RecognitionConfig()) |
| 60 | + ->setLanguageCodes(['en-US']) |
| 61 | + ->setModel('long') // Or other models like 'telephony', 'medical_dictation' |
| 62 | + ->setExplicitDecodingConfig($explicitConfig); |
| 63 | + |
| 64 | + $recognizer = (new Recognizer()) |
| 65 | + ->setName($recognizerName) |
| 66 | + ->setDefaultRecognitionConfig($config); |
| 67 | + |
| 68 | + $createRecognizerRequest = (new CreateRecognizerRequest()) |
| 69 | + ->setParent($client->locationName($projectId, $location)) |
| 70 | + ->setRecognizer($recognizer) |
| 71 | + ->setRecognizerId($recognizerId); |
| 72 | + $operation = $client->createRecognizer($createRecognizerRequest); |
| 73 | + |
| 74 | + $operation->pollUntilComplete(); |
| 75 | + $recognizer = $operation->getResult(); |
| 76 | + printf('Created Recognizer: %s' . PHP_EOL, $recognizer->getName()); |
| 77 | + } else { |
| 78 | + throw $e; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// Detects speech in the audio file |
47 | 83 | $recognizeRequest = (new RecognizeRequest()) |
48 | | - ->setRecognizer($config); |
| 84 | + ->setRecognizer($recognizerName) |
| 85 | + ->setUri($gcsUri); |
49 | 86 | $response = $client->recognize($recognizeRequest); |
50 | 87 |
|
51 | | -# Print most likely transcription |
| 88 | +// Print most likely transcription |
52 | 89 | foreach ($response->getResults() as $result) { |
53 | 90 | $alternatives = $result->getAlternatives(); |
54 | 91 | $mostLikely = $alternatives[0]; |
|
0 commit comments