@@ -51,52 +51,54 @@ def make_channel(host, port):
51
51
return implementations .secure_channel (host , port , composite_channel )
52
52
53
53
54
- def main (input_uri , encoding , sample_rate ):
54
+ def main (input_uri , encoding , sample_rate , language_code = 'en-US' ):
55
55
channel = make_channel ('speech.googleapis.com' , 443 )
56
56
service = cloud_speech_pb2 .beta_create_Speech_stub (channel )
57
57
# The method and parameters can be inferred from the proto from which the
58
58
# grpc client lib was generated. See:
59
59
# https://github.com/googleapis/googleapis/blob/master/google/cloud/speech/v1beta1/cloud_speech.proto
60
- response = service .AsyncRecognize (cloud_speech_pb2 .AsyncRecognizeRequest (
60
+ operation = service .AsyncRecognize (cloud_speech_pb2 .AsyncRecognizeRequest (
61
61
config = cloud_speech_pb2 .RecognitionConfig (
62
62
# There are a bunch of config options you can specify. See
63
63
# https://goo.gl/KPZn97 for the full list.
64
64
encoding = encoding , # one of LINEAR16, FLAC, MULAW, AMR, AMR_WB
65
65
sample_rate = sample_rate , # the rate in hertz
66
- # See
67
- # https://g.co/cloud/speech/docs/best-practices#language_support
68
- # for a list of supported languages.
69
- language_code = 'en-US' , # a BCP-47 language tag
66
+ # See https://g.co/cloud/speech/docs/languages for a list of
67
+ # supported languages.
68
+ language_code = language_code , # a BCP-47 language tag
70
69
),
71
70
audio = cloud_speech_pb2 .RecognitionAudio (
72
71
uri = input_uri ,
73
72
)
74
73
), DEADLINE_SECS )
75
74
76
75
# Print the longrunning operation handle.
77
- print (response )
76
+ print (operation )
78
77
79
78
# Construct a long running operation endpoint.
80
79
service = operations_grpc_pb2 .beta_create_Operations_stub (channel )
81
80
82
- name = response .name
81
+ name = operation .name
83
82
84
83
while True :
85
84
# Give the server a few seconds to process.
86
85
print ('Waiting for server processing...' )
87
86
time .sleep (1 )
88
- # Get the long running operation with response.
89
- response = service .GetOperation (
87
+ operation = service .GetOperation (
90
88
operations_grpc_pb2 .GetOperationRequest (name = name ),
91
89
DEADLINE_SECS )
92
90
93
- if response .done :
91
+ if operation .done :
94
92
break
95
93
96
- # Print the recognition results.
97
- results = cloud_speech_pb2 .AsyncRecognizeResponse ()
98
- response .response .Unpack (results )
99
- print (results )
94
+ response = cloud_speech_pb2 .AsyncRecognizeResponse ()
95
+ operation .response .Unpack (response )
96
+ # Print the recognition result alternatives and confidence scores.
97
+ for result in response .results :
98
+ print ('Result:' )
99
+ for alternative in result .alternatives :
100
+ print (u' ({}): {}' .format (
101
+ alternative .confidence , alternative .transcript ))
100
102
101
103
102
104
def _gcs_uri (text ):
0 commit comments