Example #1
0
def transform_audio_to_text(filename):

    user = expanduser("~")
    path = user + "/DTAI_Internship/src/speech_recognizer_node/data/"

    lm_file = path + "generated_language_model.lm"
    dict_file = path + "generated_dictionary.dic"

    hmm_file = user + "/.local/lib/python2.7/site-packages/pocketsphinx/model/en-us"

    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, lm_file),
        'dict': os.path.join(model_path, dict_file)
    }

    ps = Pocketsphinx(**config)
    ps.decode(audio_file=os.path.join(data_path, filename),
              buffer_size=2048,
              no_search=False,
              full_utt=False)

    text = ps.hypothesis()

    print(text)

    return text
Example #2
0
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)])
Example #3
0
def pocket():

	ps = Pocketsphinx()


	language_directory = os.path.dirname(os.path.realpath(__file__))
	
	print language_directory

	acoustic_parameters_directory = os.path.join(language_directory, "acoustic-model")
	language_model_file = os.path.join(language_directory, "language-model.lm.bin")
	phoneme_dictionary_file = os.path.join(language_directory, "pronounciation-dictionary.dict")
    
	config = Decoder.default_config()
	config.set_string("-hmm", acoustic_parameters_directory)  # set the path of the hidden Markov model (HMM) parameter files
	config.set_string("-lm", language_model_file)
	config.set_string("-dict", phoneme_dictionary_file)

	decoder = Decoder(config)

	with sr.AudioFile(s_dir + "/a bad situation could become dramatically worse. /a bad situation could become dramatically worse. .wav") as source:
		audio_data = r.record(source)
	decoder.start_utt()
	decoder.process_raw(audio_data, False, True)
	decoder.end_utt()

	print decoder.hyp()

	ps.decode(
	    audio_file=os.path.join(s_dir, 'a bad situation could become dramatically worse. /a bad situation could become dramatically worse. .wav'),
	    buffer_size=2048,
	    no_search=False,
	    full_utt=False)
	print(ps.hypothesis()) # => ['<s>', '<sil>', 'go', 'forward', 'ten', 'meters', '</s>']
#pocket()
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)
        ])
Example #5
0
	def __init__(self, mode):

		# state
		self.micbuf = np.zeros((0, 4), 'uint16')
		self.outbuf = None
		self.buffer_stuff = 0
		self.mode = mode
		self.playchan = 0
		self.playsamp = 0
		
		# check mode
		if not (mode == "echo" or mode == "record" or mode == "record4"):
			error("argument not recognised")

		# robot name
		topic_base_name = "/" + os.getenv("MIRO_ROBOT_NAME")

		# publish
		topic = topic_base_name + "/control/stream"
		print ("publish", topic)
		self.pub_stream = rospy.Publisher(topic, Int16MultiArray, queue_size=0)

		# subscribe
		topic = topic_base_name + "/sensors/stream"
		print ("subscribe", topic)
		self.sub_stream = rospy.Subscriber(topic, UInt16MultiArray, self.callback_stream, queue_size=1, tcp_nodelay=True)

		# subscribe
		topic = topic_base_name + "/sensors/mics"
		print ("subscribe", topic)
		self.sub_mics = rospy.Subscriber(topic, Int16MultiArray, self.callback_mics, queue_size=5, tcp_nodelay=True)
		
		# report
		print "recording from 4 microphones for", RECORD_TIME, "seconds..."


		####### Speech Recongnition using Pocket-Sphinx #########  
		

		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=("/tmp/input.wav"), #add temp input.wav file
		buffer_size=2048,
		no_search= False,
		full_utt=False)

		print("Recognized: ")
		print((ps.hypothesis())) ## output
		print("END")
Example #6
0
    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)
Example #7
0
class SpeechProcessor:
    def __init__(self, hmm='data/spanish/CIEMPIESS_Spanish_Models_581h/Models/modelo',
                       lm='data/spanish/CIEMPIESS_Spanish_Models_581h/Models/leng.lm.bin',
                       dict='data/spanish/CIEMPIESS_Spanish_Models_581h/Models/dicc.dic',
                       grammar='data/gramatica-tp2.gram', dataPath='tmp/'):
        self.data_path = dataPath
        config = {
            'hmm': hmm,
            'lm': lm,
            'dict': dict
        }
        #model_path = get_model_path()

        self.ps = Pocketsphinx(**config)
        
        # Switch to JSGF grammar
        jsgf = Jsgf(grammar)
        rule = jsgf.get_rule('tp2.grammar')
        fsg = jsgf.build_fsg(rule, self.ps.get_logmath(), 7.5)
        self.ps.set_fsg('tp2', fsg)
        self.ps.set_search('tp2')

        # Síntesis
        self.tts_authenticator = IAMAuthenticator('cq9_4YcCXxClw2AfgUhbokFktZ-xSRT4kcHS2akcZ05J')
        self.tts = TextToSpeechV1(authenticator=self.tts_authenticator)
        self.tts.set_service_url('https://stream.watsonplatform.net/text-to-speech/api')


    def sintetizar(self, outFileName, msg):
        if len(msg) > 0:
            with open(outFileName, 'wb') as audio_file:
                audio_file.write(
                    self.tts.synthesize(
                        msg,
                        voice='es-LA_SofiaV3Voice',
                        accept='audio/wav'
                    ).get_result().content)

    def reconocer(self, inFileName='audio.wav'):
        # Reconocimiento
        print(self.data_path)
        self.ps.decode(
            audio_file=os.path.join(self.data_path,inFileName),
            buffer_size=2048,
            no_search=False,
            full_utt=False
        )
        return self.ps.segments(), self.ps.best(count=3)
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_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 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')
Example #11
0
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 D T AE N NG IY ZH ER S SIL'
        )

    def test_phoneme_best_phonemes(self):
        self.assertEqual(self.ps.segments(), [
            'SIL',
            'G',
            'OW',
            'F',
            'AO',
            'R',
            'D',
            'T',
            'AE',
            'N',
            'NG',
            'IY',
            'ZH',
            'ER',
            'S',
            'SIL'
        ])
Example #12
0
    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')
Example #13
0
    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')
Example #14
0
    
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)
    
with open(filename_output_segments, 'a') as f:
    print(*ps.segments(detailed=True), sep='\n', file=f)
    
Example #15
0
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)
Example #16
0
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
# print(ps.confidence())  # => 0.04042641466841839
Example #17
0
    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()
Example #18
0
import os
from pocketsphinx import AudioFile, Pocketsphinx, get_model_path, get_data_path

model_path = get_model_path()
data_path = get_data_path()

config = {
    'samprate': 8000.0,
    'hmm': os.path.join(model_path, 'zero_ru.cd_cont_4000'),
    'lm': os.path.join(model_path, 'ru.lm.bin'),
    'dict': os.path.join(model_path, 'ru.dic')
}

ps = Pocketsphinx(**config)
ps.decode(audio_file=os.path.join(data_path, 'decoder-test.wav'),
          buffer_size=2048,
          no_search=False,
          full_utt=False)

print(ps.hypothesis())
Example #19
0
	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
Example #20
0
    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)
    if ("TURN ON THE LIGHT" in speech):
        playwave(os.path.join(voice_path, 'beep_lo.wav'))
        print("turn on the light")
        return "light on"
    elif ("TURN OFF THE LIGHT" in speech):
        playwave(os.path.join(voice_path, 'beep_lo.wav'))
        print("turn off the light")
        return "light off"
    else:
        return "null"


if __name__ == "__main__":
    ps.decode(audio_file=os.path.join(voice_path, "baby.wav"),
              buffer_size=2048,
              no_search=False,
              full_utt=False)

    best_result = ps.best(count=10)
    speech = []

    for phrase in best_result:
        speech.append(phrase[0])

    time.sleep(3)

    while True:
        if "HI BABY" in speech:
            print("recognise right")

        playwave(os.path.join(voice_path, 'beep_hi.wav'))
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)
#     ('</s>', 0, 212, 260)
# ]
Example #23
0
from pocketsphinx import Pocketsphinx

ps = Pocketsphinx(verbose=True)
ps.decode()

print(ps.hypothesis())
# 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)
#     ('</s>', 0, 212, 260)
# ]
Example #25
0
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
Example #26
0
# 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())
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  
p = pyaudio.PyAudio()  

#open stream  
stream = p.open(format = 8,