Output of Speech Recognition (Python) - 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()

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")
´´´

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!

How to make custom speech recognition in 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.

not converting more the 10 sec of audio (wav) file

getting results but it only converts the 10 sec of audio to text not more than that i tried with different audio files
import speech_recognition as sr
r = sr.Recognizer()
with sr.AudioFile("E:/g.wav") as source:
audio = r.record(source)
try:
s = r.recognize_google(audio).count(60)
print("Text: "+s)
except Exception as e:
print("Exception: "+str(e))
please help
I think there is a mistake in the code you provided. I imagine what you wanted was
import speech_recognition as sr
r = sr.Recognizer()
with sr.AudioFile("E:/g.wav") as source:
audio = r.record(source)
try:
s = r.recognize_google(audio)
print("Text: " + s)
except Exception as e:
print("Exception: "+ str(e))
I tried running this with this sample file and it recognized the entire thing, not just the first 10 seconds, so I cannot reproduce your problem.
Can you try with the same file I did?

Categories