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()
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!
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.
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?
I have a constraint with my syntax. I just tried python some time ago and I started it from AI. I make a bot like Jarvis to help me like opening google or youtube. From the tutorial available in Pythonspot.com it shows for Ubuntu tutorial but I use Windows. And there are some tools or plugins that do not work in windows is mpg321. I have found a replacement with mixer.music to play the sound of the AI. This works but I have constraints on the second sound, ie i make the first sound with audio.mp3 and it works then when my second voice uses the same filename ie audio.mp3 and i have constraints like this
Traceback (most recent call last):
File "D:\#AI Projects\Jarvis\Jarvis.py", line 71, in
jarvis(data)
File "D:\#AI Projects\Jarvis\Jarvis.py", line 53, in jarvis speak(ctime())
File "D:\#AI Projects\Jarvis\Jarvis.py", line 17, in speak tts.save("audio.mp3")
File "C:\Users\inialdan\AppData\Local\Programs\Python\Python36\lib\site-packages\gtts\tts.py",
line 110, in save
with open(savefile, 'wb') as f: PermissionError: [Errno 13] Permission denied: 'audio.mp3'
This is my code
#!/usr/bin/env python3
# Requires PyAudio and PySpeech.
import speech_recognition as sr
from time import ctime
import time
import os
import subprocess
from gtts import gTTS
from pygame import mixer
def speak(audioString):
print(audioString)
tts = gTTS(text=audioString, lang='en')
tts.save("audio.mp3")
mixer.init()
mixer.music.load('D:/#AI Projects/Jarvis/audio.mp3')
mixer.music.play()
def recordAudio():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Try to say something!")
audio = r.listen(source)
data = ""
try:
data = r.recognize_google(audio)
print("You said : " + data)
except sr.UnknownValueError:
print("I'm Sorry, i couldn't understand what you mean ...")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
return data
def jarvis(data):
CHROME = os.path.join('C:\\', 'Program Files (x86)', 'Google', 'Chrome', 'Application', 'chrome.exe')
if "jarvis" in data:
speak("Yes, sir ?")
if "what is your name" in data:
speak("You can call me, Jarvis")
if "where do you leave" in data:
speak("In your heart.")
if "how are you" in data:
speak("I am fine")
if "what time is it" in data:
speak(ctime())
if "where is" in data:
data = data.split(" ")
location = data[2]
speak("Hold on Aldan, I will show you where " + location + " is.")
os.system('taskkill /im chrome.exe')
subprocess.call([CHROME, "https://www.google.nl/maps/place/" + location + "/&"])
if "open" in data:
data = data.split(" ")
application = data[1]
speak("Hold on Aldan, I will show you " + application)
os.system('taskkill /im chrome.exe')
subprocess.call([CHROME, "https://www." + application + ".com"])
time.sleep(2)
speak("Hi Aldan, How may I assist you?")
while 1:
data = recordAudio()
jarvis(data)
I have tried it with os.remove (); to remove the audio.mp3 and rewrite it. but still failed
I have modified Speak method with the help of TemporaryFile.Click on the icon to check the Code
from googletrans import Translator
import pygame, time
import tempfile
from gtts import gTTS
from pygame import mixer
from tempfile import TemporaryFile
def speak(text, lang='en'):
"""Text to speech. For funp."""
try:
translator = Translator()
tts = gTTS(text=translator.translate(text, dest=lang).text, lang=lang)
mixer.init()
sf = TemporaryFile()
tts.write_to_fp(sf)
sf.seek(0)
mixer.music.load(sf)
mixer.music.play()
except Exception:
raise
Just create a second def that removes the filename and run that command after your speak()
def complete():
os.remove('audio.mp3')
speak("blah blah")
complete()
I did this, it worked. You may have to guess the length of the sound. For mine, 2 sec is long enough.
voice = gtts.gTTS(item)
voice.save("temp.mp3")
sound = SoundLoader.load("temp.mp3")
sound.play()
time.sleep(2)
sound.stop()
os.remove("temp.mp3")