|
| 1 | +# Copyright 2022 Google Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +# [START speech_quickstart_v2] |
| 17 | +import io |
| 18 | + |
| 19 | +from google.cloud.speech_v2 import SpeechClient |
| 20 | +from google.cloud.speech_v2.types import cloud_speech |
| 21 | + |
| 22 | + |
| 23 | +def quickstart_v2(project_id, recognizer_id, audio_file): |
| 24 | + # Instantiates a client |
| 25 | + client = SpeechClient() |
| 26 | + |
| 27 | + request = cloud_speech.CreateRecognizerRequest( |
| 28 | + parent=f"projects/{project_id}/locations/global", |
| 29 | + recognizer_id=recognizer_id, |
| 30 | + recognizer=cloud_speech.Recognizer( |
| 31 | + language_codes=["en-US"], model="latest_long" |
| 32 | + ), |
| 33 | + ) |
| 34 | + |
| 35 | + # Creates a Recognizer |
| 36 | + operation = client.create_recognizer(request=request) |
| 37 | + recognizer = operation.result() |
| 38 | + |
| 39 | + # Reads a file as bytes |
| 40 | + with io.open(audio_file, "rb") as f: |
| 41 | + content = f.read() |
| 42 | + |
| 43 | + config = cloud_speech.RecognitionConfig(auto_decoding_config={}) |
| 44 | + |
| 45 | + request = cloud_speech.RecognizeRequest( |
| 46 | + recognizer=recognizer.name, config=config, content=content |
| 47 | + ) |
| 48 | + |
| 49 | + # Transcribes the audio into text |
| 50 | + response = client.recognize(request=request) |
| 51 | + |
| 52 | + for result in response.results: |
| 53 | + print("Transcript: {}".format(result.alternatives[0].transcript)) |
| 54 | + |
| 55 | + return response |
| 56 | + |
| 57 | + |
| 58 | +# [END speech_quickstart_v2] |
| 59 | + |
| 60 | + |
| 61 | +if __name__ == "__main__": |
| 62 | + quickstart_v2() |
0 commit comments