def SpeechToText():
r = sr.Recognizer() #Speech recognition
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
message = r.recognize_google(audio)
print("Check: "+message)
try:
print("User: " + r.recognize_google(audio))
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
return message
#function to find importance of words to use them to deduce that which thing is being asked more
python类Recognizer()的实例源码
def OnClicked(self):
r = sr.Recognizer()
with sr.Microphone() as source:
speak.say('Hey I am Listening ')
speak.runAndWait()
audio = r.listen(source)
try:
put=r.recognize_google(audio)
self.displayText(put)
self.textBox.insert('1.2',put)
self.textBox.delete('1.2',tk.END)
events(self,put)
except sr.UnknownValueError:
self.displayText("Could not understand audio")
except sr.RequestError as e:
self.displayText("Could not request results; {0}".format(e))
def OnClicked(self):
r = sr.Recognizer()
with sr.Microphone() as source:
speak.say('Hey I am Listening ')
speak.runAndWait()
audio = r.listen(source)
try:
put=r.recognize_google(audio)
self.displayText(put)
self.textBox.insert('1.2',put)
put=put.lower()
put = put.strip()
#put = re.sub(r'[?|$|.|!]', r'', put)
link=put.split()
events(self,put,link)
except sr.UnknownValueError:
self.displayText("Could not understand audio")
except sr.RequestError as e:
self.displayText("Could not request results; {0}".format(e))
def __init__(self, name, voice, purpose, location, input, output, microphone):
"""Initializes assistant, adjusts for ambient noise, and checks TTS"""
self.name = name
self.name = self.name.lower()
self.purpose = purpose
self.voice = voice
self.location = location
self.r = sr.Recognizer()
self.mic = microphone
self.input = input
self.output = output
with sr.Microphone(device_index=self.mic, chunk_size=1024, sample_rate=48000) as source:
self.r.adjust_for_ambient_noise(source) # Adjust for ambient noise by listening for 1 second
# self.r.energy_threshold = 30 # Threshold offset
print ("\tThreshold: " + str(self.r.energy_threshold))
print("\tInitializing {}...".format(self.name))
self.speak("My name is " + self.name + " and I am here " + self.purpose + ". How may I help you?")
def __init__(self):
self.status = 1
self.version = "0.0.7"
self.array = []
self.numbers = []
self.path = os.path.abspath(os.path.dirname(sys.argv[0]))
try:
self.con = sqlite3.connect(self.path + "config/Jarbas.db")
except:
g = self.path.split("core")
dbpath = g[0] + "/config/Jarbas.db"
self.con = sqlite3.connect(dbpath)
self.serialport = self.arduino_check()
self.rec = sr.Recognizer()
self.engine = pyttsx.init()
self.rate = self.engine.getProperty('rate')
self.engine.setProperty('rate', self.rate-60)
self.voices = self.engine.getProperty('voices')
self.engine.setProperty('voice',self.voices[16].id) #1,9,10,11,16,22,25
self.ser = serial.Serial()
self.ser.port = self.serialport
self.ser.baudrate = 9600
def recognize():
r = sr.Recognizer()
m = sr.Microphone()
try:
with m as source: r.adjust_for_ambient_noise(source)
print("Escuchando...")
play('beep.wav')
with m as source: audio = r.listen(source)
print("Enterado! Reconociendo...")
try:
value = r.recognize_google(audio, language="es-MX")
if str is bytes:
print(u"Dijiste {}".format(value).encode("utf-8"))
else:
print("Dijiste {}".format(value))
except sr.UnknownValueError:
print("Ups! No entendimos")
except sr.RequestError as e:
print("Oh oh! Error en la peticion a Google; {0}".format(e))
except KeyboardInterrupt:
pass
def __init__(self):
self.ai = ai_interface.API(apiai.ApiAI(settings['CLIENT_ACCESS_TOKEN'],
settings['SUBSCRIPTION_KEY']))
self.debugging = settings['debugging']
self.spoken_language = settings['spoken language']
self.input_system = settings['input system'] # google, local, text
self.output_system = settings['output system'] # both, audio, text
self.speech_file_location = settings['speech file location'] # .
self.speech_file_name = settings['speech file name'] # audio response
self.speak = settings['speak'] # True
# The question that the assistant hears
self.question = None
# The chosen, spoken response given to the user.
self.response = None
# Whether Friday is active
self.is_active = True
# What type of question is being asked.
self.request_type = None
if settings['input system'] != 'text':
self.recognizer = sr.Recognizer()
self.microphone = sr.Microphone(device_index=settings['DEVICE'],
sample_rate=settings['RATE'],
chunk_size=settings['CHUNK'])
if settings['input_system'] == 'google':
with self.microphone as source:
if settings['debugging']:
click.echo("Adjusting to ambient noise.")
# we only need to calibrate once, before we start listening
self.recognizer.adjust_for_ambient_noise(source)
# Build the manager
self.manager = PluginManager()
# Tell it the default place(s) where to find plugins
self.manager.setPluginPlaces(settings["plugin folder"])
# Load all plugins
self.manager.locatePlugins()
self.manager.loadPlugins()
self.plugins = {}
# Activate all loaded plugins
for plugin in self.manager.getAllPlugins():
self.plugins[plugin.name] = plugin.plugin_object
def main():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
try:
speech_text = r.recognize_google(audio).lower().replace("'", "")
print("Mustani thinks you said '" + speech_text + "'")
except sr.UnknownValueError:
print("Mustani could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition
service; {0}".format(e))
brain(name,speech_text,city_name, city_code)
def start_speech_recording(tmp):
# Record Audio
global recognised_speech
BING_KEY = "cfee7d6db79d4671b9cea936da4689d7"
while True:
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
r.adjust_for_ambient_noise(source, duration = 1)
audio = r.listen(source)
try:
recognised_speech = r.recognize_bing(audio, key=BING_KEY).lower()
print("Microsoft Bing Voice Recognition thinks you said:" + recognised_speech)
if "hallo" in recognised_speech or "wakeup" in recognised_speech or "start" in recognised_speech or "makeup" in recognised_speech or "star" in recognised_speech or "breakup" in recognised_speech:
thread.start_new_thread( face_identify, (3, ) )
except sr.UnknownValueError:
print("Microsoft Bing Voice Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Microsoft Bing Voice Recognition service; {0}".format(e))
def start_speech_recording(tmp):
# Record Audio
global recognised_speech
while True:
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
r.adjust_for_ambient_noise(source, duration = 1)
audio = r.listen(source)
try:
recognised_speech = r.recognize_google(audio).lower()
print("You said: " + r.recognize_google(audio))
if "hallo" in recognised_speech or "wakeup" in recognised_speech or "start" in recognised_speech or "makeup" in recognised_speech or "star" in recognised_speech or "breakup" in recognised_speech:
thread.start_new_thread( face_identify, (3, ) )
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
def recognize_speech():
# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
try:
print("Starting speech recognition")
cred_json = json.loads(open(config.get('google_cloud_api_service_account_json')).read())
recognized_speech = r.recognize_google_cloud(audio, credentials_json=cred_json)
# we need some special handling here to correctly print unicode characters to standard output
if str is bytes: # this version of Python uses bytes for strings (Python 2)
print(u"You said {}".format(recognized_speech).encode("utf-8"))
else: # this version of Python uses unicode for strings (Python 3+)
print("You said {}".format(recognized_speech))
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
def reconhecer():
rec = sr.Recognizer()
with sr.Microphone() as fala:
frase = rec.listen(fala)
frase = rec.recognize_google(frase,language='pt')
frase = frase.lower()
return frase
def __init__(self, client, text_channel):
self.bot = client
self.text_channel = text_channel
self.server = text_channel.server
self.queue = asyncio.Queue()
self.current = None
self.play_next_song = asyncio.Event()
self.ytdl_options = {"default_search": "auto", "noplaylist": True, "quiet": True,
"format": "webm[abr>0]/bestaudio/best", "prefer_ffmpeg": True}
self.ytdl_download_options = {"default_search": "auto", "noplaylist": True, "quiet": True,
"format": "bestaudio/best", "extractaudio": True, "outtmpl": "data/audio_cache/%(id)s-%(title)s.%(ext)s", "restrictfilenames": True} # "audioformat": "mp3" ?
self.ytdl_playlist_options = {"default_search": "auto", "extract_flat": True, "forcejson": True, "quiet": True,
"logger": playlist_logger}
self.default_volume = 100.0
self.skip_votes_required = 0
self.skip_votes = set()
self.player = self.bot.loop.create_task(self.player_task())
self.resume_flag = asyncio.Event()
self.not_interrupted = asyncio.Event()
self.not_interrupted.set()
self.audio_files = os.listdir("data/audio_files/")
self.library_files = [f for f in os.listdir(clients.library_files) if f.endswith((".mp3", ".m4a"))]
self.library_flag = False
self.radio_flag = False
self.recognizer = speech_recognition.Recognizer()
self.listener = None
self.listen_paused = False
self.previous_played_time = 0
speech_recognition_stt_base.py 文件源码
项目:mycroft-light
作者: MatthewScholefield
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def __init__(self, rt):
super().__init__(rt)
from speech_recognition import Recognizer
self.recognizer = Recognizer()
def __init__(self, renderer=None):
recognizer = Recognizer()
recognizer.dynamic_energy_threshold = False
recognizer.energy_threshold = 1000
self.recognizer = recognizer
self.microphone = Microphone()
self.susi = susi
self.renderer = renderer
try:
res = requests.get('http://ip-api.com/json').json()
self.susi.update_location(
longitude=res['lon'], latitude=res['lat'])
except ConnectionError as e:
logging.error(e)
self.config = json_config.connect('config.json')
if self.config['usage_mode'] == 'authenticated':
try:
susi.sign_in(email=self.config['login_credentials']['email'],
password=self.config['login_credentials']['password'])
except Exception:
print('Some error occurred in login. Check you login details in config.json')
if self.config['hotword_engine'] == 'Snowboy':
from main.hotword_engine import SnowboyDetector
self.hotword_detector = SnowboyDetector()
else:
from main.hotword_engine import PocketSphinxDetector
self.hotword_detector = PocketSphinxDetector()
if self.config['wake_button'] == 'enabled':
if self.config['device'] == 'RaspberryPi':
from ..hardware_components import RaspberryPiWakeButton
self.wake_button = RaspberryPiWakeButton()
else:
self.wake_button = None
else:
self.wake_button = None
def __init__(self, f_action_handler: callable):
"""Constructor.
:param f_action_handler: do action based on received text.
"""
self.plugin_action_handler = f_action_handler
self.source = None
self.re = sr.Recognizer()
self.re.dynamic_energy_threshold = DictonatorSettings.settings['Main']['dynamic_noise_suppression']
self.mic = sr.Microphone()
self.re_stopper = None
self.is_listening = False
self.is_prepared = False
self.noise_level = None
logger.debug("Speech Recogniser initialised")
def cut_and_send(infile, outfile, length):
# print(infile)
# print(outfile)
# print(length)
# return
myaudio = AudioSegment.from_file(infile, "wav")
chunk_length_ms = length # pydub calculates in millisec
chunks = make_chunks(myaudio, chunk_length_ms) # Make chunks of one sec
for i, chunk in enumerate(chunks):
chunk_name = "chunk{0}.wav".format(i)
print("exporting", chunk_name)
chunk.export(chunk_name, format="wav")
r = sr.Recognizer()
with sr.AudioFile(chunk_name) as source:
audio = r.record(source)
# recognize speech using Google Speech Recognition
try:
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# instead of `r.recognize_google(audio)`
txt = r.recognize_google(audio) + " "
with open(outfile, 'a') as f:
f.write(txt)
except sr.UnknownValueError:
print("Ehm... sorry not understood this one.")
except sr.RequestError as e:
print("Request failed; {0}".format(e))
os.remove(chunk_name)
def main():
# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
r.listen_in_background(source, recognize)
# ----------------------------
def audio_to_text(outname):
import speech_recognition as sr
r = sr.Recognizer()
with sr.WavFile(outname) as source:
audio = r.record(source)
try:
command = r.recognize_google(audio,language='pt_BR')
if not command.isspace():
report(now,command.encode('utf-8'))
log = '%s>>>%s\n' %(now,command.encode('utf-8'))
report(log)
except Exception as e:
print str(e)
os.remove(outname)
def get_recognizer():
return sr.Recognizer()
def __init__(self, master):
global APP_INSTANCE
APP_INSTANCE = self
self.top = master
self.index = 0
# display first image
filename = "images/alphabet6.jpg"
if not os.path.exists(filename):
print "Unable to find %s" % filename
self.top.quit()
im = Image.open(filename)
im = im.resize((self.top.winfo_screenwidth(), self.top.winfo_screenheight()))
if im.format == "SPIDER":
im = im.convert2byte()
self.size = im.size
self.tkimage = ImageTk.PhotoImage(im)
self.lbl = Label(master, image=self.tkimage)
self.lbl.pack(side="top")
r = sr.Recognizer()
m = sr.Microphone()
with m as source:
r.adjust_for_ambient_noise(source)
self.stop_listening = r.listen_in_background(m, callback)
# image doesn't appear unless put Image.open in separate function?
# and need to use tkimage.paste, not ImageTk.PhotoImage
def stot():
r = sr.Recognizer()
with sr.Microphone() as source:
try:
print("Say it!!")
audio = r.listen(source)
print(r.recognize_google(audio))
except sr.UnknownValueError:
print("Could not understand audio")
except sr.RequestError as e:
print("Could not request results; {0}".format(e))
def audio():
# Record Audio
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
return str(r.recognize_google(audio))
def run(sdk_conn):
'''The run method runs once the Cozmo SDK is connected.'''
robot = sdk_conn.wait_for_robot()
try:
print("Say something")
r = sr.Recognizer()
with sr.Microphone() as source:
while 1:
hear(source, r, robot)
print("say something else")
recognized = None
except KeyboardInterrupt:
print("")
print("Exit requested by user")
def initialize(self):
self.recognizer = Recognizer()
self.adjusted = False
# obtain audio from the microphone
def __init__(self, audio_file=None):
"""
Thread used to caught n audio from the microphone and pass it to a callback method
"""
super(SpeechRecognition, self).__init__()
self.recognizer = sr.Recognizer()
self.microphone = sr.Microphone()
self.callback = None
self.stop_thread = None
self.kill_yourself = False
self.audio_stream = None
# get global configuration
sl = SettingLoader()
self.settings = sl.settings
if audio_file is None:
# audio file not set, we need to capture a sample from the microphone
with self.microphone as source:
if self.settings.recognition_options.adjust_for_ambient_noise_second > 0:
# threshold is calculated from capturing ambient sound
logger.debug("[SpeechRecognition] threshold calculated by "
"capturing ambient noise during %s seconds" %
self.settings.recognition_options.adjust_for_ambient_noise_second)
Utils.print_info("[SpeechRecognition] capturing ambient sound during %s seconds" %
self.settings.recognition_options.adjust_for_ambient_noise_second)
self.recognizer.adjust_for_ambient_noise(source,
duration=self.settings.
recognition_options.adjust_for_ambient_noise_second)
else:
# threshold is defined manually
logger.debug("[SpeechRecognition] threshold defined by settings: %s" %
self.settings.recognition_options.energy_threshold)
self.recognizer.energy_threshold = self.settings.recognition_options.energy_threshold
Utils.print_info("Threshold set to: %s" % self.recognizer.energy_threshold)
else:
# audio file provided
with sr.AudioFile(audio_file) as source:
self.audio_stream = self.recognizer.record(source) # read the entire audio file
SpeechRecognition_translator.py 文件源码
项目:respeaker_python_library
作者: respeaker
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def task(quit_event):
mic = Microphone(quit_event=quit_event)
r = sr.Recognizer()
while not quit_event.is_set():
if mic.wakeup('respeaker'):
print('Wake up')
data = mic.listen()
try:
text = r.recognize_google(convert(data), language='en-US')
if text:
print('Recognized %s' % text)
except Exception as e:
print(e.message)
def __init__(self, google_api_key):
self.recognizer = sr.Recognizer()
self._api_key = google_api_key
self._number_channels = None
def __init__(self):
"""
Constructs a new googleSpeech object
"""
self.recognizer = speech_recognition.Recognizer()
self.error_message = ""
def initiate_Persia(self):
"""
Initializes Persia and starts the listening loop
"""
# starts the recognizer
r = sr.Recognizer()
with sr.Microphone() as source:
while True:
logger.debug("Awaiting user input.")
audio = r.listen(source)
logger.debug("Interpreting user input.")
# Speech recognition using Google Speech Recognition
try:
result = r.recognize_google(audio)
#result = r.recognize_sphinx(audio)
self.handle_action(result)
except sr.UnknownValueError:
logger.debug("Could not understand audio")
#Persia.speak("I'm sorry, but I couldn't understand what you said.")
except sr.RequestError as e:
logger.warn("Could not request results from Google Speech Recognition service: %s", e)
except Exception as e:
logger.error("Could not process text: %s", e)