In SpeechRecognition, when I start speech recognition and speak, I'm recognized a few seconds after the end of the talk, but can't I shorten the time it takes for my words to end and become recognized?
import speech_recognition as sr
from speech_recognition import *
import pyaudio
import os
translator = googletrans.Translator()
r = Recognizer()
m = Microphone()
while 1 == True:
with m as source:
print('ready')
audio = r.listen(source)
Text = r.recognize_google(audio, language='en')
print(Text)
Related
I have some voice detection code and it works! but, it runs really slowly. Can I do anything to make it faster?
import speech_recognition
import pyttsx3
recognizer = speech_recognition.Recognizer()
while True:
try:
with speech_recognition.Microphone() as mic:
recognizer.adjust_for_ambient_noise(mic, duration=0.2)
audio = recognizer.listen(mic)
text = recognizer.recognize_google(audio)
text = text.lower()
print(f" {text}")
except speech_recognition.UnknownValueError():
recognizer = speech_recognition.Recognizer()
continue
Try creating mic once instead of each iteration:
import speech_recognition
import pyttsx3
recognizer = speech_recognition.Recognizer()
with speech_recognition.Microphone() as mic:
while True:
try:
recognizer.adjust_for_ambient_noise(mic, duration=0.2)
audio = recognizer.listen(mic)
text = recognizer.recognize_google(audio)
text = text.lower()
print(f" {text}")
except speech_recognition.UnknownValueError():
pass
I'm working with speech_recognition python and I want to know if It's possible to check if the text that I've said is egual to "x"
Here is my code :
import speech_recognition
import speech_recognition as sr
import pyttsx3
recognizer = speech_recognition.Recognizer()
while True:
try:
with speech_recognition.Microphone() as mic:
recognizer.adjust_for_ambient_noise(mic, duration=0.2)
audio = recognizer.listen(mic)
text = recognizer.recognize_google(audio, language="fr-FR")
text = text.lower()
print(text)
except speech_recognition.UnknownValueError():
print("I don't understand sir")
continue
I've checked
if (text == "x"):
But it didn't works
here is my code I'm using pocketsphinx I want to compare my voice with these words but it's not working.
from pocketsphinx import LiveSpeech
import speech_recognition as sr
'''The start of Speech system is here,'''
def chair_offline():
words = ["LEFT","RIGHT","FORWARD","BACKWARD","STOP","RUN"]
r = sr.Recognizer()
for audioo in LiveSpeech():
res = str(audioo)
if res in words:
print(res)
else:
print("sorry modle not train")
chair_offline()
I'm doing a project on speech recognition and am trying to use Tkinter to create a GUI for my project...the SR part works well but when I integrate it with Tkinter it doesn't work...please help. (am new to programming so please don't mind my code:) )
#MY CODE
import speech_recognition as sr
import tkinter as tk
obj = tk.Tk()
obj.title("SpeechToText")
obj.geometry('400x200')
obj.resizable(0,0)
def rec():
r = sr.Recognizer()
msg.configure(text="Say something")
while True:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
try:
txt = "".format(r.recognize_google(audio).get())
msg.configure(text=txt)
except Exception as e:
print(e)
break
msg = tk.Label()
msg.grid(row=0,column=0)
btn = tk.Button(text="Start",command=rec)
btn.grid(row=2,column=0)
obj.mainloop()
I would like it to display the translated text in the label but it doesn't. It only shows "say something" even after speaking.
Try this, I blocked out the msg.configure(text='Say Somethin'). This line makes the recorded text being reformatted to 'Say Something' and not the text that was recorded. Hope this helps.
import speech_recognition as sr
import tkinter as tk
obj = tk.Tk()
obj.title("SpeechToText")
obj.geometry('400x200')
obj.resizable(0,0)
def rec():
r = sr.Recognizer()
#msg.configure(text="Say something")
while True:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
try:
txt = r.recognize_google(audio)
msg.configure(text=txt)
print(txt)
except Exception as e:
print(e)
break
msg = tk.Label()
msg.grid(row=0,column=0)
btn = tk.Button(text="Start",command=rec)
btn.grid(row=2,column=0)
obj.mainloop()
Jarvis should say every instruction comes on screen but here first statement is spoken & none other than that gets audio voice. And while saving the audio only the first statement gets stored as audio file. how to delete the previous audio file after process & save new audio file as instruction.How to get print statements in audio form, like there's a print statement ("Say something!") , it should be there on the screen as well as audio of same should be spoken.
import speech_recognition as sr
from time import ctime
import time
import os
from gtts import gTTS
from pygame import mixer
mixer.init()
def speak(audioString):
print(audioString)
tts = gTTS(text=audioString, lang='en')
tts.save("audio.mp3")
os.system("mpg321 audio.mp3")
def speak_out():
mixer.music.load('audio.mp3')
mixer.music.play()
time.sleep(2)
def recordAudio():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
r.pause_threshold = 1
r.adjust_for_ambient_noise(source, duration=1)
audio = r.listen(source)
data = ""
try:
data = r.recognize_google(audio)
print("You said: " + data)
except sr.UnknownValueError:
print("I could not understand")
except sr.RequestError as e:
print("Server error. Check your internet connection: {0}".format(e))
return data
def jarvis(data):
if "how are you" in data:
speak("I am fine")
if "what time is it" in data:
speak(ctime())
if 'mail' in data:
speak('who is recipient')
recipient = speak()
if 'John' in data:
speak('What should I say')
content = recordAudio()
mail = smtplib.SMTP('smtp.gmail.com', 587)
mail.ehlo()
mail.starttls()
mail.login('abc#gmail.com','xyz')
mail.sendmail('Person name', 'mail_id', content)
mail.close()
speak('Email sent')
time.sleep(2)
speak("Hi Ajay, what can I do for you?")
speak_out()
while 1:
data = recordAudio()
jarvis(data)