我们从Python开源项目中,提取了以下42个代码示例,用于说明如何使用speech_recognition.Microphone()。
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
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 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 listen(): while True: try: with sr.Microphone() as source: print("Waiting for initialisation") r.adjust_for_ambient_noise(source) audio = r.listen(source) initiate = r.recognize_google(audio) keyword = initiate[:8] COMMAND = str(initiate[9:]) if keyword == "computer": if "please" == COMMAND[:6]: space = COMMAND.index(" ") COMMAND = COMMAND[space+1:] if "can you" or "may you" == COMMAND[:7]: space = COMMAND.index("you") COMMAND = COMMAND[space+4:] command(COMMAND) else: print ("Oh, never mind.") except Exception as e: print(e)
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 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 __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, 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 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 main(): # obtain audio from the microphone r = sr.Recognizer() with sr.Microphone() as source: print("Say something!") r.listen_in_background(source, recognize) # ----------------------------
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 helpint(): subprocess.call("runuser -l pi -c 'cvlc --play-and-exit /home/pi/computer/voice/help/intro.mp3'", shell=True) with sr.Microphone() as source: print("Waiting for initialisation") r.adjust_for_ambient_noise(source) audio = r.listen(source) helpreq = r.recognize_google(audio) response(helpreq) return
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 loop(self): with Microphone() as source: if not self.adjusted: self.recognizer.adjust_for_ambient_noise(source) self.adjusted = True audio = self.recognizer.listen(source) text = self.process_audio(audio) if text: subj = speech._ subj.speechContent = text return subj
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
def Listen(self): with sr.Microphone() as source: self.audio = self.rec.listen(source) try: self.result = self.rec.recognize_google(self.audio) print self.result return self.result except sr.UnknownValueError: print("Google Speech Recognition could not understand audio") except sr.RequestError: self.Say("Could not request results from Google Speech Recognition service master, check our internet connection.")
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)
def listen(recognizer, message='Say Something'): with speech_recognition.Microphone() as source: print(message) audio = recognizer.listen(source) try: return recognizer.recognize_google(audio).lower() except speech_recognition.UnknownValueError: try: return recognizer.recognize_sphinx(audio).lower() except speech_recognition.UnknownValueError: print("Pandora could not understand audio") except speech_recognition.RequestError as e: print("Sphinx error; {0}".format(e)) except speech_recognition.RequestError as e: print("Could not request results from Google Speech Recognition service; {0}".format(e))
def v2t(self): listening = True; while listening: r = sr.Recognizer() with sr.Microphone() as source: audio = r.listen(source) engine = pyttsx3.init() text = r.recognize_google(audio) if text == "quit listening": listening = False else: v = self.n.get(); self.n.set(v)
def detected_callback(self): self.detector.terminate() play_wav(onyx.__path__[0] + "/client/speech/resources/ding.wav") r = sr.Recognizer() with sr.Microphone() as source: print("Say something!") audio = r.listen(source, timeout=1, phrase_time_limit=5) try: result = stt.execute(audio, language=self.lang) print("You said: " + result) def create_ws(): def onConnected(event=None): print ("Sending message...") payload = { 'utterances': [result] } ws.emit(Message('recognizer_loop:utterance', payload)) t.close() #self.detector.start(self.detected_callback) ws = WebsocketClient() ws.on('connected', onConnected) # This will block until the client gets closed ws.run_forever() t = threading.Thread(target=create_ws) t.start() time.sleep(2) self.detector.start(self.detected_callback) except sr.UnknownValueError: print("Speech Recognition could not understand audio") except sr.RequestError as e: print("Could not request results from Speech Recognition service; {0}".format(e))
def get_command(): 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 #Read keys from json with open('keys.json','r') as keys_file: keys = json.load(keys_file) apiai_client_token = keys['apiai_client_token'] #Create a session session = requests.Session() session.headers.update({"Authorization":"Bearer {}".format(apiai_client_token), "Content-Type":"application/json; charset=utf-8"}) #API.ai API_BASE_URL="https://api.api.ai/v1/" #Make a request return session.get(API_BASE_URL+"query", params={"query": tts_result,"v":"20170204","sessionId":"furby","lang":"en"}).json()["result"]
def ears(): # obtain audio from the microphone r = sr.Recognizer() with sr.Microphone() as source: audio = r.listen(source) # recognize speech using Google Speech Recognition try: return r.recognize_google(audio) except sr.UnknownValueError: return ears() except sr.RequestError as e: return "I do not understand; {0}".format(e)
def listen(): speak('Listening!') with speech_recognition.Microphone() as source: recognizer.adjust_for_ambient_noise(source) audio = recognizer.listen(source) try: return recognizer.recognize_google(audio) except speech_recognition.UnknownValueError: notify(message="Could not understand audio") except speech_recognition.RequestError as e: notify(message="Connection Problem") return ""
def get_speech(): with sr.Microphone() as source: r.adjust_for_ambient_noise(source) print '\n>>>', audio = r.listen(source) print ">", try: speech = r.recognize_google(audio) except: speech = "No input detected." print speech return speech
def listen_translate(): while(True): # obtain audio from the microphone r = sr.Recognizer() with sr.Microphone(sample_rate=8000) as source: print("Say something!") # print(5), # time.sleep(1) # print(4), # time.sleep(1) # print(3), # time.sleep(1) # print(2), # time.sleep(1) # print(1), # time.sleep(1) audio = r.listen(source)#,timeout=5,phrase_time_limit=0.05 # r = sr.Recognizer() # with sr.AudioFile('./english.wav') as source: # audio = r.record(source) # read the entire audio file # write audio to a WAV file ``
with open("microphone-results.wav", "wb") as f: f.write(audio.get_wav_data()) # recognize speech using Sphinx try: print("Sphinx thinks you said :" + r.recognize_sphinx(audio)) except sr.UnknownValueError: print("Sphinx could not understand audio") except sr.RequestError as e: print("Sphinx error; {0}".format(e))
```
def listen_and_recognize(): r = sr.Recognizer() m = sr.Microphone(sample_rate=8000) r.listen_in_background(m,callback,phrase_time_limit=1) while(True): lastlen=0 if len(audiolist)==0: time.sleep(10) continue if lastlen==len(audiolist): time.sleep(10) continue output = wave.open('microphone-results.wav', 'wb') output.setnchannels(1) setparam=False para=None for audio in audiolist: with open("temps.wav", "wb") as f: f.write(audio.get_wav_data()) temps = wave.open('temps.wav', 'rb') #print temps.getparams() if not setparam: para=temps.getparams() output.setparams(para) setparam=True output.writeframes(temps.readframes(temps.getnframes())) output.close() # output = wavefile.open('microphone-results.wav', 'rb') # outputaudio=sr.AudioData(output.readframes(output.getnframes()),para[2],para[1]) # translate(r,outputaudio) #baidu('microphone-results.wav') lastlen=len(audiolist) time.sleep(10)
def processcommand(self, usermsg, source): """ Processes command from user and deals with wake word detection Returns True if Response was when assistant is awoken :param usermsg: The message that the user inputted :param source: Microphone audio source """ print ("< " + usermsg) wake_words = ["hey", "ok", "okay", "yo", "you", "hi"] awake = False for word in wake_words: if self.userCommand == word + " " + self.name: awake = True if awake: self.playsound("start.mp3") self.speak("Yes?") print ("\tWaiting for command..") response_heard = False while not response_heard: print ("\tThreshold: " + str(self.r.energy_threshold)) print ("\tWaiting for command...") try: audio = self.r.listen(source, timeout=5) print("...") self.playsound("end.mp3") try: self.userCommand = self.r.recognize_google(audio) self.userCommand = self.userCommand.lower() response_heard = True print ("< " + self.userCommand) except sr.UnknownValueError: print("\tUnknown Value from Google, or nothing heard") 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 ("\tHeard nothing") pass return True else: for word in wake_words: if self.userCommand.__contains__(word + " " + self.name): print ("\tGetting command..") self.userCommand = self.userCommand.split(word + " " + self.name + " ")[1] return True return False
def initiate(self): print("Stephanie is on and loading, wait for the beep sound to give your command.") if self.c.config.getboolean("APPLICATION", "update_check"): self.updater.check_for_update() self.status = True if self.c.config.getboolean("SYSTEM", "wake_up_engine"): self.status = False self.active = False if self.c.config.getboolean("SYSTEM", "always_on_engine"): self.status = False self.active = False r = sr.Recognizer() act = Activity(sr, r, self.events) assistant = VirtualAssistant(sr, r, self.events) if self.c.config.getboolean("SYSTEM", "wake_up_engine"): while not self.active: with sr.Microphone() as source: self.active = act.check(source) self.status = self.active self.events.sleep_status = not self.status if self.active: self.speaker.speak("How may I help you?") while self.status: with sr.Microphone() as source: assistant.main(source) if self.events.active_status: self.status = False self.active = True elif self.events.sleep_status: self.status = False self.active = False elif self.c.config.getboolean("SYSTEM", "always_on_engine"): while not self.active: with sr.Microphone() as source: self.active = act.check_always_on(source) self.status = self.active if self.active: while self.status: with sr.Microphone() as source: assistant.main(source) self.status = False self.active = False if self.events.active_status: self.status = False self.active = True else: self.speaker.speak("How may I help you?") while self.status: with sr.Microphone() as source: assistant.main(source) if self.events.active_status: self.status = False
def callback(self,data): i=0 rg=spr.Recognizer() try: frame = self.bridge.imgmsg_to_cv2(data, "bgr8") frame = libs.resize(frame, width=600) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) (rects, i, facess) = et.track(gray, i) for rect in rects: cv2.rectangle(frame, (rect[0], rect[1]), (rect[2], rect[3]), (0, 255, 0), 2) if facess != []: for face in facess: pred, conf = recognizer.predict(face) if conf < 120: print "Reconozco a Lucas con una confianza de {}".format(conf) self.num=self.num+1 if self.num==10: self.engine.say('Hi ') self.engine.say( list(dictid.keys())[list(dictid.values()).index(pred)]) self.engine.runAndWait() with spr.Microphone() as source: rg.adjust_for_ambient_noise(source) print 'Escuchando' audio=rg.listen(source) try: respuesta= rg.recognize_sphinx(audio) print respuesta if respuesta!='no': self.engine.say('OKEY ') self.engine.say('Getting') self.engine.say('new') self.engine.say('data') self.engine.runAndWait() except spr.UnknownValueError: print 'error' else: print "Desconocido" cv2.imshow("Tracking", frame) cv2.waitKey(1) except CvBridgeError as e: print(e)
def get_command(): 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) except sr.UnknownValueError: print("Google Speech Recognition could not understand audio") exit() except sr.RequestError as e: print("Could not request results from Google Speech Recognition service; {0}".format(e)) exit() #Read keys from json with open('keys.json','r') as keys_file: keys = json.load(keys_file) apiai_client_token = keys['apiai_client_token'] #Create a session session = requests.Session() session.headers.update({"Authorization":"Bearer {}".format(apiai_client_token), "Content-Type":"application/json; charset=utf-8"}) #API.ai API_BASE_URL="https://api.api.ai/v1/" #Make a request return session.get(API_BASE_URL+"query", params={"query": tts_result,"v":"20170204","sessionId":"furby","lang":"en"}).json()["result"] #bee_movie, #get_date, #get_forecast, #get_fortune, #prompt_name (for get lucky number), #get_stallman, #torture, #get_time, #prompt_question (for wolfram query) # # joke # dad joke # love # music
def write_by_speak(self): r = sr.Recognizer() with sr.Microphone() as source: r.adjust_for_ambient_noise(source, duration=1) # print(r.energy_threshold) # print("Chucking rate: ", source.CHUNK) # print("format rate :", source.format) # Debuggin purpose # CHUNK = 1024 # FORMAT = pyaudio.paInt16 # the Format is picked up from the pyaudio # CHANNELS = 2 # The Cross Channels # # RATE = 44100 # source.CHUNK = CHUNK # source.format = FORMAT # FORMATING THE SOURCE FILE # print(dir(source)) print("Say something!...") # print(r.energy_threshold) r.energy_threshold += 280 # # print(r.adjust_for_ambient_noise(source,duration=1)) audio = r.listen(source) # Speech recognition using Google Speech Recognition try: print("Parsing ...") # Debugging To # 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.energy_threshold ) # print(help(r.recognize_google)) # text = r.recognize_google(audio, language='en-US') text = r.recognize_google(audio, language='en-GB') # Recognizing the command through the google # r.re # r.re print("You said: " + text) return text except sr.UnknownValueError: print("Google Speech Recognition could not understand audio") return except sr.RequestError as e: print("Could not request results from Google Speech Recognition service; {0}".format(e)) return except sr.HTTPError as e: print("Couldn't connect to the websites perhaps , Hyper text transfer protocol error; {0}".format(e)) return # returning for the debugging