From 6d7455870b59052caadad2697647fccc31324326 Mon Sep 17 00:00:00 2001 From: minyoungjun Date: Sun, 10 Dec 2017 18:23:04 +0900 Subject: [PATCH] change --- .../cloud-client/transcribe_streaming_mic.py | 75 +++++++++++++------ 1 file changed, 52 insertions(+), 23 deletions(-) diff --git a/speech/cloud-client/transcribe_streaming_mic.py b/speech/cloud-client/transcribe_streaming_mic.py index eb6f6ded3dd..e50a757cbb8 100644 --- a/speech/cloud-client/transcribe_streaming_mic.py +++ b/speech/cloud-client/transcribe_streaming_mic.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#-*- coding: utf-8 -*- # Copyright 2017 Google Inc. All Rights Reserved. # @@ -25,50 +25,67 @@ python transcribe_streaming_mic.py """ -# [START import_libraries] from __future__ import division +import argparse import re import sys +import urllib +import urllib2 +from threading import Thread +from httplib import BadStatusLine + +import random +import unicodedata from google.cloud import speech from google.cloud.speech import enums from google.cloud.speech import types import pyaudio from six.moves import queue -# [END import_libraries] - -# Audio recording parameters -RATE = 16000 -CHUNK = int(RATE / 10) # 100ms +temp = '' +def open_website(url, ts, is_end): + global temp + print("temp" + temp) + if (ts != temp): + temp = ts + is_end + if is_end == 'false': + return urllib2.urlopen(url + urllib.urlencode({'is_end': is_end, 'q': ts}).encode('utf-8'), timeout=0.5) + else: + return urllib2.urlopen(url + urllib.urlencode({'is_end': is_end, 'q': ts}).encode('utf-8')) + else: + return True class MicrophoneStream(object): """Opens a recording stream as a generator yielding the audio chunks.""" - def __init__(self, rate, chunk): + def __init__(self, rate, chunk_size): self._rate = rate - self._chunk = chunk + self._chunk_size = chunk_size # Create a thread-safe buffer of audio data self._buff = queue.Queue() self.closed = True + # Some useful numbers + self._num_channels = 1 # API only supports mono for now + def __enter__(self): + self.closed = False + self._audio_interface = pyaudio.PyAudio() self._audio_stream = self._audio_interface.open( format=pyaudio.paInt16, # The API currently only supports 1-channel (mono) audio # https://goo.gl/z757pE - channels=1, rate=self._rate, - input=True, frames_per_buffer=self._chunk, + channels=self._num_channels, rate=self._rate, + input=True, frames_per_buffer=self._chunk_size, # Run the audio stream asynchronously to fill the buffer object. # This is necessary so that the input device's buffer doesn't # overflow while the calling thread makes network requests, etc. stream_callback=self._fill_buffer, ) - self.closed = False - return self def __exit__(self, type, value, traceback): @@ -80,7 +97,7 @@ def __exit__(self, type, value, traceback): self._buff.put(None) self._audio_interface.terminate() - def _fill_buffer(self, in_data, frame_count, time_info, status_flags): + def _fill_buffer(self, in_data, *args, **kwargs): """Continuously collect data from the audio stream, into the buffer.""" self._buff.put(in_data) return None, pyaudio.paContinue @@ -106,7 +123,6 @@ def generator(self): break yield b''.join(data) -# [END audio_stream] def listen_print_loop(responses): @@ -137,7 +153,8 @@ def listen_print_loop(responses): continue # Display the transcription of the top alternative. - transcript = result.alternatives[0].transcript + top_alternative = result.alternatives[0] + transcript = top_alternative.transcript # Display interim results, but with a carriage return at the end of the # line, so subsequent lines will overwrite them. @@ -146,14 +163,20 @@ def listen_print_loop(responses): # some extra spaces to overwrite the previous result overwrite_chars = ' ' * (num_chars_printed - len(transcript)) - if not result.is_final: + if (not result.is_final) and (len(transcript.split()) < 6) : + + #Thread(target=open_website, args=['http://localhost:3000/listen?', transcript.encode('utf-8'), 'false']).start() + print("buffer:" + transcript) + sys.stdout.write(transcript + overwrite_chars + '\r') sys.stdout.flush() num_chars_printed = len(transcript) else: + Thread(target=open_website, args=['http://localhost:3000/listen?', transcript.encode('utf-8'), 'true']).start() print(transcript + overwrite_chars) + print("end line") # Exit recognition if any of the transcribed phrases could be # one of our keywords. @@ -164,21 +187,22 @@ def listen_print_loop(responses): num_chars_printed = 0 -def main(): +def main(sample_rate): # See http://g.co/cloud/speech/docs/languages # for a list of supported languages. - language_code = 'en-US' # a BCP-47 language tag + language_code = 'ko-KR' # a BCP-47 language tag client = speech.SpeechClient() config = types.RecognitionConfig( encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16, - sample_rate_hertz=RATE, - language_code=language_code) + sample_rate_hertz=sample_rate, + language_code=language_code, + max_alternatives=1) streaming_config = types.StreamingRecognitionConfig( config=config, interim_results=True) - with MicrophoneStream(RATE, CHUNK) as stream: + with MicrophoneStream(sample_rate, int(sample_rate / 10)) as stream: audio_generator = stream.generator() requests = (types.StreamingRecognizeRequest(audio_content=content) for content in audio_generator) @@ -190,4 +214,9 @@ def main(): if __name__ == '__main__': - main() + parser = argparse.ArgumentParser( + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument('--rate', default=16000, help='Sample rate.', type=int) + args = parser.parse_args() + main(args.rate)