How to make custom speech recognition in python? - python

I tried out following code:
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
print("Say something!")
audio = r.listen(source)
try:
text = r.recognize_google(audio)
print("You said: {}".format(text))
except:
print("Sorry")
This code works fine and print whatever i say.
But i want something like:
If someone says: i want to create a new article / create a new article /create article....
something like that
Then i want to get output like:
create a new article
Basically i want to know how to do this(steps) or what modules can help me.
Any help is appreciated

Here is the updated code #Nitin Singhal
import speech_recognition as sr
#Speech Recognition Module
r = sr.Recognizer()
#Initializing r as a Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise
print("Say Something")
audio = r.listen(source)
#Gets the Audio Input
try:
text = r.recognize_google(audio)
text = text.lower()
print("You said: {}".format(text))
#Recognizes the speech(Intenet required)
if text == "i want to create a new article" or "create a new article" or "create article":
print("Create new article")
#Displays the desired output
except:
print("Sorry")
#In case any error, prints this
You can give as many as statements like I gave under the if statement. This gives the desired output.

Related

Voice Recognition (converting voice to text)

I have code to convert voice to written text, I want to save the written text after it's converted to files that can be accessed later, how do I do it in the following code?
import speech_recognition as sr
def main():
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
print("Please say something to start recording the lecture ")
audio = r.listen(source)
print("Recognizing Now .... ")
# recognize speech using google
try:
print("You have said \n" + r.recognize_google(audio))
print("Audio Recorded Successfully \n ")
except Exception as e:
print("Error : " + str(e))
# write audio
with open("recorded.wav", "wb") as f:
f.write(audio.get_wav_data())
if __name__ == "__main__":
main()
I tried to create another python file and run it as .txt but it saves the code not recoreded one
In your def main(): function you need to open a text file using open(), something similar to as shown:
transcript = open('transcript.txt', 'w')
after that where you are using print("You have said \n" + r.recognize_google(audio))
You have to write the data coming from r.recognize_google(audio) to the above transcript.txt text file using transcript file descriptor
transcript.write(r.recognize_google(audio))
Also you need to make sure to close the open file descriptor using transcript.close()
Modified your code slightly to show how you can do it
import speech_recognition as sr
def main():
transcript = open('transcript.txt', 'w')
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
print("Please say something to start recording the lecture ")
audio = r.listen(source)
print("Recognizing Now .... ")
# recognize speech using google
try:
print("You have said \n" + r.recognize_google(audio))
transcript.write(r.recognize_google(audio))
print("Audio Recorded Successfully \n ")
except Exception as e:
print("Error : " + str(e))
# write audio
with open("recorded.wav", "wb") as f:
f.write(audio.get_wav_data())
f.close()
transcript.close()
if __name__ == "__main__":
main()

voice assistant that can open applications with the help of voice commands in python

Hey everyone,
I've been trying to build a voice assistant that can open applications with the help of voice commands in python. Since I'm just a starter I only tried it with google and Wikipedia but it's not working. It only opens Wikipedia, even when I'm telling him to open Google. Can anyone please help me with that? I am on a mac by the way.
Thank you very much
my code:
import speech_recognition as sr
import webbrowser
speech_engine = sr.Recognizer()
with sr.Microphone() as micro:
print("Recording...")
audio = speech_engine.record(micro, duration=5)
print("Recognizing...")
text = speech_engine.recognize_google(audio, language="de-DE")
print(text)
if speech_engine == "Open Google":
webbrowser.open("https.//google.com")
else:
webbrowser.open("https://wikipedia.org")´´´
In your code text is returing your ouput not speech_engine so you shoud change your condition and it will work!
import speech_recognition as sr
import webbrowser
speech_engine = sr.Recognizer()
with sr.Microphone() as micro:
print("Recording...")
audio = speech_engine.record(micro, duration=5)
print("Recognizing...")
text = speech_engine.recognize_google(audio, language="en")
print(text)
if text == "Open Google" or text=="Google":
webbrowser.open("https.//google.com")
else:
webbrowser.open("https://wikipedia.org")
Output:
Recording...
Recognizing...
Google
I don't really know if that is a good answer but I found out that the assistant has a problem with executing the command used in the first if-statement. All other commands in the elif-statements work fine. That is now my code:
import speech_recognition as sr
import webbrowser
speech_engine = sr.Recognizer()
with sr.Microphone() as micro:
print("Recording...")
audio = speech_engine.record(micro, duration=5)
print("Recognizing...")
text = speech_engine.recognize_google(audio, language="en")
print(text)
if text == 'come on':
print("I am sorry, Sir")
elif text == 'open Wikipedia':
webbrowser.open("https://wikipedia.org")
print("opening Wikipedia")
elif text == 'open Google':
webbrowser.open("https://google.com")
print("opening Google")
elif text == 'open stack overflow':
webbrowser.open("https://stackoverflow.com")
print("opening Google")
else:
print("Unrecognized Command")
´´´

Output of Speech Recognition (Python)

I am working on a project with speech recognition module and due to my different language, I want to save on text file so I can read and have proper output; but when I tried sys module to save the file it came with some errors. If you help me to fix this I`ll be appreciate
This is my code:
import speech_recognition as sr
import sys
r = sr.Recognizer()
print('How can I help you?')
def my_first_sr():
while True:
try:
with sr.Microphone() as mic:
r.adjust_for_ambient_noise(mic)
audio = r.listen(mic)
text = r.recognize_google(audio, language = 'fa-IR')
print(text)
except sr.UnknownValueError:
print('I didn`t understand!')
except sr.RequestError:
print('Sorry my service is down')
my_first_sr()
output = open('Speech Recognition.txt', 'w')
sys.stdout = output
print(text)
output.close()
You have to write the text to the file output:
import speech_recognition as sr
r = sr.Recognizer()
print('How can I help you?')
def my_first_sr():
while True:
try:
with sr.Microphone() as mic:
r.adjust_for_ambient_noise(mic)
audio = r.listen(mic)
text = r.recognize_google(audio, language = 'fa-IR')
print(text)
output = open('Speech Recognition.txt', 'w')
output.write(text)#Write the text to the file
output.close()
except sr.UnknownValueError:
print('I didn`t understand!')
except sr.RequestError:
print('Sorry my service is down')
my_first_sr()

Google Speech Recognition doesn't print the data as String

So, I wanted to write a program that recognizes the audio from the Microphone and prints it as a string. But Python didn't print the recorded audio as a string. I watched some tutorials and read some articles but nothing helped.
Can you help me?
This is my code:
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
try:
print("You said " + r.recognize_sphinx(audio))
except LookupError:
print("Could not understand audio")
Thanks!

demo how to Transcribe an audio file using the SpeechRecognition

I recently tried to learn how to transcribe an audio file, but I am not very familiar with python.
I have read the example from the SpeechRecognition from the following website
https://github.com/Uberi/speech_recognition/blob/master/examples/audio_transcribe.py
I try to use them using the following code:
However, it looks like I cannot import my file in my windows computer.
I wonder if I have a wav file in my computer with the path
"C:\Users\Chen\Downloads\english.wav"
and I tried to replace the file with "C:\Users\Chen\Downloads" in my python code.
But it shows me that
FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\Chen\english.wav'
Please help me to fix the problems.
import speech_recognition as sr
# obtain path to "english.wav" in the same folder as this script
from os import path
AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "english.wav")
# use the audio file as the audio source
r = sr.Recognizer()
with sr.AudioFile(AUDIO_FILE) as source:
audio = r.record(source) # read the entire audio file
print("Google Speech Recognition thinks you said " + r.recognize_google(audio))
Use function listen() if you need to recognize text
r = sr.Recognizer()
with sr.AudioFile(AUDIO_FILE) as source:
audio = r.listen(source) # read the entire audio file
text = r.recognize_google(audio)
print("Google Speech Recognition thinks you said " + text)
# Below code is for audio file in hindi
file = "hindi.wav"
with sr.AudioFile(file) as source:
audio = r.listen(source)
text = r.recognize_google(audio, language='hi-IN')
print("Text : " + text)

Categories