Voice to Text with Flask
This is a web app made with Python Flask which converts the users voice to text and displays it. It also gives the reply with voice of what we have spoke. Here, i have showed the app.py code . This is the Backend of our voice to text web app.
To download the html and css files , go to - View files and open static and templates folder which is having our front end part.
Code on : Voice to Text
First we have to install the required library :
pip install SpeechRecognition==1.2.3
pip install pyttsx3
pip install Flask
## create a file name with app.py or you can give any name.
from flask import Flask , render_template , request
import speech_recognition as sr
import pyttsx3
app = Flask(__name__)
# home page
@app.route('/')
def home():
return render_template('home.html')
# Function works after user clicks the mic icon.
@app.route('/speech' , methods=["POST"])
def speech():
if request.method == "POST":
r = sr.Recognizer()
#this converts voice to text
engine = pyttsx3.init()
with sr.Microphone() as source:
audio = r.listen(source)
try:
text = r.recognize_google(audio)
result = text
rate = engine.getProperty("rate")
engine.setProperty("rate",100)
engine.say(text)
engine.runAndWait()
return render_template('home.html', result=result)
except:
error = "Sorry, can't read"
engine.say(error)
engine.runAndWait()
return render_template('home.html', error=error)
else:
return render_template('home.html')
if __name__ == '__main__':
app.run(debug=True)