@@ -54,6 +54,11 @@ def main(input_uri, encoding, sample_rate, language_code='ja-JP'):
54
54
# grpc client lib was generated. See:
55
55
# https://github.com/googleapis/googleapis/blob/master/google/cloud/speech/v1/cloud_speech.proto
56
56
start = time .time ()
57
+ if input_uri .startswith ('gs://' ):
58
+ audio = cloud_speech_pb2 .RecognitionAudio (uri = input_uri )
59
+ else :
60
+ with open (input_uri , 'rb' ) as f :
61
+ audio = cloud_speech_pb2 .RecognitionAudio (content = f .read ())
57
62
response = service .Recognize (cloud_speech_pb2 .RecognizeRequest (
58
63
config = cloud_speech_pb2 .RecognitionConfig (
59
64
# There are a bunch of config options you can specify. See
@@ -64,11 +69,10 @@ def main(input_uri, encoding, sample_rate, language_code='ja-JP'):
64
69
# supported languages.
65
70
language_code = language_code , # a BCP-47 language tag
66
71
),
67
- audio = cloud_speech_pb2 .RecognitionAudio (
68
- uri = input_uri ,
69
- )
72
+ audio = audio
70
73
), DEADLINE_SECS )
71
74
print ('Time: %s' % (time .time () - start ))
75
+ print (response )
72
76
73
77
# Print the recognition result alternatives and confidence scores.
74
78
for result in response .results :
@@ -91,13 +95,13 @@ def _gcs_uri(text):
91
95
parser = argparse .ArgumentParser (
92
96
description = __doc__ ,
93
97
formatter_class = argparse .RawDescriptionHelpFormatter )
94
- parser .add_argument ('input_uri' , type = _gcs_uri )
98
+ parser .add_argument ('filename' )
95
99
parser .add_argument (
96
100
'--encoding' , default = 'LINEAR16' , choices = [
97
- 'LINEAR16' , 'FLAC' , 'MULAW' , 'AMR' , 'AMR_WB' , 'SPEEX_WITH_HEADER_BYTE' ],
101
+ 'LINEAR16' , 'FLAC' , 'MULAW' , 'AMR' , 'AMR_WB' , 'SPEEX_WITH_HEADER_BYTE' , 'OGG_OPUS' ],
98
102
help = 'How the audio file is encoded. See {}#L67' .format (PROTO_URL ))
99
103
parser .add_argument ('--sample_rate' , type = int , default = 16000 )
100
104
parser .add_argument ('--lang' , default = 'en-US' )
101
105
102
106
args = parser .parse_args ()
103
- main (args .input_uri , args .encoding , args .sample_rate , args .lang )
107
+ main (args .filename , args .encoding , args .sample_rate , args .lang )
0 commit comments