A story that struggled to handle the Python package of PocketSphinx

What is Pocket Sphinx?

Pocket Sphinx is a speech recognition engine that performs continuous speech recognition. It only supports English, so it doesn't seem to be suitable for Japanese. (** There seems to be a way to forcefully respond, but I will omit it because the story derails ... **)

Execution environment

This time, we checked the operation in the following environment.

Package installation

It doesn't end with pip install pocketsphinx. .. .. You will need swig and Build Tool (C ++) to install PocketSphinx.

I was able to install swig by referring to here. You can download the Build Tool from here.

When you're ready, use pip to install PocketSphinx.

pip install pocketsphinx

Sample code execution

The sample code of PocketSphinx is as follows. If you execute this way, English words will be detected.

sample.py


import os
from pocketsphinx import LiveSpeech, get_model_path

model_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')
)

for phrase in speech:
    print(phrase)

It seems that the default value of LiveSpeech is as follows.

verbose = False
logfn = /dev/null or nul
audio_file = site-packages/pocketsphinx/data/goforward.raw
audio_device = None
sampling_rate = 16000
buffer_size = 2048
no_search = False
full_utt = False
hmm = site-packages/pocketsphinx/model/en-us
lm = site-packages/pocketsphinx/model/en-us.lm.bin
dict = site-packages/pocketsphinx/model/cmudict-en-us.dict

Understanding the referenced directory

In PocketSphinx, an event like this occurred. We will sort out the causes of this event.

RuntimeError: new_Decoder returned -1

The model_path used in hmm / lm / dict is set as follows.

model_path = get_model_path()

When I print this path, it looks like this.

C:\Users\<UserName>\.conda\envs\pocketsphinx\lib\site-packages\pocketsphinx\model
(pocketsphinx) C:\Users\<UserName>\.conda\envs\pocketsphinx\Lib\site-packages\pocketsphinx\model>tree /f
C:.
│  cmudict-en-us.dict
│  en-us.lm.bin
│
└─en-us
        feat.params
        mdef
        means
        noisedict
        README
        sendump
        transition_matrices
        variances

Apparently, the default dictionary data is included in the package installed by pip. It seems that RuntimeError will occur if this path is incorrect.

Digression The story of this error has information when I search, but it is written to download en-us, In the first place, there was some code that the error could not be avoided. (In the end, as Python in the first place, some code failed to replace the path ...) At first, I also neglected to confirm the pass, so it was an event that became more and more mysterious.

Application of self-made dictionary data

This time, the directory structure is like this. model \ sample.dict is the self-made dictionary data.

(pocketsphinx) C:\Users\<UserName>\Documents\pocketsphinx_sample>tree /f
C:.
│  exmaple.py
└─model
       sample.dict

Next, looking at the arguments of LiveSpeech, it seems that you should set the following arguments. For lm, the document also describes how to set False, but for some reason it causes a RuntimeError, so I am targeting it.

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')

exmaple.py


import os
from pocketsphinx import LiveSpeech, get_model_path

model_path = get_model_path()
my_model_path = 'C:\\Users\\<UserName>\\Documents\\pocketsphinx_sample\\model'

speech = LiveSpeech(
    hmm=os.path.join(model_path, 'en-us'),
    lm=os.path.join(model_path, 'en-us.lm.bin'),
    dic=os.path.join(my_model_path, 'sample.dict'),
)

for phrase in speech:
    print(phrase)

Remaining questions

All I had to do was read the dictionary data, so I was able to achieve my goal. However, the cause of the error with lm = False remains unknown. I also saw some sample code that wrote an argument called jsgf to read a gram (grammar file). Looking at the code on PocketSphinx's Github, I didn't see any such arguments. If you need to read the grammar file in the future, you need to confirm it.

reference

http://rinatz.github.io/swigdoc/install.html https://pypi.org/project/pocketsphinx/ https://visualstudio.microsoft.com/ja/thank-you-downloading-visual-studio/?sku=BuildTools https://stackoverflow.com/questions/44339312/new-decoder-returned-1-when-trying-to-run-pocketsphinx-on-raspberry-pi/51346264#51346264

Recommended Posts

A story that struggled to handle the Python package of PocketSphinx
The story of introducing jedi (python auto-completion package) to emacs
The story of blackjack A processing (python)
The story that the version of python 3.7.7 was not adapted to Heroku
[python] A note that started to understand the behavior of matplotlib.pyplot
The story of making a module that skips mail with python
[Python] A program that rotates the contents of the list to the left
[Python] A program that calculates the number of socks to be paired
The story of making a package that speeds up the operation of Juman (Juman ++) & KNP
A story that reduces the effort of operation / maintenance
[Python] A program that counts the number of valleys
The story of IPv6 address that I want to keep at a minimum
Around the authentication of PyDrive2, a package that operates Google Drive with Python
A story that struggled with the common set HTTP_PROXY = ~
A story that analyzed the delivery of Nico Nama.
[Python] A program that compares the positions of kangaroos.
Python Note: The mystery of assigning a variable to a variable
A story that got stuck when trying to upgrade the Python version on GCE
The story of Linux that I want to teach myself half a year ago
A story about trying to introduce Linter in the middle of a Python (Flask) project
Try to get the function list of Python> os package
A Python script that compares the contents of two directories
[Python] A story that seemed to fall into a rounding trap
A record of patching a python package
The story of writing a program
A story that makes it easy to estimate the living area using Elasticsearch and Python
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
Python> __init__.py> Required to handle the specified directory as a package (empty file is acceptable)
A Python script that allows you to check the status of the server from your browser
[Python] A program to find the number of apples and oranges that can be harvested
How to determine the existence of a selenium element in Python
How to check the memory size of a variable in Python
The story of making a standard driver for db with python.
How to check the memory size of a dictionary in Python
A function that measures the processing time of a method in python
The story of creating a site that lists the release dates of books
[Python3] Define a decorator to measure the execution time of a function
A script that returns 0, 1 attached to the first Python prime number
[Python] A simple function to find the center coordinates of a circle
The story of trying to reconnect the client
The story of adding MeCab to ubuntu 16.04
The story of making Python an exe
The story of manipulating python global variables
[python] [meta] Is the type of python a type?
A story that is a little addicted to the authority of the directory specified by expdp (for beginners)
A story that didn't work when I tried to log in with the Python requests module
The story of pep8 changing to pycodestyle
A story about a person who uses Python addicted to the judgment of an empty JavaScript dictionary
[Python] A program that rounds the score
The story of creating a bot that displays active members in a specific channel of slack with python
[Python] A program that calculates the number of chocolate segments that meet the conditions
The story of making a university 100 yen breakfast LINE bot with Python
Have Alexa run Python to give you a sense of the future
[Introduction to Python] How to sort the contents of a list efficiently with list sort
Semi-automatically generate a description of the package to be registered on PyPI
The story of developing a web application that automatically generates catchphrases [MeCab]
How to create a wrapper that preserves the signature of the function to wrap
Python code to determine the monthly signal of a relative strength investment
I made a program to check the size of a file in Python
From a book that makes the programmer's way of thinking interesting (Python)
Python: I want to measure the processing time of a function neatly