Trouble with speech recognition - python

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?

Related

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!

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?

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