def test_lattice(self): ps = Pocketsphinx() ps.decode() lattice = ps.get_lattice() self.assertEqual(lattice.write('tests/goforward.lat'), None) lattice = ps.get_lattice() self.assertEqual(lattice.write_htk('tests/goforward.htk'), None)
class TestRawDecoder(TestCase): def __init__(self, *args, **kwargs): self.ps = Pocketsphinx() self.ps.decode() super(TestRawDecoder, self).__init__(*args, **kwargs) def test_raw_decoder_lookup_word(self): self.assertEqual(self.ps.lookup_word('hello'), 'HH AH L OW') self.assertEqual(self.ps.lookup_word('abcdf'), None) def test_raw_decoder_hypothesis(self): self.assertEqual(self.ps.hypothesis(), 'go forward ten meters') self.assertEqual(self.ps.score(), -7066) self.assertEqual(self.ps.confidence(), 0.04042641466841839) def test_raw_decoder_segments(self): self.assertEqual(self.ps.segments(), [ '<s>', '<sil>', 'go', 'forward', 'ten', 'meters', '</s>' ]) def test_raw_decoder_best_hypothesis(self): self.assertEqual(self.ps.best(), [ ('go forward ten meters', -28034), ('go for word ten meters', -28570), ('go forward and majors', -28670), ('go forward and meters', -28681), ('go forward and readers', -28685), ('go forward ten readers', -28688), ('go forward ten leaders', -28695), ('go forward can meters', -28695), ('go forward and leaders', -28706), ('go for work ten meters', -28722) ])
class TestPhoneme(TestCase): def __init__(self, *args, **kwargs): self.ps = Pocketsphinx( lm=False, dic=False, allphone='deps/pocketsphinx/model/en-us/en-us-phone.lm.bin', lw=2.0, pip=0.3, beam=1e-200, pbeam=1e-20, mmap=False ) self.ps.decode() super(TestPhoneme, self).__init__(*args, **kwargs) def test_phoneme_hypothesis(self): self.assertEqual( self.ps.hypothesis(), 'SIL G OW F AO R W ER D T AE N M IY IH ZH ER Z S V SIL' ) def test_phoneme_best_phonemes(self): self.assertEqual(self.ps.segments(), [ 'SIL', 'G', 'OW', 'F', 'AO', 'R', 'W', 'ER', 'D', 'T', 'AE', 'N', 'M', 'IY', 'IH', 'ZH', 'ER', 'Z', 'S', 'V', 'SIL' ])
def test_cep_decoder_hypothesis(self): ps = Pocketsphinx() with open('deps/pocketsphinx/test/data/goforward.mfc', 'rb') as f: with ps.start_utterance(): f.read(4) buf = f.read(13780) ps.process_cep(buf, False, True) self.assertEqual(ps.hypothesis(), 'go forward ten meters') self.assertEqual(ps.score(), -7095) self.assertEqual(ps.probability(), -32715)
def __init__(self, *args, **kwargs): self.ps = Pocketsphinx( lm=False, dic=False, allphone='deps/pocketsphinx/model/en-us/en-us-phone.lm.bin', lw=2.0, pip=0.3, beam=1e-200, pbeam=1e-20, mmap=False ) self.ps.decode() super(TestPhoneme, self).__init__(*args, **kwargs)
class PocketSphinxWUW(WUWInterface): def __init__(self, keyword: str, kws_threshold: float): self._decoder = Pocketsphinx(keyphrase=keyword, lm=False, kws_threshold=kws_threshold) self._sound = pyaudio.PyAudio() self._audio_stream = self._sound.open(rate=_SAMPLE_RATE, channels=1, format=pyaudio.paInt16, input=True, frames_per_buffer=_FRAME_LENGTH) def prepare(self) -> None: print("starting utterance") self._audio_stream.start_stream() self._decoder.start_utt() print("started utterance") def process(self) -> bool: buf = self._audio_stream.read(_FRAME_LENGTH) if buf: self._decoder.process_raw(buf, False, False) else: return False if self._decoder.hyp(): print(self._decoder.hyp().hypstr) # print([(seg.word, seg.prob, seg.start_frame, seg.end_frame) for seg in self._decoder.seg()]) # print("Detected keyphrase, restarting search") # for best, i in zip(self._decoder.nbest(), range(10)): # print(best.hypstr, best.score) print("ending utterance") self._decoder.end_utt() self._audio_stream.stop_stream() print("ended utterance") return True return False def terminate(self) -> None: if self._audio_stream is not None: self._audio_stream.close() if self._sound is not None: self._sound.terminate()
def __init__(self): model_path = get_model_path() print(model_path) data_path = get_data_path() config = { 'hmm' : os.path.join(model_path, 'en-us'), # Hidden Markov Model, Speech Recongnition model - trained probability scoring system 'lm': os.path.join(model_path, 'en-us.lm.bin'), #language model 'dict' : os.path.join(model_path, 'testdict.dict')#, # language dictionary } #Start PocketSphinx Deocde self.ps = Pocketsphinx(**config) # Variables for Audio self.micbuf = np.zeros((0, 4), 'uint16') self.outbuf = None self.buffer_stuff = 0 self.audio_level = 0 self.timeofclap = 0 self.playchan = 0 self.playsamp = 0 self.startTime = 0 self.TimeSinceLast = 0 self.DemoPause = False self.PID = '' self.velocity = TwistStamped() # Variables for Illumination self.illum = UInt32MultiArray() self.illum.data = [0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF] self.illumInt = 0 self.illumState = 0 # robot name topic_base_name = "/" + os.getenv("MIRO_ROBOT_NAME") #Publisher for Illum to control LED's while we are processing requests topic = topic_base_name + "/control/illum" self.pub_illum = rospy.Publisher(topic, UInt32MultiArray, queue_size=0) self.velocity_pub = rospy.Publisher(topic_base_name + "/control/cmd_vel", TwistStamped, queue_size=0) # subscribe topic = topic_base_name + "/sensors/mics" self.sub_mics = rospy.Subscriber(topic, Int16MultiArray, self.callback_mics, queue_size=1, tcp_nodelay=True)
def detect(): from pocketsphinx import Pocketsphinx, Ad ad = Ad(None, 16000) # default input decoder = Pocketsphinx(lm=False, hmm=hmm, dic=dic, keyphrase=keyphrase, kws_threshold=kws_threshold) buf = bytearray(2048) with ad: with decoder.start_utterance(): while ad.readinto(buf) >= 0: decoder.process_raw(buf, False, False) if decoder.hyp(): with decoder.end_utterance(): logging.info('Wake word detected for %s' % system) wake_statuses[system] = 'detected' break
def decode(): nonlocal decoder, decoded_phrase # Dynamically load decoder if decoder is None: _LOGGER.debug('Loading decoder') hass.states.async_set(OBJECT_POCKETSPHINX, STATE_LOADING, state_attrs) decoder = Pocketsphinx( hmm=acoustic_model, lm=language_model, dic=dictionary) hass.states.async_set(OBJECT_POCKETSPHINX, STATE_DECODING, state_attrs) # Do actual decoding with decoder.start_utterance(): decoder.process_raw(recorded_data, False, True) # full utterance hyp = decoder.hyp() if hyp: with decoder.end_utterance(): decoded_phrase = hyp.hypstr decoded_event.set()
def __init__(self): # state self.micbuf = np.zeros((0, 4), 'uint16') self.spkrbuf = None self.buffer_stuff = 0 # robot name topic_base = "/" + os.getenv("MIRO_ROBOT_NAME") + "/" # publish topic = topic_base + "control/stream" print ("publish", topic) self.pub_stream = rospy.Publisher(topic, Int16MultiArray, queue_size=0) # subscribe topic = topic_base + "sensors/stream" print ("subscribe", topic) self.sub_stream = rospy.Subscriber(topic, UInt16MultiArray, self.callback_stream) # subscribe topic = topic_base + "sensors/mics" print ("subscribe", topic) self.sub_mics = rospy.Subscriber(topic, Int16MultiArray, self.callback_mics) # report print "recording on 4 microphone channels..." ####### Speech Recongnition using Pocket-Sphinx ######### #obtain audio from microphone r = sr.Recognizer() with sr.callback_mics() as source: print("Say Hello") audio = r.listen(source) #write audio as a wav file with open("./tmp/input.wav", "wb") as f: f.write(audio.get_wav_data()) model_path = get_model_path() data_path = get_data_path() config = { 'hmm' : os.path.join(model_path, 'en-us'), # Hidden Markov Model, Speech Recongnition model - trained probability scoring system 'lm': os.path.join(model_path, 'en-us.lm.bin'), #language model 'dict' : os.path.join(model_path, 'cmudict-en-us.dict') # language dictionary } ps = Pocketsphinx(**config) ps.decode( audio_file=os.path.join(data_path, "./tmp/input.wav"),#add temp input.wav file buffer_size=2048 no_search= False, full_utt=False ) print(ps.hypothesis()) ## output
class Sphinx(Thread): def __init__(self): Thread.__init__(self) self.ready = False def run(self): print_important("Info! Thread sphinx started.") self.config = { 'verbose': True, 'hmm': os.path.join('s2m', 'core', 'sphinx', 'fr'), 'lm': os.path.join('s2m', 'core', 'sphinx', 'fr.lm.dmp'), 'dict': os.path.join('s2m', 'core', 'sphinx', 's2m.dict'), 'jsgf': os.path.join('s2m', 'core', 'sphinx', 's2m.jsgf'), } self.pocketsphinx = Pocketsphinx(**self.config) self.ready = True def get_silence(self, duration): if duration < 0.25: return '[veryshortsil]' elif duration < 0.5: return '[shortsil]' elif duration < 1.5: return '[sil]' elif duration < 3.: return '[longsil]' else: return '[verylongsil]' def get_segment_string(self, segments): segment_list = [] last_silence = 0 spoken_duration = 0 word_count = 0 for segment in segments: if segment.word in ['<s>', '</s>']: continue elif segment.word == '<sil>': last_silence += segment.end_frame - segment.start_frame else: if last_silence > 0: segment_list.append(last_silence) last_silence = 0 spoken_duration += segment.end_frame - segment.start_frame segment_list.append(segment.word) word_count += 1 if word_count == 0: return '' avg_word_duration = spoken_duration / word_count return ' '.join((self.get_silence(s / avg_word_duration) if type(s) is int else nobrackets(s)) for s in segment_list) def to_text(self, filename, erase=False): if not self.ready: raise EnvironmentError('Initialization of sphinx not finished.') FILLER_WORDS = ['<s>', '<sil>', '</s>'] try: self.pocketsphinx.decode(filename) except Exception as e: print("An error was raised by sphinx while decoding file '%r', parsing aborted" % filename) text = " ".join( [s for s in self.pocketsphinx.segments() if s not in FILLER_WORDS]) text = nobrackets(text) segment_string = self.get_segment_string(self.pocketsphinx.seg()) nbest = [nobrackets(w[0]) for w in self.pocketsphinx.best(count=10)[1:]] if erase: os.remove(loc) return segment_string, nbest
from pocketsphinx import Pocketsphinx print(Pocketsphinx().decode()) # => "go forward ten meters"
from __future__ import print_function import os from os import environ,path from pocketsphinx import Pocketsphinx, get_model_path, get_data_path model_path = get_model_path() data_path = path.join(path.dirname(path.realpath(os.curdir))+'\\pyexperiments\\data')#get_data_path() config = { 'hmm': os.path.join(model_path, 'en-us'), 'lm': os.path.join(model_path, 'en-us.lm.bin'), 'dict': os.path.join(model_path, 'cmudict-en-us.dict') } ps = Pocketsphinx(**config) ps.decode( audio_file=os.path.join(data_path, 'out.wav'), buffer_size=2048, no_search=False, full_utt=False ) #print(ps.segments()) # => ['<s>', '<sil>', 'go', 'forward', 'ten', 'meters', '</s>'] #print('Detailed segments:', *ps.segments(detailed=True), sep='\n') # => [ # word, prob, start_frame, end_frame # ('<s>', 0, 0, 24) # ('<sil>', -3778, 25, 45) # ('go', -27, 46, 63) # ('forward', -38, 64, 116) # ('ten', -14105, 117, 152) # ('meters', -2152, 153, 211)
def test_lm(self): ps = Pocketsphinx( dic='deps/pocketsphinx/test/data/defective.dic', mmap=False ) # Decoding with 'defective' dictionary ps.decode() self.assertEqual(ps.hypothesis(), '') # Switch to 'turtle' language model turtle_lm = 'deps/pocketsphinx/test/data/turtle.lm.bin' lm = NGramModel(ps.get_config(), ps.get_logmath(), turtle_lm) ps.set_lm('turtle', lm) ps.set_search('turtle') # Decoding with 'turtle' language model ps.decode() self.assertEqual(ps.hypothesis(), '') # The word 'meters' isn't in the loaded dictionary # Let's add it manually ps.add_word('foobie', 'F UW B IY', False) ps.add_word('meters', 'M IY T ER Z', True) # Decoding with 'turtle' language model ps.decode() self.assertEqual(ps.hypothesis(), 'foobie meters meters')
def test_lm(self): ps = Pocketsphinx(dic='deps/pocketsphinx/test/data/defective.dic', mmap=False) # Decoding with 'defective' dictionary ps.decode() self.assertEqual(ps.hypothesis(), '') # Switch to 'turtle' language model turtle_lm = 'deps/pocketsphinx/test/data/turtle.lm.bin' lm = NGramModel(ps.get_config(), ps.get_logmath(), turtle_lm) ps.set_lm('turtle', lm) ps.set_search('turtle') # Decoding with 'turtle' language model ps.decode() self.assertEqual(ps.hypothesis(), '') # The word 'meters' isn't in the loaded dictionary # Let's add it manually ps.add_word('foobie', 'F UW B IY', False) ps.add_word('meters', 'M IY T ER Z', True) # Decoding with 'turtle' language model ps.decode() self.assertEqual(ps.hypothesis(), 'foobie meters meters')
import os from pocketsphinx import Pocketsphinx, get_model_path, get_data_path import sys model_path = get_model_path() data_path = get_data_path() print "config set" config = { 'hmm': os.path.join(model_path, 'en-us'), 'lm': os.path.join(model_path, 'en-us.lm.bin'), 'dict': os.path.join(model_path, 'cmudict-en-us.dict') } ps = Pocketsphinx(**config) print "decoging" ps.decode(audio_file=sys.argv[1], buffer_size=2048, no_search=False, full_utt=False) print "decoded" print(ps.segments() ) # => ['<s>', '<sil>', 'go', 'forward', 'ten', 'meters', '</s>'] print "DETAILED SHIT" print(ps.segments(detailed=True)) # => [ # word, prob, start_frame, end_frame # ('<s>', 0, 0, 24) # ('<sil>', -3778, 25, 45) # ('go', -27, 46, 63) # ('forward', -38, 64, 116) # ('ten', -14105, 117, 152) # ('meters', -2152, 153, 211)
def __init__(self, vocabulary, hmm_dir="/usr/local/share/" + "pocketsphinx/model/hmm/en_US/hub4wsj_sc_8k"): """ Initiates the pocketsphinx instance. Arguments: vocabulary -- a PocketsphinxVocabulary instance hmm_dir -- the path of the Hidden Markov Model (HMM) """ self._logger = logging.getLogger(__name__) # quirky bug where first import doesn't work # try: # import pocketsphinx as ps # except Exception: # import pocketsphinx as ps from pocketsphinx import Pocketsphinx with tempfile.NamedTemporaryFile(prefix='psdecoder_', suffix='.log', delete=False) as f: self._logfile = f.name self._logger.debug("Initializing PocketSphinx Decoder with hmm_dir " + "'%s'", hmm_dir) # Perform some checks on the hmm_dir so that we can display more # meaningful error messages if neccessary if not os.path.exists(hmm_dir): msg = ("hmm_dir '%s' does not exist! Please make sure that you " + "have set the correct hmm_dir in your profile.") % hmm_dir self._logger.error(msg) raise RuntimeError(msg) # Lets check if all required files are there. Refer to: # http://cmusphinx.sourceforge.net/wiki/acousticmodelformat # for details missing_hmm_files = [] for fname in ('mdef', 'feat.params', 'means', 'noisedict', 'transition_matrices', 'variances'): if not os.path.exists(os.path.join(hmm_dir, fname)): missing_hmm_files.append(fname) mixweights = os.path.exists(os.path.join(hmm_dir, 'mixture_weights')) sendump = os.path.exists(os.path.join(hmm_dir, 'sendump')) if not mixweights and not sendump: # We only need mixture_weights OR sendump missing_hmm_files.append('mixture_weights or sendump') if missing_hmm_files: self._logger.warning("hmm_dir '%s' is missing files: %s. Please " + "make sure that you have set the correct " + "hmm_dir in your profile.", hmm_dir, ', '.join(missing_hmm_files)) # self._decoder = ps.Decoder(hmm=hmm_dir, logfn=self._logfile, # **vocabulary.decoder_kwargs) config = { 'hmm': hmm_dir, 'logfn': self._logfile } config.update(**vocabulary.decoder_kwargs) ps = Pocketsphinx(**config) self._decoder = ps.decode()
def transcribe(audiofile): return Pocketsphinx()\ .decode( audio_file = audiofile)\ .hypothesis()
exit() else: processAudio(file) elif args.daemon is True: frames = [] model_path = get_model_path() data_path = get_data_path() # This is the configuration for the pocketsphinx object config = { 'hmm': os.path.join(model_path, 'en-us'), 'lm': os.path.join(model_path, 'en-us.lm.bin'), 'dict': os.path.join(model_path, 'cmudict-en-us.dict') } ps = Pocketsphinx(**config) #PyAudio stuff chunk = 1024 sample_format = pyaudio.paInt16 # 16 bits per sample channels = 1 fs = 16000 # Record 16000 samples per second ouput = "dump-folder/audio/" # This is where the audio files are kept p = pyaudio.PyAudio() # Create an interface to PortAudio # See PyAudio Documentation stream = p.open(format=sample_format, channels=channels, rate=fs, frames_per_buffer=chunk,
def async_setup(hass, config): name = config[DOMAIN].get(CONF_NAME, DEFAULT_NAME) hotword = config[DOMAIN].get(CONF_HOTWORD) acoustic_model = os.path.expanduser(config[DOMAIN].get( CONF_ACOUSTIC_MODEL, DEFAULT_ACOUSTIC_MODEL)) dictionary = os.path.expanduser(config[DOMAIN].get(CONF_DICTIONARY, DEFAULT_DICTIONARY)) threshold = config[DOMAIN].get(CONF_THRESHOLD, DEFAULT_THRESHOLD) audio_device_str = config[DOMAIN].get(CONF_AUDIO_DEVICE, DEFAULT_AUDIO_DEVICE) sample_rate = config[DOMAIN].get(CONF_SAMPLE_RATE, DEFAULT_SAMPLE_RATE) buffer_size = config[DOMAIN].get(CONF_BUFFER_SIZE, DEFAULT_BUFFER_SIZE) detected_event = threading.Event() detected_phrase = None terminated = False from pocketsphinx import Pocketsphinx, Ad decoder = Pocketsphinx(hmm=acoustic_model, lm=False, dic=dictionary, keyphrase=hotword, kws_threshold=threshold) audio_device = Ad(audio_device_str, sample_rate) state_attrs = {'friendly_name': 'Hotword', 'icon': 'mdi:microphone'} @asyncio.coroutine def async_listen(call): nonlocal terminated, detected_phrase terminated = False detected_phrase = None hass.states.async_set(OBJECT_DECODER, STATE_LISTENING, state_attrs) def listen(): buf = bytearray(buffer_size) with audio_device: with decoder.start_utterance(): while not terminated and audio_device.readinto(buf) >= 0: decoder.process_raw(buf, False, False) hyp = decoder.hyp() if hyp: with decoder.end_utterance(): # Make sure the hotword is matched detected_phrase = hyp.hypstr if detected_phrase == hotword: break detected_event.set() # Listen asynchronously detected_event.clear() thread = threading.Thread(target=listen, daemon=True) thread.start() yield from asyncio.get_event_loop().run_in_executor( None, detected_event.wait) if not terminated: thread.join() hass.states.async_set(OBJECT_DECODER, STATE_IDLE, state_attrs) # Fire detected event hass.bus.async_fire( EVENT_HOTWORD_DETECTED, { 'name': name # name of the component }) hass.services.async_register(DOMAIN, SERVICE_LISTEN, async_listen) hass.states.async_set(OBJECT_DECODER, STATE_IDLE, state_attrs) # Make sure snowboy terminates property when home assistant stops @asyncio.coroutine def async_terminate(event): nonlocal terminated terminated = True detected_event.set() hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, async_terminate) _LOGGER.info('Started') return True
def test_jsgf(self): ps = Pocketsphinx(lm='deps/pocketsphinx/test/data/turtle.lm.bin', dic='deps/pocketsphinx/test/data/turtle.dic') # Decoding with 'turtle' language model ps.decode() self.assertEqual(ps.hypothesis(), 'go forward ten meters') # Switch to JSGF grammar jsgf = Jsgf('deps/pocketsphinx/test/data/goforward.gram') rule = jsgf.get_rule('goforward.move2') fsg = jsgf.build_fsg(rule, ps.get_logmath(), 7.5) ps.set_fsg('goforward', fsg) ps.set_search('goforward') # Decoding with 'goforward' grammar ps.decode() self.assertEqual(ps.hypothesis(), 'go forward ten meters')
def __init__(self, *args, **kwargs): self.ps = Pocketsphinx() self.ps.decode() super(TestRawDecoder, self).__init__(*args, **kwargs)
def async_setup(hass, config): name = config[DOMAIN].get(CONF_NAME, DEFAULT_NAME) acoustic_model = os.path.expanduser(config[DOMAIN].get( CONF_ACOUSTIC_MODEL, DEFAULT_ACOUSTIC_MODEL)) language_model = os.path.expanduser(config[DOMAIN].get( CONF_LANGUAGE_MODEL, DEFAULT_LANGUAGE_MODEL)) dictionary = os.path.expanduser(config[DOMAIN].get(CONF_DICTIONARY, DEFAULT_DICTIONARY)) audio_device_index = config[DOMAIN].get(CONF_AUDIO_DEVICE, DEFAULT_AUDIO_DEVICE) if (audio_device_index is not None) and (audio_device_index < 0): audio_device_index = None # default device sample_width = 2 # 16-bit channels = 1 # mono sample_rate = config[DOMAIN].get(CONF_SAMPLE_RATE, DEFAULT_SAMPLE_RATE) buffer_size = config[DOMAIN].get(CONF_BUFFER_SIZE, DEFAULT_BUFFER_SIZE) # Set up voice activity detection (VAD) import webrtcvad vad_mode = config[DOMAIN].get(CONF_VAD_MODE, DEFAULT_VAD_MODE) assert 0 <= vad_mode <= 3, 'VAD mode must be in [0-3]' vad = webrtcvad.Vad() vad.set_mode(vad_mode) # agressiveness (0-3) # Controls how phrase is recorded min_sec = config[DOMAIN].get(CONF_MIN_SEC, DEFAULT_MIN_SEC) silence_sec = config[DOMAIN].get(CONF_SILENCE_SEC, DEFAULT_SILENCE_SEC) timeout_sec = config[DOMAIN].get(CONF_TIMEOUT_SEC, DEFAULT_TIMEOUT_SEC) seconds_per_buffer = buffer_size / sample_rate # Create speech-to-text decoder from pocketsphinx import Pocketsphinx, Ad decoder = Pocketsphinx(hmm=acoustic_model, lm=language_model, dic=dictionary) import pyaudio data_format = pyaudio.get_format_from_width(sample_width) # Events for asynchronous recording/decoding recorded_event = threading.Event() decoded_event = threading.Event() decoded_phrase = None terminated = False # ------------------------------------------------------------------------- state_attrs = { 'friendly_name': 'Speech to Text', 'icon': 'mdi:comment-text', 'text': '' } @asyncio.coroutine def async_listen(call): nonlocal decoded_phrase, terminated decoded_phrase = None terminated = False hass.states.async_set(OBJECT_POCKETSPHINX, STATE_LISTENING, state_attrs) # Recording state max_buffers = int(math.ceil(timeout_sec / seconds_per_buffer)) silence_buffers = int(math.ceil(silence_sec / seconds_per_buffer)) min_phrase_buffers = int(math.ceil(min_sec / seconds_per_buffer)) in_phrase = False after_phrase = False finished = False recorded_data = bytearray() # PyAudio callback for each buffer from audio device def stream_callback(buf, frame_count, time_info, status): nonlocal max_buffers, silence_buffers, min_phrase_buffers nonlocal in_phrase, after_phrase nonlocal recorded_data, finished # Check maximum number of seconds to record max_buffers -= 1 if max_buffers <= 0: # Timeout finished = True # Reset in_phrase = False after_phrase = False # Detect speech in buffer is_speech = vad.is_speech(buf, sample_rate) if is_speech and not in_phrase: # Start of phrase in_phrase = True after_phrase = False recorded_data += buf min_phrase_buffers = int( math.ceil(min_sec / seconds_per_buffer)) elif in_phrase and (min_phrase_buffers > 0): # In phrase, before minimum seconds recorded_data += buf min_phrase_buffers -= 1 elif in_phrase and is_speech: # In phrase, after minimum seconds recorded_data += buf elif not is_speech: # Outside of speech if after_phrase and (silence_buffers > 0): # After phrase, before stop recorded_data += buf silence_buffers -= 1 elif after_phrase and (silence_buffers <= 0): # Phrase complete recorded_data += buf finished = True # Reset in_phrase = False after_phrase = False elif in_phrase and (min_phrase_buffers <= 0): # Transition to after phrase after_phrase = True silence_buffers = int( math.ceil(silence_sec / seconds_per_buffer)) if finished: recorded_event.set() return (buf, pyaudio.paContinue) # Open microphone device audio = pyaudio.PyAudio() mic = audio.open(format=data_format, channels=channels, rate=sample_rate, input_device_index=audio_device_index, input=True, stream_callback=stream_callback, frames_per_buffer=buffer_size) loop = asyncio.get_event_loop() # Wait for recorded to complete recorded_event.clear() mic.start_stream() yield from loop.run_in_executor(None, recorded_event.wait) # Stop audio mic.stop_stream() mic.close() audio.terminate() if not terminated: # Fire recorded event hass.bus.async_fire( EVENT_SPEECH_RECORDED, { 'name': name, # name of the component 'size': len(recorded_data) # bytes of recorded audio data }) hass.states.async_set(OBJECT_POCKETSPHINX, STATE_DECODING, state_attrs) def decode(): nonlocal decoded_phrase with decoder.start_utterance(): decoder.process_raw(recorded_data, False, True) # full utterance hyp = decoder.hyp() if hyp: with decoder.end_utterance(): decoded_phrase = hyp.hypstr decoded_event.set() # Decode in separate thread decoded_event.clear() thread = threading.Thread(target=decode, daemon=True) thread.start() yield from loop.run_in_executor(None, decoded_event.wait) if not terminated: thread.join() state_attrs['text'] = decoded_phrase hass.states.async_set(OBJECT_POCKETSPHINX, STATE_IDLE, state_attrs) # Fire decoded event hass.bus.async_fire( EVENT_SPEECH_TO_TEXT, { 'name': name, # name of the component 'text': decoded_phrase }) # ------------------------------------------------------------------------- @asyncio.coroutine def async_decode(call): nonlocal decoded_phrase, terminated decoded_phrase = None terminated = False if ATTR_FILENAME in call.data: # Use WAV file filename = call.data[ATTR_FILENAME] with wave.open(filename, mode='rb') as wav_file: data = wav_file.readframes(wav_file.getnframes()) else: # Use data directly from JSON filename = None data = bytearray(call.data[ATTR_DATA]) hass.states.async_set(OBJECT_POCKETSPHINX, STATE_DECODING, state_attrs) def decode(): nonlocal decoded_phrase, data, filename # Check if WAV is in the correct format. # Convert with sox if not. with io.BytesIO(data) as wav_data: with wave.open(wav_data, mode='rb') as wav_file: rate, width, channels = wav_file.getframerate( ), wav_file.getsampwidth(), wav_file.getnchannels() _LOGGER.debug('rate=%s, width=%s, channels=%s.' % (rate, width, channels)) if (rate != 16000) or (width != 2) or (channels != 1): # Convert to 16-bit 16Khz mono (required by pocketsphinx acoustic models) _LOGGER.debug('Need to convert to 16-bit 16Khz mono.') if shutil.which('sox') is None: _LOGGER.error( "'sox' command not found. Cannot convert WAV file to appropriate format. Expect poor performance." ) else: temp_input_file = None if filename is None: # Need to write original WAV data out to a file for sox temp_input_file = tempfile.NamedTemporaryFile( suffix='.wav', mode='wb+') temp_input_file.write(data) temp_input_file.seek(0) filename = temp_input_file.name # sox <IN> -r 16000 -e signed-integer -b 16 -c 1 <OUT> with tempfile.NamedTemporaryFile( suffix='.wav', mode='wb+') as out_wav_file: subprocess.check_call([ 'sox', filename, '-r', '16000', '-e', 'signed-integer', '-b', '16', '-c', '1', out_wav_file.name ]) out_wav_file.seek(0) # Use converted data with wave.open(out_wav_file, 'rb') as wav_file: data = wav_file.readframes( wav_file.getnframes()) if temp_input_file is not None: # Clean up temporary file del temp_input_file # Process WAV data as a complete utterance (best performance) with decoder.start_utterance(): decoder.process_raw(data, False, True) # full utterance if decoder.hyp(): with decoder.end_utterance(): decoded_phrase = decoder.hyp().hypstr decoded_event.set() loop = asyncio.get_event_loop() # Decode in separate thread decoded_event.clear() thread = threading.Thread(target=decode, daemon=True) thread.start() yield from loop.run_in_executor(None, decoded_event.wait) if not terminated: thread.join() state_attrs['text'] = decoded_phrase hass.states.async_set(OBJECT_POCKETSPHINX, STATE_IDLE, state_attrs) # Fire decoded event hass.bus.async_fire( EVENT_SPEECH_TO_TEXT, { 'name': name, # name of the component 'text': decoded_phrase }) # ------------------------------------------------------------------------- hass.http.register_view(ExternalSpeechView) # Service to record commands hass.services.async_register(DOMAIN, SERVICE_LISTEN, async_listen) # Service to do speech to text hass.services.async_register(DOMAIN, SERVICE_DECODE, async_decode, schema=SCHEMA_SERVICE_DECODE) hass.states.async_set(OBJECT_POCKETSPHINX, STATE_IDLE, state_attrs) # Make sure everything terminates property when home assistant stops @asyncio.coroutine def async_terminate(event): nonlocal terminated terminated = True recorded_event.set() decoded_event.set() hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, async_terminate) _LOGGER.info('Started') return True
from __future__ import print_function import os from pocketsphinx import Pocketsphinx, get_model_path, get_data_path model_path = get_model_path() data_path = get_data_path() config = { 'hmm': os.path.join(model_path, 'en-us'), 'lm': r'C:\Users\BZT\Desktop\speech_segment\5446.lm', 'dict': r'C:\Users\BZT\Desktop\speech_segment\5446.dic' } ps = Pocketsphinx(**config) ps.decode( audio_file= r'C:\Users\BZT\Desktop\speech_segment\speech_segment\Ses01F_impro01_M013.wav', buffer_size=2048, no_search=False, full_utt=False) # print(ps.segments()) # => ['<s>', '<sil>', 'go', 'forward', 'ten', 'meters', '</s>'] # print('Detailed segments:', *ps.segments(detailed=True), sep='\n') for sample in ps.segments(detailed=True): for subsample in sample: print(subsample, end='\t') print() print(ps.hypothesis()) # => go forward ten meters # print(ps.probability()) # => -32079 # print(ps.score()) # => -7066
from pocketsphinx import Pocketsphinx ps = Pocketsphinx(verbose=True) ps.decode() print(ps.hypothesis())
model_path = get_model_path() data_path = get_data_path() config = { 'hmm': os.path.join(model_path, 'en-us'), 'lm': os.path.join(model_path, 'en-us.lm.bin'), 'dict': os.path.join(model_path, 'cmudict-en-us.dict') } #place the .wav fie in the directory of data_path # /home/krishna/anaconda3/lib/python3.7/site-packages/pocketsphinx/data ps = Pocketsphinx(**config) ps.decode( audio_file=os.path.join(data_path, filename_wav_extn), buffer_size=2048, no_search=False, full_utt=False ) #print(ps.segments()) #save the detailed segments of the words, #which will contain details word, probablity, start_time and end_time #print('Detailed segments:', *ps.segments(detailed=True), sep='\n') # with open('output_segments_obama_farewell_speech.txt', 'a') as f: # print(*ps.segments(detailed=True), sep='\n', file=f)
class SpeechToText: ''' Предназначен для распознавания речи с помощью PocketSphinx. 1. mode - может иметь два значения: from_file и from_microphone 1.1. from_file - распознавание речи из .wav файла (частота дискретизации >=16кГц, 16bit, моно) 1.2. from_microphone - распознавание речи с микрофона 2. name_dataset - имя набора данных, на основе которого построена языковая модель: plays_ru, subtitles_ru или conversations_ru ''' def __init__(self, mode='from_microphone', name_dataset='plays_ru'): self.current_dirname = os.path.dirname(os.path.realpath(__file__)) self.work_mode = mode model_path = get_model_path() if not (name_dataset == 'plays_ru' or name_dataset == 'subtitles_ru' or name_dataset == 'conversations_ru'): print( '\n[E] Неверное значение name_dataset. Возможные варианты: plays_ru, subtitles_ru или conversations_ru\n' ) return if self.work_mode == 'from_file': config = { 'hmm': os.path.join(model_path, 'zero_ru.cd_cont_4000'), 'lm': os.path.join(model_path, 'ru_bot_' + name_dataset + '.lm'), 'dict': os.path.join(model_path, 'ru_bot_' + name_dataset + '.dic') } self.speech_from_file = Pocketsphinx(**config) elif self.work_mode == 'from_microphone': self.speech_from_microphone = LiveSpeech( verbose=False, sampling_rate=16000, buffer_size=2048, no_search=False, full_utt=False, hmm=os.path.join(model_path, 'zero_ru.cd_cont_4000'), lm=os.path.join(model_path, 'ru_bot_' + name_dataset + '.lm'), dic=os.path.join(model_path, 'ru_bot_' + name_dataset + '.dic')) else: print( '[E] Неподдерживаемый режим работы, проверьте значение аргумента mode.' ) # Добавить фильтры шума, например с помощью sox def get(self, f_name_audio=None): ''' Распознавание речи с помощью PocketSphinx. Режим задаётся при создании объекта класса (из файла или с микрофона). 1. f_name_audio - имя .wav или .opus файла с речью (для распознавания из файла, частота дискретизации >=16кГц, 16bit, моно) 2. возвращает строку с распознанной речью ''' if self.work_mode == 'from_file': if f_name_audio is None: print( '[E] В режиме from_file необходимо указывать имя .wav или .opus файла.' ) return filename_audio_raw = f_name_audio[:f_name_audio.find('.')] + '.raw' filename_audio_wav = f_name_audio[:f_name_audio.find('.')] + '.wav' audio_format = f_name_audio[f_name_audio.find('.') + 1:] # Конвертирование .opus файла в .wav if audio_format == 'opus': command_line = "yes | ffmpeg -i '" + f_name_audio + "' '" + filename_audio_wav + "'" proc = subprocess.Popen(command_line, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = proc.communicate() if err.decode().find(f_name_audio + ':') != -1: return 'error' # Конвертирование .wav файла в .raw audio_file = AudioSegment.from_wav(self.current_dirname + '/' + filename_audio_wav) audio_file = audio_file.set_frame_rate(16000) audio_file.export(self.current_dirname + '/' + filename_audio_raw, format='raw') # Создание декодера и распознавание self.speech_from_file.decode(audio_file=self.current_dirname + '/' + filename_audio_raw, buffer_size=2048, no_search=False, full_utt=False) return self.speech_from_file.hypothesis() elif self.work_mode == 'from_microphone': for phrase in self.speech_from_microphone: return str(phrase)
class HotwordRecognizer: """热词(唤醒词)识别器,对 |pocketsphinx| 的简单封装,默认的热词是 `'阿Q'` 和 `'R-cute`。 如果要自定义热词,请参考 https://blog.51cto.com/feature09/2300352 .. |pocketsphinx| raw:: html <a href='https://github.com/bambocher/pocketsphinx-python' target='blank'>pocketsphinx</a> .. |config| raw:: html <a href='https://github.com/bambocher/pocketsphinx-python#default-config' target='blank'>pocketsphinx Default config</a> :param hotword: 热词或热词列表,默认为 `['阿Q', 'R-cute']` :type hotword: str / list, optional :param hmm: 参考 |config| :type hmm: str, optional :param lm: 参考 |config| :type lm: str, optional :param dic: 参考 |config| :type dic: str, optional """ def __init__(self, **kwargs): # signal.signal(signal.SIGINT, self.stop) self._no_search = False self._full_utt = False hotword = kwargs.pop('hotword', ['阿Q', 'R-cute']) self._hotwords = hotword if isinstance(hotword, list) else [hotword] model_path = get_model_path() opt = { 'verbose': False, 'hmm': os.path.join(model_path, 'en-us'), 'lm': util.resource('sphinx/rcute.lm'), 'dic': util.resource('sphinx/rcute.dic'), } opt.update(kwargs) self._rec = Pocketsphinx(**opt) def recognize(self, stream, timeout=None): """开始识别 :param source: 声音来源 :param timeout: 超时,即识别的最长时间(秒),默认为 `None` ,表示不设置超时,知道识别到热词才返回 :type timeout: float, optional :return: 识别到的热词模型对应的热词,若超时没识别到热词则返回 `None` :rtype: str """ self._cancel = False if timeout: count = 0.0 in_speech = False with self._rec.start_utterance(): while True: data = stream.raw_read() self._rec.process_raw(data, self._no_search, self._full_utt) if in_speech != self._rec.get_in_speech(): in_speech = not in_speech if not in_speech and self._rec.hyp(): with self._rec.end_utterance(): hyp = self._rec.hypothesis() if hyp in self._hotwords: return hyp if self._cancel: raise RuntimeError( 'Hotword detection cancelled by another thread') elif timeout: count += source.frame_duration #len(data) / 32000 if count > timeout: return def cancel(self): """停止识别""" self._cancel = True
def decode(): nonlocal decoder, decoded_phrase, data, filename # Check if WAV is in the correct format. # Convert with sox if not. with io.BytesIO(data) as wav_data: with wave.open(wav_data, mode='rb') as wav_file: rate, width, channels = wav_file.getframerate(), wav_file.getsampwidth(), wav_file.getnchannels() _LOGGER.debug('rate=%s, width=%s, channels=%s.' % (rate, width, channels)) if (rate != 16000) or (width != 2) or (channels != 1): # Convert to 16-bit 16Khz mono (required by pocketsphinx acoustic models) _LOGGER.debug('Need to convert to 16-bit 16Khz mono.') if shutil.which('sox') is None: _LOGGER.error("'sox' command not found. Cannot convert WAV file to appropriate format. Expect poor performance.") else: temp_input_file = None if filename is None: # Need to write original WAV data out to a file for sox temp_input_file = tempfile.NamedTemporaryFile(suffix='.wav', mode='wb+') temp_input_file.write(data) temp_input_file.seek(0) filename = temp_input_file.name # sox <IN> -r 16000 -e signed-integer -b 16 -c 1 <OUT> with tempfile.NamedTemporaryFile(suffix='.wav', mode='wb+') as out_wav_file: subprocess.check_call(['sox', filename, '-r', '16000', '-e', 'signed-integer', '-b', '16', '-c', '1', out_wav_file.name]) out_wav_file.seek(0) # Use converted data with wave.open(out_wav_file, 'rb') as wav_file: data = wav_file.readframes(wav_file.getnframes()) if temp_input_file is not None: # Clean up temporary file del temp_input_file # Dynamically load decoder if decoder is None: _LOGGER.debug('Loading decoder') hass.states.async_set(OBJECT_POCKETSPHINX, STATE_LOADING, state_attrs) decoder = Pocketsphinx( hmm=acoustic_model, lm=language_model, dic=dictionary) hass.states.async_set(OBJECT_POCKETSPHINX, STATE_DECODING, state_attrs) # Process WAV data as a complete utterance (best performance) with decoder.start_utterance(): decoder.process_raw(data, False, True) # full utterance if decoder.hyp(): with decoder.end_utterance(): decoded_phrase = decoder.hyp().hypstr decoded_event.set()
import time import socket from pocketsphinx import AudioFile, Pocketsphinx, get_model_path from __playwave import playwave sys_model_path = get_model_path() voice_path = os.path.join(os.getcwd(), 'voice') usr_model_path = os.path.join(os.getcwd(), 'model') config = { 'hmm': os.path.join(sys_model_path, 'en-us'), 'lm': os.path.join(usr_model_path, '4767.lm'), 'dict': os.path.join(usr_model_path, '4767.dic') } ps = Pocketsphinx(**config) def is_net_ok(testserver): s = socket.socket() s.settimeout(3) try: status = s.connect_ex(testserver) if status == 0: s.close() return True else: return False except Exception as e: return False
def test_jsgf(self): ps = Pocketsphinx( lm='deps/pocketsphinx/test/data/turtle.lm.bin', dic='deps/pocketsphinx/test/data/turtle.dic' ) # Decoding with 'turtle' language model ps.decode() self.assertEqual(ps.hypothesis(), 'go forward ten meters') # Switch to JSGF grammar jsgf = Jsgf('deps/pocketsphinx/test/data/goforward.gram') rule = jsgf.get_rule('goforward.move2') fsg = jsgf.build_fsg(rule, ps.get_logmath(), 7.5) ps.set_fsg('goforward', fsg) ps.set_search('goforward') # Decoding with 'goforward' grammar ps.decode() self.assertEqual(ps.hypothesis(), 'go forward ten meters')
def loop(self): # loop while not rospy.core.is_shutdown(): # if recording finished if not self.outbuf is None: # write output file print("writing output file") outfilename = '/tmp/input.wav' file = wave.open(outfilename, 'wb') file.setparams((1, 4, 20000, 0, 'NONE', 'not compressed')) print("Starting Reshape") x = np.reshape(self.outbuf[:, [0, 0]], (-1)) print("writing frames") print(len(x)) values = [] for s in x: packed_value = struct.pack('<h', s) values.append(packed_value) #file.writeframes(struct.pack('<h', s)) #close file value_str = b''.join(values) file.writeframes(value_str) print("Closing file") file.close() model_path = get_model_path() data_path = get_data_path() config = { 'hmm': os.path.join( model_path, 'en-us' ), # Hidden Markov Model, Speech Recongnition model - trained probability scoring system 'lm': os.path.join(model_path, 'en-us.lm.bin'), #language model 'dict': os.path.join( model_path, 'cmudict-en-us.dict') #, # language dictionary #'samprate' : 16000 } #cmd= "ffmpeg -y -i /tmp/output.wav -ar 8000 -af asetrate=16000*" + pitch + ",aresample=16000,atempo=" + tempo + " -ac 1 /tmp/outputConv.wav" #cmd = "ffmpeg -y -i /tmp/input.wav -f s32le -acodec pcm_s32le -ar 16000 -ac 1 /tmp/inputConv.wav" #cmd = "sox /tmp/input.wav -r 16000 inputConv.wav" #cmd = "ffmpeg -i /tmp/input.wav -ar 16000 /tmp/inputConv.wav" print("Converting via FFMPEG") cmd = "ffmpeg -y -i /tmp/input.wav -f s16le -acodec pcm_s16le -ar 16000 -af 'aresample=20000' -ac 1 /tmp/inputConv.wav -loglevel quiet" os.system(cmd) print("Decoding Via Pocketsphinx") ps = Pocketsphinx(**config) ps.decode( audio_file=( "/tmp/inputConv.wav"), #add temp input.wav file buffer_size=8192, no_search=False, full_utt=False) print("Recognized: ") print(ps.hypothesis()) ## output ## Speech Analysis, (what to start?) if ps.hypothesis() == "hello": mml.say("Hello there human") # Change this to whatever elif ps.hypothesis().find("how are you") >= 0: mml.say("I'm always good") print("END") self.micbuf = np.zeros((0, 4), 'uint16') self.outbuf = None self.buffer_stuff = 0 self.playchan = 0 self.playsamp = 0 # state time.sleep(0.02)
from pocketsphinx import Pocketsphinx, get_model_path, get_data_path # This file will take in a raw formatted audio # Using pocket Sphinx we are able to get the probably of probable words # Then it will display all of the details about the raw -> audio model_path = get_model_path() data_path = get_data_path() config = { 'hmm': os.path.join(model_path, 'en-us'), 'lm': os.path.join(model_path, 'en-us.lm.bin'), 'dict': os.path.join(model_path, 'cmudict-en-us.dict') } ps = Pocketsphinx(**config) ps.decode(audio_file=os.path.join(data_path, 'output.raw'), buffer_size=2048, no_search=False, full_utt=False) # => ['<s>', '<sil>', 'go', 'forward', 'ten', 'meters', '</s>'] print(ps.segments()) print('Detailed segments:', *ps.segments(detailed=True), sep='\n') # => [ # word, prob, start_frame, end_frame # ('<s>', 0, 0, 24) # ('<sil>', -3778, 25, 45) # ('go', -27, 46, 63) # ('forward', -38, 64, 116) # ('ten', -14105, 117, 152) # ('meters', -2152, 153, 211)
# Code retested by KhalsaLabs # You can use your own audio file in code # Raw or wav files would work perfectly # For mp3 files, you need to modify code (add codex) from __future__ import print_function import os from pocketsphinx import Pocketsphinx, get_model_path, get_data_path model_path = get_model_path() data_path = get_data_path() config = { 'hmm': os.path.join(model_path, 'en-us'), 'lm': os.path.join(model_path, 'en-us.lm.bin'), 'dict': os.path.join(model_path, 'cmudict-en-us.dict') } ps = Pocketsphinx(**config) ps.decode( audio_file=os.path.join(data_path, 'test1.wav'), # add your audio file here buffer_size=2048, no_search=False, full_utt=False) print(ps.hypothesis())
client.simCharSetFacePresets(presets) # predict phoneme from audio model_path = get_model_path() data_path = get_data_path() AUDIO=os.path.join(data_path, opt.audio) config = { 'samprate' : 16000, 'allphone' : os.path.join(model_path, 'en-us-phone.lm.bin'), 'remove_silence':False } ps = Pocketsphinx(**config) ps.decode( audio_file=AUDIO, buffer_size=1024, no_search=False, full_utt=False ) #define stream chunk chunk = 1024 #open the audio files (bytes) f = open(opt.audio,"rb") #instantiate PyAudio