def listen(self) -> str: # Record Audio data = '' recognizer = Recognizer() with Microphone() as source: self.log.debug('Listening on microphone...') try: audio = recognizer.listen(source, timeout=10) except WaitTimeoutError: self.log.debug('Listen timed out.') return data # Speech recognition using Google Speech Recognition try: # Uses the default API key # To use another API key: `r.recognize_google(audio, key='GOOGLE_SPEECH_RECOGNITION_API_KEY')` data = recognizer.recognize_google(audio) self.log.debug(f'You said: "{data}"') except UnknownValueError: self.log.debug( 'Google Speech Recognition could not understand audio') except RequestError as e: self.log.error( f'Could not request results from Google Speech Recognition service: {e}' ) return str(data)
def task(quit_event): #mic = Microphone(quit_event=quit_event) mic_index = None for i, microphone_name in enumerate(Microphone.list_microphone_names()): if 'seeed' in microphone_name: mic_index = i print("Using microphone {}".format(microphone_name)) break if not mic_index: print("Could not find a proper microphone") exit() with Microphone(device_index=mic_index) as mic: recognizer = Recognizer() while not quit_event.is_set(): pixel_ring.off() print("Listening for keyword") data = recognizer.listen(source=mic) kw_text = recognizer.recognize_sphinx(data) print("Heard '{}' while listening for keyword".format(kw_text)) if kw_text == name: print('Wake up') pixel_ring.listen() data = recognizer.listen(mic) pixel_ring.think() text = recognizer.recognize_sphinx(data) print('Done listening') pixel_ring.off() if text: print('Recognized {}'.format(text)) tts.say(text)
def Audio_file_Read(filename): universal_dict = {} cnt = {} gantu = [0, 0, 0, 0] analysis = {} token = Tokenizer() recog = Recognizer() try: audioFile = sr.AudioFile(filename) with audioFile as source: audio = recog.record(source) recognized = recog.recognize_google(audio, language="ko-KR") res = text_to_word_sequence(recognized) cnt = collections.Counter(res) universal_dict = dict(cnt) if "어" in universal_dict: gantu[0] = universal_dict["어"] if "아니" in universal_dict: gantu[1] = universal_dict["아니"] if "근데" in universal_dict: gantu[2] = universal_dict["근데"] if "이제" in universal_dict: gantu[3] = universal_dict["이제"] text = recognized analysis['text'] = text analysis['data'] = gantu return analysis except UnknownValueError: analysis['text'] = "당신이 말한 문장이 없습니다." analysis['data'] = [0, 0, 0, 0] return analysis
def record(filename, duration): mic = MutableMicrophone() recognizer = Recognizer() with mic as source: audio = recognizer.record(source, duration=duration) with open(filename, 'wb') as f: f.write(audio.get_wav_data())
def listen(self): """ listen audio from microphone and convert to the string. :return:data """ self.recognizer = Recognizer() with Microphone() as source: self.recognizer.adjust_for_ambient_noise(source) print("I am listening you...") audio = self.recognizer.listen(source, phrase_time_limit=6) data = "" try: # Uses the default API key # To use another API key: # `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")` data = self.recognizer.recognize_google(audio, language="tr") print("You said : " + data) except UnknownValueError: print("Google Speech Recognition could not understand audio") return self.listen() except RequestError as exception: print("Could not request results from " "Google Speech Recognition service; {0}".format(exception)) return self.listen() return data.lower()
def __init__(self, callback, language="pl-PL", api_option=ApiOption.GOOGLE): super().__init__() self.callback_command_detected = callback self.api_option = api_option self.language = language self.listen_thread = None self.phrase_time_limit = 3 try: self.recognizer = Recognizer() self.microphone = Microphone() """ adjust the recognizer sensitivity to ambient noise and record audio from the microphone """ with self.microphone as source: self.recognizer.adjust_for_ambient_noise(source) except OSError as ose_err: Logger.critical("OSError: {0}".format(ose_err)) self.api_runs = False self.api_info = GLO_MSG['MICROPHONE_FAILURE'] return except Exception as err: Logger.critical("Exception: {0}".format(err)) self.api_runs = False self.api_info = GLO_MSG['MICROPHONE_FAILURE'] return Logger.debug("Initialization of CommandsRecognition class") self.api_info = GLO_MSG['MICROPHONE_INITIALIZED']
def __init__(self): config_core = STT_CONFIG self.lang = str(self.init_language(config_core)) config_stt = config_core.get("stt", {}) self.config = config_stt.get(config_stt.get("module"), {}) self.credential = self.config.get("credential", {}) self.recognizer = Recognizer()
def __init__(self): recognizer = Recognizer() recognizer.dynamic_energy_threshold = False recognizer.energy_threshold = 1000 self.recognizer = recognizer self.microphone = Microphone() self.susi = susi 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['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): config_core = ConfigurationManager.get() self.lang = str(self.init_language(config_core)) config_stt = config_core.get("stt", {}) self.config = config_stt.get(config_stt.get("module"), {}) self.credential = self.config.get("credential", {}) self.recognizer = Recognizer()
def main(): # start to listen background with another thread. start_listen_background() while not os.path.exists(BG_WAV_PATH): print('ready for bg wav ...') time.sleep(1) # initialize recognizer recognizer = Recognizer() recognizer.speaking_duration = 0.1 recognizer.phrase_threshold = 0.1 with Microphone(sample_rate=SAMPLE_RATE) as source: # listen for 1 second to calibrate the energy threshold for ambient noise levels recognizer.adjust_for_ambient_noise(source) print("Calibrated. Say something!") source = Microphone(sample_rate=SAMPLE_RATE) with Pool(THREAD_NUM) as pool: callback_with_model = partial(callback, model_map=ModelMap(), pool=pool) recognizer.listen_in_background(source, callback_with_model, PHRASE_TIME_LIMIT) while True: time.sleep(10)
def __init__(self, pin: int = 17, language: str = "en", api: str = "", mic=None) -> None: """ Initialize LED lights, button, language, microphone, and recognizer. pin: tell Pi what pin to use for LED and set it's default value to 0 (off) language: set language to the language you are translating from microphone: initialize microphone so it can be used in this program recognizer: take input from microphone and decipher it into text Microphone.list_microphone_names() to list all microphones Pass the needed device as device_index=n if program can't pick up device automatically """ self.microphone = Microphone( device_index=mic) if mic else Listener.get_mic() self.recognizer = Recognizer() self.led = PWMLED(pin) self.src = language self.target = "es" self.api = api
def GetTextInAudio(audio_msg, chat_id, bot): from speech_recognition import AudioFile, Recognizer, UnknownValueError rec = Recognizer() file_id = audio_msg['file_id'] tempAudio = dirLoc + file_id + '.' + audio_msg['mime_type'].split('/')[1] bot.download_file(file_id, tempAudio) from pydub import AudioSegment filename = file_id + '.wav' file_loc = dirLoc + filename sound = AudioSegment.from_file(tempAudio) sound.export(file_loc, format='wav') with AudioFile(file_loc) as AudioSrc: content = rec.record(AudioSrc) try: text = rec.recognize_google( audio_data=content, language='ja-JP', ) except UnknownValueError: bot.sendMessage(chat_id, 'None') return mainText = t_j2k(text) bot.sendMessage(chat_id, mainText) remove(tempAudio) remove(file_loc)
def __init__(self): config_core = CONFIGURATION self.lang = str(self.init_language(config_core)) config_stt = config_core.get("stt", {}) self.config = config_stt.get(config_stt.get("module"), {}) self.credential = self.config.get("credential", {}) self.recognizer = Recognizer() self.can_stream = False
def __init__(self): Thread.__init__(self, daemon=True); self.rec = Recognizer(); self.rec.energy_threshold=110; self.rec.pause_threshold=0.5; self.rec.operation_timeout=5; self.mic = Microphone(device_index = 2); self.answer = AnswerBot('de.txt');
def escuchar(): print("Escuchando...") recognizer = Recognizer() microfono = Microphone() with microfono: recognizer.adjust_for_ambient_noise(microfono) recognizer.listen_in_background(microfono, callback)
def __init__(self, renderer=None): try: import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.OUT) GPIO.setup(27, GPIO.OUT) GPIO.setup(22, GPIO.OUT) except ImportError: print("Only available for devices with GPIO ports ") except RuntimeError: pass 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'], country_name=res['country'], country_code=res['countryCode']) 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.snowboy_detector import SnowboyDetector self.hotword_detector = SnowboyDetector() else: from main.hotword_engine.sphinx_detector import PocketSphinxDetector self.hotword_detector = PocketSphinxDetector() if self.config['WakeButton'] == 'enabled': print("\nSusi has the wake button enabled") if self.config['Device'] == 'RaspberryPi': print("\nSusi runs on a RaspberryPi") from ..hardware_components import RaspberryPiWakeButton self.wake_button = RaspberryPiWakeButton() else: print("\nSusi is not running on a RaspberryPi") self.wake_button = None else: print("\nSusi has the wake button disabled") self.wake_button = None
def dothis(message): """ From speech to text :param message: :return: text """ session = message.get_session() ans = '' current_cmd = message.get_setting(session, 'active') if message.attachments['sound']: try: r = Recognizer() mode = 'google' lang = 'ru-RUS' ans = '' for attachment in message.attachments['sound']: ext = attachment[1] path = os.path.abspath(os.curdir) fname = time.strftime("%Y%m%d-%H%M%S") + '.' dir = path + '/temp/' + fname urllib.request.urlretrieve( attachment[0], dir + ext) # getting file if ext != 'wav': subprocess.run(['ffmpeg', '-i', dir + ext, dir + 'wav']) os.remove(dir + ext) with AudioFile(dir + 'wav') as source: song = r.record(source) os.remove(dir + 'wav') if "en" in message.params: lang = 'en-EN' if 'wit' in message.params: mode = 'wit' recg = r.recognize_google( song, language=lang ) if mode == 'google' else r.recognize_wit(song, witkey) ans += f">>>>>>{recg}\n\n" yield ans except Exception as f: ans += "Произошла непредвиденная ошибка: " + str(f) + "\n" finally: if current_cmd: message.delete_active(session) yield str(ans) elif 'Выход' in message.params and current_cmd: message.delete_active(session) yield {'msg': 'Успешно!', 'keyboard': [[], False]} else: if current_cmd is None: message.add_setting(session, 'active', 'stt') yield {'msg': 'Прикрепите аудио или напишите Выход', 'keyboard': [[[('Выход', 'negative')]], False] }
def __init__(self): rospy.init_node("speech") self.pub = rospy.Publisher("speech_recognizer", String, latch=True, queue_size=1) self.recognizer = Recognizer() # self.recognizer.energy_threshold = 1000 # self.recognizer.pause_threshold = .7 self.microphone = Microphone()
def listen(self): try: with Microphone() as source: recognizer = Recognizer() recognizer.adjust_for_ambient_noise(source) audio = recognizer.listen(source) return recognizer.recognize_google(audio, language=self.lang).lower() except (UnknownValueError, RequestError): return ''
def gettingWordsFromMic(): mic = Microphone() recognizer = Recognizer() print("Say something...") print(Microphone.list_microphone_names()) with mic as source: recognizer.adjust_for_ambient_noise(source) audio = recognizer.listen(source) input("Press a key to process") print(recognizer.recognize_google(audio))
def __init__(self, width, height): super().__init__(width, height) arcade.set_background_color(arcade.color.AMAZON) self.microphone = Microphone() self.recognizer = Recognizer() # with self.microphone as source: # self.recognizer.adjust_for_ambient_noise(source, duration=2.0) self.recognizer.energy_threshold = 5000 self.stop_listening = None
def sphinx(config, data, sample_rate): """Perform speech recognition using sphinx.""" audio_data = AudioData(data, sample_rate, 2) try: text = Recognizer().recognize_sphinx(audio_data, language="en-US", keyword_entries=None, show_all=False) except UnknownValueError: text = "" _LOGGER.warning("No speech found in audio.") return text
def listen(): print("Say something!") r = Recognizer() m = Microphone() with m as src: # Deal with ambient noise since microphone output is rather unpredictable r.adjust_for_ambient_noise(src) # Start listening r.listen_in_background(m, callback) # Keep listening even though main thread is blocked while True: sleep(1)
def speech_recog(file_name="input_sample/audio/introduction_ml.mp3", duration=10): convert_mp3_to_wav("input_sample/audio/introduction_ml.mp3") result = 0 class_audio = AudioFile("input_sample/audio/introduction_ml.wav") print(type(class_audio)) recongizer = Recognizer() with class_audio as src_audio: audio = recongizer.record(src_audio, duration=duration) print(recongizer.recognize_google(audio))
def google_cloud(config, data, sample_rate): """Perform speech recognition using Google Cloud.""" audio_data = AudioData(data, sample_rate, 2) try: text = Recognizer().recognize_google_cloud(audio_data, credentials_json=None, language="en-GB", preferred_phrases=None, show_all=False) except UnknownValueError: text = "" _LOGGER.warning("No speech found in audio.") return text
def start(data): if data.endswith('.wav'): data = converter(data) else: print("Need Conversion") r = Recognizer() with AudioFile(data) as source: audio = r.listen(source) print("Status: Working\r", end="") query = r.recognize_google(audio) file = open("{}.txt".format(data.split('.')[0]), 'w') file.write(query) file.close() print(query)
def __init__(self, renderer=None): GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.OUT) GPIO.setup(27, GPIO.OUT) GPIO.setup(22, GPIO.OUT) 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 audio_to_text(message_input): # initialise the recognizer r = Recognizer() # Use the sysdefault microphone for i, microphone_name in enumerate(Microphone.list_microphone_names()): if microphone_name == "sysdefault": micro = Microphone(device_index=i) with micro as source: # Extract the audio and convert it to text audio = r.listen(source) # recognize speech using Google Speech Recognition and add it to the text input area try: message_input.setText(r.recognize_google(audio)) except UnknownValueError: message_input.setText('The audio was not understood')
def myCommand(self): #listens for commands r = Recognizer() with Microphone() as source: print('Say something...') r.pause_threshold = 1 r.adjust_for_ambient_noise(source, duration=1) audio = r.listen(source) try: command = r.recognize_google(audio).lower() print('You said: ' + command + '\n') # loop back to continue to listen for commands if unrecognizable speech is received except UnknownValueError: print('....') command = speech.myCommand(self) return command
def get_audio(): rObject = Recognizer() audio = '' with Microphone() as source: print("Speak...") # recording the audio using speech recognition audio = rObject.listen(source, phrase_time_limit=5) print("Stop.") # limit 5 secs try: text = rObject.recognize_google(audio, language='en-US') print("You: ", text) return text except: chatbot_speaks("Could not understand your audio, Please try again !") return 0