Package: pocketsphinx-fork
Pocketsphinx PythonPocketsphinx is a part of theCMU SphinxOpen Source Toolkit For Speech Recognition.This package provides a python interface to CMUSphinxbaseandPocketsphinxlibraries created withSWIGandSetuptools.Supported platformsWindows (untested)LinuxMac OS X (untested)Install requirementsWindows requirements:PythonGitSwigVisual Studio CommunityUbuntu requirements:sudoapt-getinstall-qqpythonpython-devpython-pipbuild-essentialswiggitlibpulse-devlibasound2-devMac OS X requirements:brewreinstallswigpythonInstallation# Make sure we have up-to-date versions of pip, setuptools and wheelpython-mpipinstall--upgradepipsetuptoolswheel
pipinstall--upgradepocketsphinxMore binary distributions for manual installation are availablehere.Installing ModelsPocketsphinx models in.tar.gzformat can be installed using this packages as well.frompocketsphinximportPocketsphinxModel,AudioFilemodels=PocketsphinxModel(model_path='/some/installation/path')# this will install the model from the give url under name 'de'models.install_model('https://sourceforge.net/projects/cmusphinx/files/Acoustic%20and%20Language%20Models/German/cmusphinx-de-voxforge-5.2.tar.gz','de')de=models.get_model('de')# this returns a dictionary with the locations of hmm, lm and dict of the model# we can now use the 'de' model directly with any pocketsphinx objectforphraseinLiveSpeech(model=de):print(phrase)The defaultmodel_pathis'~/pocketsphinx_models'.UsageLiveSpeechIt's an iterator class for continuous recognition or keyword search from a microphone.frompocketsphinximportLiveSpeechforphraseinLiveSpeech():print(phrase)An example of a keyword search:frompocketsphinximportLiveSpeechspeech=LiveSpeech(lm=False,keyphrase='forward',kws_threshold=1e-20)forphraseinspeech:print(phrase.segments(detailed=True))With your model and dictionary:importosfrompocketsphinximportLiveSpeech,get_model_pathmodel_path=get_model_path()speech=LiveSpeech(verbose=False,sampling_rate=16000,buffer_size=2048,no_search=False,full_utt=False,hmm=os.path.join(model_path,'en-us'),lm=os.path.join(model_path,'en-us.lm.bin'),dic=os.path.join(model_path,'cmudict-en-us.dict'))forphraseinspeech:print(phrase)StreamSpeechThis can be used to send chunks of raw bytes to the iterator, usually when transferring audio over a socket or similar.frompocketsphinximportStreamSpeechf=open('somefile.wav','rb')defcallback():returnf.read(2048)forphraseinStreamSpeech(callback=callback):print(phrase)For an example of keyword search and custom models, seeLiveSpeech.AudioFileIt's an iterator class for continuous recognition or keyword search from a file.frompocketsphinximportAudioFileforphraseinAudioFile():print(phrase)# => "go forward ten meters"An example of a keyword search:frompocketsphinximportAudioFileaudio=AudioFile(lm=False,keyphrase='forward',kws_threshold=1e-20)forphraseinaudio:print(phrase.segments(detailed=True))# => "[('forward', -617, 63, 121)]"With your model and dictionary:importosfrompocketsphinximportAudioFile,get_model_path,get_data_pathmodel_path=get_model_path()data_path=get_data_path()config={'verbose':False,'audio_file':os.path.join(data_path,'goforward.raw'),'buffer_size':2048,'no_search':False,'full_utt':False,'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')}audio=AudioFile(**config)forphraseinaudio:print(phrase)Convert frame into time coordinates:frompocketsphinximportAudioFile# Frames per Secondfps=100forphraseinAudioFile(frate=fps):# frate (default=100)print('-'*28)print('|%5s|%3s|%4s|'%('start','end','word'))print('-'*28)forsinphrase.seg():print('|%4ss |%4ss |%8s|'%(s.start_frame/fps,s.end_frame/fps,s.word))print('-'*28)# ----------------------------# | start |  end  |   word   |# ----------------------------# |  0.0s | 0.24s | 
      |# | 0.25s | 0.45s | <sil>    |# | 0.46s | 0.63s | go       |# | 0.64s | 1.16s | forward  |# | 1.17s | 1.52s | ten      |# | 1.53s | 2.11s | meters   |# | 2.12s |  2.6s | 
     |# ----------------------------PocketsphinxIt's a simple and flexible proxy class topocketsphinx.Decode.frompocketsphinximportPocketsphinxprint(Pocketsphinx().decode())# => "go forward ten meters"A more comprehensive example:from__future__importprint_functionimportosfrompocketsphinximportPocketsphinx,get_model_path,get_data_pathmodel_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,'goforward.raw'),buffer_size=2048,no_search=False,full_utt=False)print(ps.segments())# => ['
', '<sil>', 'go', 'forward', 'ten', 'meters', '
']print('Detailed segments:',*ps.segments(detailed=True),sep='\n')# => [#     word, prob, start_frame, end_frame#     ('
', 0, 0, 24)#     ('<sil>', -3778, 25, 45)#     ('go', -27, 46, 63)#     ('forward', -38, 64, 116)#     ('ten', -14105, 117, 152)#     ('meters', -2152, 153, 211)#     ('
', 0, 212, 260)# ]print(ps.hypothesis())# => go forward ten metersprint(ps.probability())# => -32079print(ps.score())# => -7066print(ps.confidence())# => 0.04042641466841839print(*ps.best(count=10),sep='\n')# => [#     ('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)# ]Default configIf you don't pass any argument while creating an instance of the Pocketsphinx, AudioFile or LiveSpeech class, it will use next default values:verbose=Falselogfn=/dev/nullornulaudio_file=site-packages/pocketsphinx/data/goforward.rawaudio_device=Nonesampling_rate=16000buffer_size=2048no_search=Falsefull_utt=Falsehmm=site-packages/pocketsphinx/model/en-uslm=site-packages/pocketsphinx/model/en-us.lm.bindict=site-packages/pocketsphinx/model/cmudict-en-us.dictAny other option must be passed into the config as is, without using symbol-.If you want to disable default language model or dictionary, you can change the value of the corresponding options to False:lm=Falsedict=FalseVerboseSend output to stdout:frompocketsphinximportPocketsphinxps=Pocketsphinx(verbose=True)ps.decode()print(ps.hypothesis())Send output to file:frompocketsphinximportPocketsphinxps=Pocketsphinx(verbose=True,logfn='pocketsphinx.log')ps.decode()print(ps.hypothesis())CompatibilityParent classes are still available:importosfrompocketsphinximportDefaultConfig,Decoder,get_model_path,get_data_pathmodel_path=get_model_path()data_path=get_data_path()# Create a decoder with a certain modelconfig=DefaultConfig()config.set_string('-hmm',os.path.join(model_path,'en-us'))config.set_string('-lm',os.path.join(model_path,'en-us.lm.bin'))config.set_string('-dict',os.path.join(model_path,'cmudict-en-us.dict'))decoder=Decoder(config)# Decode streaming databuf=bytearray(1024)withopen(os.path.join(data_path,'goforward.raw'),'rb')asf:decoder.start_utt()whilef.readinto(buf):decoder.process_raw(buf,False,False)decoder.end_utt()print('Best hypothesis segments:',[seg.wordforsegindecoder.seg()])Projects using pocketsphinx-pythonSpeechRecognition- Library for performing speech recognition, with support for several engines and APIs, online and offline.LicenseThe BSD License