Skip to content

Instantly share code, notes, and snippets.

@urodelagames
Created July 18, 2021 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urodelagames/6ad8a429e73d114e64467988854e8eb0 to your computer and use it in GitHub Desktop.
Save urodelagames/6ad8a429e73d114e64467988854e8eb0 to your computer and use it in GitHub Desktop.
This is the speech-to-text Flask app that receives requests from the Phonograph game I made for Godot Wild Jam #35.
from flask import Flask, request
import speech_recognition as sr
import uuid
import wave
app = Flask(__name__)
@app.route('/', methods = ['GET'])
def index():
return "hello world"
@app.route('/stt_test', methods = ['GET'])
def stt_test():
return get_text_from_speech()
@app.route('/stt', methods = ['GET'])
def get_stt():
data = bytes(bytearray.fromhex(request.get_data(as_text=True)))
return get_text_from_speech(data)
def get_text_from_speech(data):
r = sr.Recognizer()
id = str(uuid.uuid4())
wav_file_name = "{}.wav".format(id)
with wave.open(wav_file_name, "wb") as wav_file:
wav_file.setnchannels(2)
wav_file.setsampwidth(2)
wav_file.setframerate(44100)
wav_file.writeframesraw(data)
audio_file = sr.AudioFile(wav_file_name)
with audio_file as source:
audio = r.record(source)
text = r.recognize_google(audio)
# text = r.recognize_sphinx(audio)
return text
if __name__ == '__main__':
app.run(port = 5000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment