@@ -106,6 +106,97 @@ def async_recognize(self, sample, language_code=None,
106
106
107
107
return Operation .from_pb (response , self )
108
108
109
+ def streaming_recognize (self , sample , language_code = None ,
110
+ max_alternatives = None , profanity_filter = None ,
111
+ speech_context = None , single_utterance = False ,
112
+ interim_results = False ):
113
+ """Streaming speech recognition.
114
+
115
+ .. note::
116
+
117
+ Streaming recognition requests are limited to 1 minute of audio.
118
+ See: https://cloud.google.com/speech/limits#content
119
+
120
+ Yields :class:`~streaming_response.StreamingSpeechResponse` containing
121
+ results and metadata from the streaming request.
122
+
123
+ :type sample: :class:`~google.cloud.speech.sample.Sample`
124
+ :param sample: Instance of ``Sample`` containing audio information.
125
+
126
+ :type language_code: str
127
+ :param language_code: (Optional) The language of the supplied audio as
128
+ BCP-47 language tag. Example: ``'en-GB'``.
129
+ If omitted, defaults to ``'en-US'``.
130
+
131
+ :type max_alternatives: int
132
+ :param max_alternatives: (Optional) Maximum number of recognition
133
+ hypotheses to be returned. The server may
134
+ return fewer than maxAlternatives.
135
+ Valid values are 0-30. A value of 0 or 1
136
+ will return a maximum of 1. Defaults to 1
137
+
138
+ :type profanity_filter: bool
139
+ :param profanity_filter: If True, the server will attempt to filter
140
+ out profanities, replacing all but the
141
+ initial character in each filtered word with
142
+ asterisks, e.g. ``'f***'``. If False or
143
+ omitted, profanities won't be filtered out.
144
+
145
+ :type speech_context: list
146
+ :param speech_context: A list of strings (max 50) containing words and
147
+ phrases "hints" so that the speech recognition
148
+ is more likely to recognize them. This can be
149
+ used to improve the accuracy for specific words
150
+ and phrases. This can also be used to add new
151
+ words to the vocabulary of the recognizer.
152
+
153
+ :type single_utterance: bool
154
+ :param single_utterance: (Optional) If false or omitted, the recognizer
155
+ will perform continuous recognition
156
+ (continuing to process audio even if the user
157
+ pauses speaking) until the client closes the
158
+ output stream (gRPC API) or when the maximum
159
+ time limit has been reached. Multiple
160
+ SpeechRecognitionResults with the is_final
161
+ flag set to true may be returned.
162
+ If true, the recognizer will detect a single
163
+ spoken utterance. When it detects that the
164
+ user has paused or stopped speaking, it will
165
+ return an END_OF_UTTERANCE event and cease
166
+ recognition. It will return no more than one
167
+ SpeechRecognitionResult with the is_final flag
168
+ set to true.
169
+
170
+ :type interim_results: bool
171
+ :param interim_results: (Optional) If true, interim results (tentative
172
+ hypotheses) may be returned as they become
173
+ available (these interim results are indicated
174
+ with the is_final=false flag). If false or
175
+ omitted, only is_final=true result(s) are
176
+ returned.
177
+
178
+ :raises: :class:`ValueError` if sample.content is not a file-like
179
+ object. :class:`ValueError` if stream has closed.
180
+
181
+ :rtype: :class:`~google.cloud.grpc.speech.v1beta1\
182
+ .cloud_speech_pb2.StreamingRecognizeResponse`
183
+ :returns: ``StreamingRecognizeResponse`` instances.
184
+ """
185
+ if getattr (sample .content , 'closed' , None ) is None :
186
+ raise ValueError ('Please use file-like object for data stream.' )
187
+ if sample .content .closed :
188
+ raise ValueError ('Stream is closed.' )
189
+
190
+ requests = _stream_requests (sample , language_code = language_code ,
191
+ max_alternatives = max_alternatives ,
192
+ profanity_filter = profanity_filter ,
193
+ speech_context = speech_context ,
194
+ single_utterance = single_utterance ,
195
+ interim_results = interim_results )
196
+ api = self ._gapic_api
197
+ responses = api .streaming_recognize (requests )
198
+ return responses
199
+
109
200
def sync_recognize (self , sample , language_code = None , max_alternatives = None ,
110
201
profanity_filter = None , speech_context = None ):
111
202
"""Synchronous Speech Recognition.
0 commit comments