cathat
cathat

Reputation: 21

How to send result of google speech recognition to web client from python server

I have written a web that uses a button to call google speech recognition API in a python flask server to do speech recognition on mic and show the results on the webpage. I tried post and socket.io but failed. How can I do it? The web client:

var socket = io.connect();
$('#start_speech').click(function(){
        socket.emit('speech_start');
      });

socket.on('speech_result', function(msg){
        socket.emit('speech_result')
        console.log(msg.data);
        $('#interim-span').html(msg.data);        
      });

The speech recognition function:

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)

@socketio.on('speech_start')
def speech_recognition():
    RATE = 16000
    CHUNK = int(RATE / 10)  # 100ms
    language_code = 'en-US'  # a BCP-47 language tag

    client = speech.SpeechClient()
    config = types.RecognitionConfig(
        encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
        sample_rate_hertz=RATE,
        language_code=language_code,
        enable_word_time_offsets=True)
    streaming_config = types.StreamingRecognitionConfig(
        config=config,
        interim_results=True)

    with MicrophoneStream(RATE, CHUNK) as stream:
        audio_generator = stream.generator()
        requests = (types.StreamingRecognizeRequest(audio_content=content)
                    for content in audio_generator)

        responses = client.streaming_recognize(streaming_config, requests)

        listen_loop(responses)



def listen_loop(responses):
            print("begin loop")
            for response in responses:
                if not response.results:
                    continue
                result = response.results[0]
                if not result.alternatives:
                    continue
                alternative = result.alternatives[0]
                transcript = alternative.transcript
                if not result.is_final:
                    print(transcript)
                    socketio.emit('speech_result', {data: speech})    
                else:
                    print(transcript)
                    socketio.emit('speech_result', {data: speech})
                    if re.search(r'\b(exit|quit)\b', transcript, re.I):
                        print('Exiting..')
                        break

According to the document, the responses passed is a generator that will block until a response is provided by the server. I first tried post and return the result in the loop, but I got a "ValueError: View function did not return a response".

Then I googled the problem and tried socketio. The speech_recognition function was called that I can see the results in console, but the speech_result did't receive any message. I think when it's in a connection of speech_start, it can't connect to others?

Actually I found a demo in google speech webpage which shows the result of speech recognition in real time but I don't know how they did it. So it's a problem of socketio or I should try other ways?

Upvotes: 1

Views: 1181

Answers (1)

cathat
cathat

Reputation: 21

I have solved this problem. The point is to use eventlet.sleep to flush the socket.

eventlet.spawn(speech_recognition)

And in function speech_recognition:

socketio.emit('server_response', {'data': transcript})
eventlet.sleep(0.2)

Upvotes: 1

Related Questions