|
1 | 1 | <?php |
2 | 2 | /** |
3 | | - * Copyright 2016 Google Inc. |
| 3 | + * Copyright 2023 Google LLC. |
4 | 4 | * |
5 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
6 | 6 | * you may not use this file except in compliance with the License. |
|
20 | 20 | require __DIR__ . '/vendor/autoload.php'; |
21 | 21 |
|
22 | 22 | # Imports the Google Cloud client library |
23 | | -use Google\Cloud\Speech\V1\RecognitionAudio; |
24 | | -use Google\Cloud\Speech\V1\RecognitionConfig; |
25 | | -use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding; |
26 | | -use Google\Cloud\Speech\V1\SpeechClient; |
| 23 | + |
| 24 | +use Google\Cloud\Speech\V2\Client\SpeechClient; |
| 25 | +use Google\Cloud\Speech\V2\CreateRecognizerRequest; |
| 26 | +use Google\Cloud\Speech\V2\ExplicitDecodingConfig; |
| 27 | +use Google\Cloud\Speech\V2\ExplicitDecodingConfig\AudioEncoding; |
| 28 | +use Google\Cloud\Speech\V2\RecognitionConfig; |
| 29 | +use Google\Cloud\Speech\V2\Recognizer; |
| 30 | +use Google\Cloud\Speech\V2\RecognizeRequest; |
27 | 31 |
|
28 | 32 | # The name of the audio file to transcribe |
29 | 33 | $gcsURI = 'gs://cloud-samples-data/speech/brooklyn_bridge.raw'; |
30 | 34 |
|
31 | | -# set string as audio content |
32 | | -$audio = (new RecognitionAudio()) |
33 | | - ->setUri($gcsURI); |
| 35 | +# Your Google Cloud Project ID and location |
| 36 | +$projectId = 'YOUR_PROJECT_ID'; |
| 37 | +$location = 'global'; |
| 38 | + |
| 39 | +# Instantiates a client |
| 40 | +$speech = new SpeechClient(); |
34 | 41 |
|
35 | | -# The audio file's encoding, sample rate and language |
36 | | -$config = new RecognitionConfig([ |
37 | | - 'encoding' => AudioEncoding::LINEAR16, |
38 | | - 'sample_rate_hertz' => 16000, |
39 | | - 'language_code' => 'en-US' |
| 42 | +// Create a Recognizer |
| 43 | +$createRecognizerRequest = new CreateRecognizerRequest([ |
| 44 | + 'parent' => SpeechClient::locationName($projectId, $location), |
| 45 | + 'recognizer_id' => $recognizerId = 'quickstart-recognizer-' . uniqid(), |
| 46 | + 'recognizer' => new Recognizer([ |
| 47 | + 'language_codes' => ['en-US'], |
| 48 | + 'model' => 'latest_short' |
| 49 | + ]) |
40 | 50 | ]); |
41 | 51 |
|
42 | | -# Instantiates a client |
43 | | -$client = new SpeechClient(); |
| 52 | +$operation = $speech->createRecognizer($createRecognizerRequest); |
| 53 | + |
| 54 | +// Wait for the operation to complete |
| 55 | +$operation->pollUntilComplete(); |
| 56 | +if ($operation->operationSucceeded()) { |
| 57 | + $result = $operation->getResult(); |
| 58 | + printf('Created Recognizer: %s' . PHP_EOL, $result->getName()); |
| 59 | +} else { |
| 60 | + print_r($operation->getError()); |
| 61 | +} |
| 62 | + |
| 63 | +$recognitionConfig = (new RecognitionConfig()) |
| 64 | + ->setExplicitDecodingConfig(new ExplicitDecodingConfig([ |
| 65 | + 'encoding' => AudioEncoding::LINEAR16, |
| 66 | + 'sample_rate_hertz' => 16000, |
| 67 | + 'audio_channel_count' => 1, |
| 68 | + ])); |
| 69 | + |
| 70 | +$recognizerName = SpeechClient::recognizerName($projectId, $location, $recognizerId); |
| 71 | +$request = (new RecognizeRequest()) |
| 72 | + ->setRecognizer($recognizerName) |
| 73 | + ->setConfig($recognitionConfig) |
| 74 | + ->setUri($gcsURI); |
44 | 75 |
|
45 | 76 | # Detects speech in the audio file |
46 | | -$response = $client->recognize($config, $audio); |
| 77 | +$response = $speech->recognize($request); |
47 | 78 |
|
48 | 79 | # Print most likely transcription |
49 | 80 | foreach ($response->getResults() as $result) { |
|
53 | 84 | printf('Transcript: %s' . PHP_EOL, $transcript); |
54 | 85 | } |
55 | 86 |
|
56 | | -$client->close(); |
57 | | - |
58 | | -# [END speech_quickstart] |
| 87 | +$speech->close(); |
0 commit comments