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?
Related
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()
import speech_recognition as sr
sr.__verison__
'3.8.1'
AUDIO_FILE = path.join(os.path.join(path, "User/Desktop", "Module1.mp3")
r = sr.Recognizer()
with sr.AudioFile(AUDIO_FILE) as source:
audio = r.record(source) # read the entire audio file
# recognize speech using Sphinx
try:
print("Sphinx thinks you said " + r.recognize_sphinx(audio))
except sr.UnknownValueError:
print("Sphinx could not understand audio")
except sr.RequestError as e:
print("Sphinx error; {0}".format(e))
File 'main.py', line 10
r = sr.Recognizer()
^ SyntaxError: invalid syntax
Hey everyone,
I'm pretty new to coding and I'm trying to make a simple program to take notes from an mp3 file. Anyone know whats wrong?
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.