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()
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 have the following piece of code:
import openai
import pyttsx3
import speech_recognition as sr
from api_key import API_KEY
openai.api_key = API_KEY
engine = pyttsx3.init()
r = sr.Recognizer()
mic = sr.Microphone(device_index=1)
print(sr.Microphone.list_microphone_names())
conversation = ""
user_name = "Josode"
while True:
with mic as source:
print("\nlistening... speak clearly into mic.")
r.adjust_for_ambient_noise(source, duration=0.2)
audio = r.listen(source)
print("no longer listening.\n")
try:
user_input = r.recognize_google(audio)
except:
continue
prompt = user_name + ": " + user_input + "\n Ava:"
conversation += prompt
response = openai.Completion.create(engine="text-davinci-002", prompt=conversation, max_tokens=100)
response_str = response["choices"][0]["text"].replace("\n", "")
response_str = response_str.split(user_name + ": ", 1)[0].split("Ava: ", 1)[0]
conversation += response_str + "\n"
print(response_str)
engine.say(response_str)
engine.runAndWait()
When I run the file, I get just listening... speak clearly into mic.
no longer listening. with no output from davinci.
Also, the mic print is ['LG FULL HD', 'Ari Chan’s AirPods', 'Ari Chan’s AirPods', 'MacBook Pro Microphone', 'MacBook Pro Speakers']. I am using index 1
API key is correct and imported. I have an account at Open AI and can use playground without a problem.
Do you see something that I don't see? It should work
Most probably you are getting an exception in r.recognize_google(audio) so it forces continue again and again without any output, try to add something like this to debug it:
import traceback
...
try:
user_input = r.recognize_google(audio)
except:
print(traceback.format_exc())
continue
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
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)
My script just stop and get stuck after engine.runandWait()... If someone has any idea of how to make it continue I would appreciate !
It seems that the answer isn't in the script itself because I tried scripts that are super simple... I also tried to uninstall and reinstall portaudio, pyaudio and pyttsx3
Here is my script :
import speech_recognition as sr
import pyttsx3
import pywhatkit
import datetime
import wikipedia
import time
import pyjokes
listener = sr.Recognizer()
engine = pyttsx3.init()
voices = engine.setProperty('voices', "french")
def talk(text):
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
def take_command():
command = ''
try:
with sr.Microphone() as source:
print("...")
voice = listener.listen(source)
command = listener.recognize_google(voice,language = 'fr-FR')
command = command.lower()
except:
talk("Je me mets en veille")
pass
return command
def run_jeff(run):
command = take_command()
if 'youtube' in command:
command = command.replace('youtube','')
command = command.replace('ouvre','')
pywhatkit.playonyt(command)
elif "stop" in command:
talk("Je vais dodo")
run = False
elif 'bonjour' in command or 'salut' in command :
talk('Bonjour, comment allez-vous ?')
talk(info)
elif 'blague' in command :
talk(pyjokes.get_joke())
else :
talk("Pouvez-vous répétez je n'ai pas compris ?")
print(command)
run = True
while True:
run_jeff(run)
if run == False:
exit()
In the def talk section, you can remove the first line and in the while True statement, delete if run == False, and add a elif statement to the take command sections saying elif 'thank you' in command: exit(the name of your assistant). Try it
Here is a short pyttsx testing program.
Save it as 'pyttsx_test.py' then run it.
If your version is working correctly it will speak the code and print it to shell.
"""pyttsx_test.py
I am a text to speech test
"""
import pyttsx3
engine = pyttsx3.Engine()
engine.setProperty( "rate", 200 )
engine.setProperty( "volume", 1.0 )
engine.say( 'say something' )
engine.runAndWait()
with open( 'pyttsx_test.py', mode='rt') as files:
lines = files.readlines()
lines = [ x.strip(' ') for x in lines ] # remove un-necessary spaces
lines = [ x for x in lines if x!='\n'] # remove empty lines
for a in lines:
print( a, end ='' )
engine.say( a )
engine.runAndWait()
You should change your input device using device_index. (default=0)
with sr.Microphone(device_index=0) as source:
You can see all your devices using:
for index, name in enumerate(sr.Microphone.list_microphone_names()):
print(f'{index}, {name}')
Every time you initiate the engine or "init()" you have to set property again. You duplicated your lines and used it wrong.
if you wish it to work delete thoes lines -
engine = pyttsx3.init()
voices = engine.setProperty('voices', "french")
def talk(text):
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
and replace them with this -
def talk(text):
engine = pyttsx3.init()
voices = engine.setProperty('voices', "french")
engine.say(text)
engine.runAndWait()