Hello, when I start my project and say for example: (open google). the project can't recognize words correctly and give me a random word.
my codes
start
import os
import sys
from playsound import playsound
import pyaudio
import webbrowser
import datetime
import wikipedia
import speech_recognition as sr
from pocketsphinx import pocketsphinx, Jsgf, FsgModel
grammar_path = os.path.abspath(os.path.dirname(grammar_file))
grammar_name = os.path.splitext(os.path.basename(grammar_file))[0]
fsg_path = "{0}/{1}.fsg".format(grammar_path, grammar_name)
if not os.path.exists(fsg_path):
jsgf = pocketsphinx.Jsgf(grammar_file)
rule = jsgf.get_rule("{0}.{0}".format(grammar_name))
fsg = jsgf.build_fsg(rule, decoder.get_logmath(), 7.5)
fsg.writefile(fsg_path)
print('Successful JSFG to FSG conversion!!!')
Pass the fsg file into the decoder
decoder.set_fsg(grammar_name, fsg)
2
r = sr.Recognizer()
mic = sr.Microphone()
with mic as source:
audio = r.listen(source)
result = r.recognize_sphinx(audio)
print(result)
with open('contact.gram') as myfile:
if result in myfile.read():
print('Blahblah')
if result =="hello":
print("You said Hello")
playsound(rootdir + "\test.mp3")
elif result == "time":
print("BETA")
elif result == "what then":
print("opening youtube")
playsound(rootdir + "\\test.mp3")
webbrowser.open("youtube.com")
elif result == "open google":
print("opening google")
playsound(rootdir + "\\test.mp3")
webbrowser.open("google.com")
Hello, when I start my project and say for example: (open google). the project can't recognize words correctly and give me a random word.
my codes
start
import os import sys from playsound import playsound import pyaudio import webbrowser import datetime import wikipedia import speech_recognition as sr from pocketsphinx import pocketsphinx, Jsgf, FsgModel
rootdir = os.path.dirname(sys.modules['main'].file) rootdir2 = os.path.dirname(sys.modules['main'].file) rootdir3 = os.path.dirname(sys.modules['main'].file) rootdir +="\sounds" rootdir2 +="\env\Lib\site-packages\speech_recognition\pocketsphinx-data\en-US" rootdir3 +="\config"
grammar_file = "contact.gram"
grammar_path = os.path.join(rootdir3 + "\contact.gram")
fsg_path = "{}.fsg".format(grammar_path)
print("Grammar Filepath: {}".format(grammar_path))
print("FSG Grammar Filepath: {}".format(fsg_path))
Create decoder object
config = pocketsphinx.Decoder.default_config() config.set_string("-hmm", os.path.join(rootdir2, 'acoustic-model')) config.set_string("-lm", os.path.join(rootdir2, 'language-model.lm.bin')) config.set_string("-dict", os.path.join(rootdir2, 'pronounciation-dictionary.dict')) config.set_string("-logfn", os.devnull) decoder = pocketsphinx.Decoder(config)
Convert grammar
grammar_path = os.path.abspath(os.path.dirname(grammar_file)) grammar_name = os.path.splitext(os.path.basename(grammar_file))[0] fsg_path = "{0}/{1}.fsg".format(grammar_path, grammar_name) if not os.path.exists(fsg_path): jsgf = pocketsphinx.Jsgf(grammar_file) rule = jsgf.get_rule("{0}.{0}".format(grammar_name)) fsg = jsgf.build_fsg(rule, decoder.get_logmath(), 7.5) fsg.writefile(fsg_path) print('Successful JSFG to FSG conversion!!!')
Pass the fsg file into the decoder
decoder.set_fsg(grammar_name, fsg)
2
r = sr.Recognizer() mic = sr.Microphone() with mic as source: audio = r.listen(source) result = r.recognize_sphinx(audio) print(result) with open('contact.gram') as myfile: if result in myfile.read(): print('Blahblah') if result =="hello": print("You said Hello") playsound(rootdir + "\test.mp3")