|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright 2019 Google LLC |
| 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 | +# https://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 | +# DO NOT EDIT! This is a generated sample ("LongRunningPromise", "speech_transcribe_async") |
| 18 | + |
| 19 | +# To install the latest published package dependency, execute the following: |
| 20 | +# pip install google-cloud-speech |
| 21 | + |
| 22 | +# sample-metadata |
| 23 | +# title: Transcribe Audio File using Long Running Operation (Local File) (LRO) |
| 24 | +# description: Transcribe a long audio file using asynchronous speech recognition |
| 25 | +# usage: python3 samples/v1/speech_transcribe_async.py [--local_file_path "resources/brooklyn_bridge.raw"] |
| 26 | + |
| 27 | +# [START speech_transcribe_async] |
| 28 | +from google.cloud import speech_v1 |
| 29 | +from google.cloud.speech_v1 import enums |
| 30 | +import io |
| 31 | + |
| 32 | + |
| 33 | +def sample_long_running_recognize(local_file_path): |
| 34 | + """ |
| 35 | + Transcribe a long audio file using asynchronous speech recognition |
| 36 | +
|
| 37 | + Args: |
| 38 | + local_file_path Path to local audio file, e.g. /path/audio.wav |
| 39 | + """ |
| 40 | + |
| 41 | + client = speech_v1.SpeechClient() |
| 42 | + |
| 43 | + # local_file_path = 'resources/brooklyn_bridge.raw' |
| 44 | + |
| 45 | + # The language of the supplied audio |
| 46 | + language_code = "en-US" |
| 47 | + |
| 48 | + # Sample rate in Hertz of the audio data sent |
| 49 | + sample_rate_hertz = 16000 |
| 50 | + |
| 51 | + # Encoding of audio data sent. This sample sets this explicitly. |
| 52 | + # This field is optional for FLAC and WAV audio formats. |
| 53 | + encoding = enums.RecognitionConfig.AudioEncoding.LINEAR16 |
| 54 | + config = { |
| 55 | + "language_code": language_code, |
| 56 | + "sample_rate_hertz": sample_rate_hertz, |
| 57 | + "encoding": encoding, |
| 58 | + } |
| 59 | + with io.open(local_file_path, "rb") as f: |
| 60 | + content = f.read() |
| 61 | + audio = {"content": content} |
| 62 | + |
| 63 | + operation = client.long_running_recognize(config, audio) |
| 64 | + |
| 65 | + print(u"Waiting for operation to complete...") |
| 66 | + response = operation.result() |
| 67 | + |
| 68 | + for result in response.results: |
| 69 | + # First alternative is the most probable result |
| 70 | + alternative = result.alternatives[0] |
| 71 | + print(u"Transcript: {}".format(alternative.transcript)) |
| 72 | + |
| 73 | + |
| 74 | +# [END speech_transcribe_async] |
| 75 | + |
| 76 | + |
| 77 | +def main(): |
| 78 | + import argparse |
| 79 | + |
| 80 | + parser = argparse.ArgumentParser() |
| 81 | + parser.add_argument( |
| 82 | + "--local_file_path", type=str, default="resources/brooklyn_bridge.raw" |
| 83 | + ) |
| 84 | + args = parser.parse_args() |
| 85 | + |
| 86 | + sample_long_running_recognize(args.local_file_path) |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + main() |
0 commit comments