示例#1
0
def request_stream(data_stream, rate, interim_results=False):
    """Yields `StreamingRecognizeRequest`s constructed from a recording audio
    stream.

    Args:
        data_stream: A generator that yields raw audio data to send.
        rate: The sampling rate in hertz.
        interim_results: Whether to return intermediate results, before the
            transcription is finalized.
    """
    # The initial request must contain metadata about the stream, so the
    # server knows how to interpret it.
    recognition_config = cloud_speech_pb2.RecognitionConfig(
        # There are a bunch of config options you can specify. See
        # https://goo.gl/KPZn97 for the full list.
        encoding='LINEAR16',  # raw 16-bit signed LE samples
        sample_rate=rate,  # the rate in hertz
        # See http://g.co/cloud/speech/docs/languages
        # for a list of supported languages.
        language_code='en-US',  # a BCP-47 language tag
    )
    streaming_config = cloud_speech_pb2.StreamingRecognitionConfig(
        interim_results=interim_results,
        config=recognition_config,
    )

    yield cloud_speech_pb2.StreamingRecognizeRequest(
        streaming_config=streaming_config)

    for data in data_stream:
        # Subsequent requests can all just have the content
        yield cloud_speech_pb2.StreamingRecognizeRequest(audio_content=data)
示例#2
0
文件: xmic.py 项目: devming/Gaongilro
def request_stream(data_stream, rate):
    recognition_config = cloud_speech.RecognitionConfig(
        encoding='LINEAR16',
        sample_rate_hertz=rate, 
        language_code='ko-KR'
    )
    streaming_config = cloud_speech.StreamingRecognitionConfig(
        config=recognition_config
    )

    yield cloud_speech.StreamingRecognizeRequest(
        streaming_config=streaming_config)

    for data in data_stream:
        yield cloud_speech.StreamingRecognizeRequest(audio_content=data)
示例#3
0
    def _process(self):
        """sets up a streaming speech api request. And streams results into a queue."""
        self.client = speech_client.SpeechClient()
        self.config = cloud_speech_pb2.StreamingRecognitionConfig(
            config=cloud_speech_pb2.RecognitionConfig(
                encoding=self.encoding,
                sample_rate_hertz=self.rate,
                language_code=self.language),
            interim_results=True)

        requests = StreamingRequest(self.audio, self.config)
        streaming_resp = self.client.streaming_recognize(iter(requests))

        # This will block until self.audio is closed...which closes the streaming_recognize req
        for resp in streaming_resp:
            self.results.put(resp)