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类UnknownValueError()的实例源码
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 recognize(recognizer_instance, audio_data):
# recognize speech using Google Speech Recognition
try:
# for testing purposes, we're just using the default API key
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# instead of `r.recognize_google(audio)`
text = str(recognizer_instance.recognize_google(audio_data))
print(text)
if 'on' in text.lower():
print('Turning on!')
if 'off' in text.lower():
print('Turning off!')
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 listen(self):
"""Listens to response from microphone and converts to text"""
self.userCommand = ""
with sr.Microphone(device_index=self.mic, chunk_size=1024, sample_rate=48000) as source:
print ("\tThreshold: " + str(self.r.energy_threshold))
print ("\tWaiting for words...")
try:
audio = self.r.listen(source, timeout=5)
# self.playSound("end.mp3")
try:
self.userCommand = self.r.recognize_google(audio)
self.userCommand = self.userCommand.lower()
if not self.processcommand(self.userCommand, source):
return False
else:
return True
except sr.UnknownValueError:
print ("\t...")
except sr.RequestError as e:
print("\tCould not request results from Google Speech Recognition service; {0}".format(e))
except Exception as e:
print (str(e))
except Exception:
print ("\tNo audio heard")
pass
def active_listen(self):
with self.sr_mic() as microphone:
try:
audio = self.sr_recognizer.listen(microphone,timeout=TIME_OUT_CONSTANT, phrase_time_limit=PHRASE_TIME_LIMIT)
except sr.WaitTimeoutError:
return ""
msg = ""
try:
msg = self.sr_recognizer.recognize_google(audio)#,language="fr-FR")
#msg = self.sr_recognizer.recognize_sphinx(audio)
print(msg.lower())
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
self.speak("Could not request results from Google STT. Check your connection! {0}".format(e))
except:
self.speak("Something it is Wrong! It hurts! Wrong! Wrong! Wrong!")
finally:
return msg.lower()
def active_listen(self):
with self.sr_mic() as microphone:
try:
audio = self.sr_recognizer.listen(microphone,timeout=TIME_OUT_CONSTANT, phrase_time_limit=PHRASE_TIME_LIMIT)
except sr.WaitTimeoutError:
pass
msg = ""
try:
msg = self.sr_recognizer.recognize_google(audio)#,language="fr-FR")
#msg = self.sr_recognizer.recognize_sphinx(audio)
print(msg.lower())
except sr.UnknownValueError:
pass#print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
self.speak("Could not request results from Google STT. Check your connection! {0}".format(e))
except:
pass#self.speak("Something it is Wrong! It hurts! Wrong! Wrong! Wrong!")
finally:
return msg.lower()
def process_audio(self, audio):
"""
Converts given audio to speech.
:param audio: Audio snippet to be converted to text.
"""
try:
# for testing purposes, we're just using the default API key
# to use another API key, use `r.recognize_google(audio,
# key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# instead of `r.recognize_google(audio)`
return self.recognizer.recognize_google(audio)
except UnknownValueError:
print("Google Speech Recognition could not understand audio")
except RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
def wit_callback(self, recognizer, audio):
try:
captured_audio = recognizer.recognize_wit(audio,
key=self.key,
show_all=self.show_all)
Utils.print_success("Wit.ai Speech Recognition thinks you said %s" % captured_audio)
self._analyse_audio(captured_audio)
except sr.UnknownValueError:
Utils.print_warning("Wit.ai Speech Recognition could not understand audio")
# callback anyway, we need to listen again for a new order
self._analyse_audio(audio_to_text=None)
except sr.RequestError as e:
Utils.print_danger("Could not request results from Wit.ai Speech Recognition service; {0}".format(e))
# callback anyway, we need to listen again for a new order
self._analyse_audio(audio_to_text=None)
# stop listening for an audio
self.stop_listening()
def houndify_callback(self, recognizer, audio):
"""
called from the background thread
"""
try:
captured_audio = recognizer.recognize_houndify(audio,
client_id=self.client_id,
client_key=self.key,
show_all=self.show_all)
Utils.print_success("Houndify Speech Recognition thinks you said %s" % captured_audio)
self._analyse_audio(captured_audio)
except sr.UnknownValueError:
Utils.print_warning("Houndify Speech Recognition could not understand audio")
# callback anyway, we need to listen again for a new order
self._analyse_audio(audio_to_text=None)
except sr.RequestError as e:
Utils.print_danger("Could not request results from Houndify Speech Recognition service; {0}".format(e))
# callback anyway, we need to listen again for a new order
self._analyse_audio(audio_to_text=None)
# stop listening for an audio
self.stop_listening()
def google_callback(self, recognizer, audio):
"""
called from the background thread
"""
try:
captured_audio = recognizer.recognize_google(audio,
key=self.key,
language=self.language,
show_all=self.show_all)
Utils.print_success("Google Speech Recognition thinks you said %s" % captured_audio)
self._analyse_audio(audio_to_text=captured_audio)
except sr.UnknownValueError:
Utils.print_warning("Google Speech Recognition could not understand audio")
# callback anyway, we need to listen again for a new order
self._analyse_audio(audio_to_text=None)
except sr.RequestError as e:
Utils.print_danger("Could not request results from Google Speech Recognition service; {0}".format(e))
# callback anyway, we need to listen again for a new order
self._analyse_audio(audio_to_text=None)
self.stop_listening()
def bing_callback(self, recognizer, audio):
"""
called from the background thread
"""
try:
captured_audio = recognizer.recognize_bing(audio,
key=self.key,
language=self.language,
show_all=self.show_all)
Utils.print_success("Bing Speech Recognition thinks you said %s" % captured_audio)
self._analyse_audio(captured_audio)
except sr.UnknownValueError:
Utils.print_warning("Bing Speech Recognition could not understand audio")
# callback anyway, we need to listen again for a new order
self._analyse_audio(audio_to_text=None)
except sr.RequestError as e:
Utils.print_danger("Could not request results from Bing Speech Recognition service; {0}".format(e))
# callback anyway, we need to listen again for a new order
self._analyse_audio(audio_to_text=None)
# stop listening for an audio
self.stop_listening()
def listen(self):
"""
:param self
This is a wrapper method to the Speech_Recognition package
found @ https://gist.github.com/GGulati/1ebaeaaa7f7408647fef#file-jarvis-py
"""
try:
with speech_recognition.Microphone() as source:
self.recognizer.adjust_for_ambient_noise(source)
audio = self.recognizer.listen(source, timeout=10, phrase_time_limit=None)
return self.recognizer.recognize_google(audio)
except speech_recognition.WaitTimeoutError:
self.error_message = "I could not hear you!"
except speech_recognition.UnknownValueError:
self.error_message = "I did not quite get that"
except speech_recognition.RequestError as e:
self.error_message = "Recognition Error: {0}".format(e)
def callback(recognizer, audio):
try:
recognized = recognizer.recognize_google(audio)
if recognized:
logging.info("clipped"+recognized+"\n")
copy_to_clipboard(recognized)
logging.info("copied text to clipboard.")
beep()
process_recognized_string(recognized)
return recognized
except sr.UnknownValueError:
print("..")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
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 get_query():
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
# recognize speech using Google Speech Recognition
try:
# for testing purposes, we're just using the default API key
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# instead of `r.recognize_google(audio)`
tts_result = r.recognize_google(audio)
print('Got ' + tts_result)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
return None
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
return None
return tts_result
def _google_stt(self):
"""
Uses Google's Speech to Text engine to
understand speech and convert it into
text.
:return: String, either the spoken text or error text.
"""
click.echo("Please speak now")
# Listen to the microphone set up in the __init__ file.
with self.microphone as mic_source:
audio = self.recognizer.listen(mic_source)
click.echo("Processing audio")
try:
# Try to recognize the text
return self.recognizer.recognize_google(audio, show_all=self.debugging)
except sr.UnknownValueError: # speech is unintelligible
return "Google could not understand the audio."
except sr.RequestError:
return "Could not request results from Google's speech recognition service."
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 process_listen(self):
if not os.path.isfile("data/temp/heard.pcm") or os.stat("data/temp/heard.pcm").st_size == 0:
await self.bot.send_embed(self.text_channel, ":warning: No input found")
return
func = functools.partial(subprocess.call, ["ffmpeg", "-f", "s16le", "-y", "-ar", "44.1k", "-ac", "2", "-i", "data/temp/heard.pcm", "data/temp/heard.wav"], shell = True)
await self.bot.loop.run_in_executor(None, func)
with speech_recognition.AudioFile("data/temp/heard.wav") as source:
audio = self.recognizer.record(source)
'''
try:
await self.bot.reply("Sphinx thinks you said: " + recognizer.recognize_sphinx(audio))
except speech_recognition.UnknownValueError:
await self.bot.reply("Sphinx could not understand audio")
except speech_recognition.RequestError as e:
await self.bot.reply("Sphinx error; {0}".format(e))
'''
try:
text = self.recognizer.recognize_google(audio)
await self.bot.send_embed(self.text_channel, "I think you said: `{}`".format(text))
except speech_recognition.UnknownValueError:
# await self.bot.send_embed(self.text_channel, ":no_entry: Google Speech Recognition could not understand audio")
await self.bot.send_embed(self.text_channel, ":no_entry: I couldn't understand that")
except speech_recognition.RequestError as e:
await self.bot.send_embed(self.text_channel, ":warning: Could not request results from Google Speech Recognition service; {}".format(e))
else:
response = clients.aiml_kernel.respond(text)
# TODO: Handle brain not loaded?
if not response:
games_cog = client.get_cog("Games")
if not games_cog: return
response = await games_cog.cleverbot_get_reply(text)
await self.bot.send_embed(self.text_channel, "Responding with: `{}`".format(response))
await self.play_tts(response, self.bot.user)
# open("data/heard.pcm", 'w').close() # necessary?
# os.remove ?
def on_enter(self, payload=None):
""" Executed on the entry to the Recognizing State. Upon entry, audio is captured from the Microphone and
recognition with preferred speech recognition engine is done. If successful, the machine transitions to Busy
State. On failure, it transitions to Error state.
:param payload: No payload is expected by this state
:return: None
"""
self.notify_renderer('listening')
recognizer = self.components.recognizer
try:
print("Say something!")
with self.components.microphone as source:
audio = recognizer.listen(source, phrase_time_limit=5)
self.notify_renderer('recognizing')
print("Got it! Now to recognize it...")
try:
value = self.__recognize_audio(
audio=audio, recognizer=recognizer)
print(value)
self.notify_renderer('recognized', value)
self.transition(self.allowedStateTransitions.get(
'busy'), payload=value)
except sr.UnknownValueError:
print("Oops! Didn't catch that")
self.transition(self.allowedStateTransitions.get(
'error'), payload='RecognitionError')
except sr.RequestError as e:
print(
"Uh oh! Couldn't request results from Speech Recognition service; {0}".format(e))
self.transition(self.allowedStateTransitions.get(
'error'), payload='ConnectionError')
except KeyboardInterrupt:
pass
def listen():
with speech_recognition.Microphone() as source:
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)
try:
return recognizer.recognize_sphinx(audio)
# or: return recognizer.recognize_google(audio)
except speech_recognition.UnknownValueError:
print("Could not understand audio")
except speech_recognition.RequestError as e:
print("Recog Error; {0}".format(e))
return ""
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)
dataset_recorder_generator.py 文件源码
项目:Personal_AI_Assistant
作者: PratylenClub
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def callback(recognizer, audio):
try:
sentence = recognizer.recognize_google(audio, language=language)
wave_file_name = "train.wav"
wav_file = open(wave_file_name,"wb")
wav_file.write(audio.get_wav_data())
wav_file.close()
wave, sample_rate = librosa.load(wave_file_name, mono=True, sr=None)
wave = wave[::3]
save_recording(wave_file_name,wave,sentence,CSV_BIG_ONE)
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 callback(recognizer, audio):
# received audio data, now we'll recognize it using Google Speech Recognition
try:
# for testing purposes, we're just using the default API key
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# instead of `r.recognize_google(audio)`
#print(r.recognize_sphinx(audio,language="fr-FR"))#
print recognizer.recognize_google(audio)#, language="fr-FR"))
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 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 hear(source, r, robot):
audio = r.listen(source)
try:
recognized = r.recognize_google(audio)
print("You said: " + recognized)
if command_activate in recognized or command_activate.lower() in recognized:
print("Action command recognized")
if command_pickup in recognized:
do_blocks(robot)
elif command_dance in recognized:
do_dance()
elif command_lookatme in recognized:
do_lookforface(robot)
elif command_followme in recognized:
do_lookforface(robot)
do_followface(robot)
elif command_takepictureofme in recognized:
do_lookforface(robot)
do_takepicture(robot)
elif command_takepicture in recognized:
do_takepicture(robot)
elif command_say in recognized:
do_say(recognized, robot)
else:
print("Command not recognized")
else:
print("You did not say the magic word " + command_activate)
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 apiai_callback(self, recognizer, audio):
"""
called from the background thread
:param recognizer:
:param audio:
:return:
"""
try:
captured_audio = recognizer.recognize_api(audio,
client_access_token=self.key,
language=self.language,
session_id=self.session_id,
show_all=self.show_all)
Utils.print_success("Apiai Speech Recognition thinks you said %s" % captured_audio)
self._analyse_audio(captured_audio)
except sr.UnknownValueError as e:
Utils.print_warning("Apiai Speech Recognition could not understand audio; {0}".format(e))
# callback anyway, we need to listen again for a new order
self._analyse_audio(audio_to_text=None)
except sr.RequestError as e:
Utils.print_danger("Could not request results from Apiai Speech Recognition service; {0}".format(e))
# callback anyway, we need to listen again for a new order
self._analyse_audio(audio_to_text=None)
# stop listening for an audio
self.stop_listening()