1313# limitations under the License.
1414
1515"""Google Cloud Speech API sample that demonstrates multichannel recognition.
16-
17- Example usage:
18- python transcribe_multichannel.py resources/multi.wav
19- python transcribe_multichannel.py \
20- gs://cloud-samples-tests/speech/multi.wav
2116"""
2217
2318# [START speech_transcribe_multichannel]
24- import argparse
2519
2620from google .cloud import speech
2721
2822
29- def transcribe_file_with_multichannel (speech_file : str ) -> speech .RecognizeResponse :
30- """Transcribe the given audio file synchronously with
31- multi channel.
32-
23+ def transcribe_file_with_multichannel (audio_file : str ) -> speech .RecognizeResponse :
24+ """Transcribe the given audio file synchronously with multi channel.
3325 Args:
34- speech_file: A path to audio file to be recognized .
35-
26+ audio_file (str): Path to the local audio file to be transcribed .
27+ Example: "resources/multi.wav"
3628 Returns:
37- The RecognizeResponse results.
29+ cloud_speech. RecognizeResponse: The full response object which includes the transcription results.
3830 """
3931 client = speech .SpeechClient ()
4032
41- with open (speech_file , "rb" ) as audio_file :
42- content = audio_file .read ()
33+ with open (audio_file , "rb" ) as f :
34+ audio_content = f .read ()
4335
44- audio = speech .RecognitionAudio (content = content )
36+ audio = speech .RecognitionAudio (content = audio_content )
4537
4638 config = speech .RecognitionConfig (
4739 encoding = speech .RecognitionConfig .AudioEncoding .LINEAR16 ,
@@ -64,22 +56,20 @@ def transcribe_file_with_multichannel(speech_file: str) -> speech.RecognizeRespo
6456 # [END speech_transcribe_multichannel]
6557
6658
67- def transcribe_gcs_with_multichannel (gcs_uri : str ) -> speech .RecognizeResponse :
68- """Transcribe the given audio file on GCS with
69- multi channel.
70-
59+ def transcribe_gcs_with_multichannel (audio_uri : str ) -> speech .RecognizeResponse :
60+ """Transcribe the given audio file from Google Cloud Storage synchronously with multichannel.
7161 Args:
72- gcs_uri: A path to audio file to be recognized .
73-
62+ audio_uri (str): The Cloud Storage URI of the input audio .
63+ E.g., gs://cloud-samples-data/speech/multi.wav
7464 Returns:
75- The RecognizeResponse results.
65+ speech.RecognizeResponse: The full response object which includes the transcription results.
7666 """
7767 # [START speech_transcribe_multichannel_gcs]
7868 from google .cloud import speech
7969
8070 client = speech .SpeechClient ()
8171
82- audio = speech .RecognitionAudio (uri = gcs_uri )
72+ audio = speech .RecognitionAudio (uri = audio_uri )
8373
8474 config = speech .RecognitionConfig (
8575 encoding = speech .RecognitionConfig .AudioEncoding .LINEAR16 ,
@@ -103,12 +93,9 @@ def transcribe_gcs_with_multichannel(gcs_uri: str) -> speech.RecognizeResponse:
10393
10494
10595if __name__ == "__main__" :
106- parser = argparse .ArgumentParser (
107- description = __doc__ , formatter_class = argparse .RawDescriptionHelpFormatter
108- )
109- parser .add_argument ("path" , help = "File or GCS path for audio file to be recognized" )
110- args = parser .parse_args ()
111- if args .path .startswith ("gs://" ):
112- transcribe_gcs_with_multichannel (args .path )
96+ # It could be a local path like: path_to_file = "resources/multi.wav"
97+ path_to_file = "gs://cloud-samples-data/speech/multi.wav"
98+ if path_to_file .startswith ("gs://" ):
99+ transcribe_gcs_with_multichannel (path_to_file )
113100 else :
114- transcribe_file_with_multichannel (args . path )
101+ transcribe_file_with_multichannel (path_to_file )
0 commit comments