Package: pocketsphinx2
Pocketsphinx PythonPocketsphinx is a part of theCMU SphinxOpen Source Toolkit For Speech Recognition.This package provides a python interface to CMUSphinxbaseandPocketsphinxlibraries created withSWIGandSetuptools.Supported platformsWindowsLinuxMac OS XInstallation# Make sure we have up-to-date versions of pip, setuptools and wheelpython-mpipinstall--upgradepipsetuptoolswheel
pipinstall--upgradepocketsphinxMore binary distributions for manual installation are availablehere.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)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()])Install development versionInstall requirementsWindows requirements:PythonGitSwigVisual Studio CommunityUbuntu requirements:sudoapt-getinstall-qqpythonpython-devpython-pipbuild-essentialswiggitlibpulse-devlibasound2-devMac OS X requirements:brewreinstallswigpythonInstall with pippipinstallhttps://github.com/bambocher/pocketsphinx-python/archive/master.zipInstall with distutilsgitclone--recursivehttps://github.com/bambocher/pocketsphinx-pythoncdpocketsphinx-python
pythonsetup.pyinstallProjects using pocketsphinx-pythonSpeechRecognition- Library for performing speech recognition, with support for several engines and APIs, online and offline.LicenseThe BSD License