17
17
"""Google Cloud Speech API sample application using the REST API for async
18
18
batch processing.
19
19
20
- Example usage: python transcribe_async.py resources/audio.raw
20
+ Example usage:
21
+ python transcribe_async.py resources/audio.raw
22
+ python transcribe_async.py gs://cloud-samples-tests/speech/brooklyn.flac
21
23
"""
22
24
23
- # [START import_libraries]
24
25
import argparse
25
26
import io
26
27
import time
27
- # [END import_libraries]
28
28
29
29
30
- def main (speech_file ):
31
- """Transcribe the given audio file asynchronously.
32
-
33
- Args:
34
- speech_file: the name of the audio file.
35
- """
36
- # [START authenticating]
37
- # Application default credentials provided by env variable
38
- # GOOGLE_APPLICATION_CREDENTIALS
30
+ def transcribe_file (speech_file ):
31
+ """Transcribe the given audio file asynchronously."""
39
32
from google .cloud import speech
40
33
speech_client = speech .Client ()
41
- # [END authenticating]
42
34
43
- # [START construct_request]
44
- # Loads the audio into memory
45
35
with io .open (speech_file , 'rb' ) as audio_file :
46
36
content = audio_file .read ()
47
37
audio_sample = speech_client .sample (
48
38
content ,
49
39
source_uri = None ,
50
40
encoding = 'LINEAR16' ,
51
41
sample_rate = 16000 )
52
- # [END construct_request]
53
42
54
- # [START send_request]
55
43
operation = speech_client .speech_api .async_recognize (audio_sample )
56
44
57
45
retry_count = 100
@@ -61,7 +49,7 @@ def main(speech_file):
61
49
operation .poll ()
62
50
63
51
if not operation .complete :
64
- print (" Operation not complete and retry limit reached." )
52
+ print (' Operation not complete and retry limit reached.' )
65
53
return
66
54
67
55
alternatives = operation .results
@@ -71,13 +59,44 @@ def main(speech_file):
71
59
# [END send_request]
72
60
73
61
74
- # [START run_application]
62
+ def transcribe_gcs (gcs_uri ):
63
+ """Asynchronously transcribes the audio file specified by the gcs_uri."""
64
+ from google .cloud import speech
65
+ speech_client = speech .Client ()
66
+
67
+ audio_sample = speech_client .sample (
68
+ content = None ,
69
+ source_uri = gcs_uri ,
70
+ encoding = 'FLAC' ,
71
+ sample_rate = 16000 )
72
+
73
+ operation = speech_client .speech_api .async_recognize (audio_sample )
74
+
75
+ retry_count = 100
76
+ while retry_count > 0 and not operation .complete :
77
+ retry_count -= 1
78
+ time .sleep (2 )
79
+ operation .poll ()
80
+
81
+ if not operation .complete :
82
+ print ('Operation not complete and retry limit reached.' )
83
+ return
84
+
85
+ alternatives = operation .results
86
+ for alternative in alternatives :
87
+ print ('Transcript: {}' .format (alternative .transcript ))
88
+ print ('Confidence: {}' .format (alternative .confidence ))
89
+ # [END send_request_gcs]
90
+
91
+
75
92
if __name__ == '__main__' :
76
93
parser = argparse .ArgumentParser (
77
94
description = __doc__ ,
78
95
formatter_class = argparse .RawDescriptionHelpFormatter )
79
96
parser .add_argument (
80
- 'speech_file ' , help = 'Full path of audio file to be recognized' )
97
+ 'path ' , help = 'File or GCS path for audio file to be recognized' )
81
98
args = parser .parse_args ()
82
- main (args .speech_file )
83
- # [END run_application]
99
+ if args .path .startswith ('gs://' ):
100
+ transcribe_gcs (args .path )
101
+ else :
102
+ transcribe_file (args .path )
0 commit comments