Introduction:
There are multiple ways to do keyword Recognition in Labview. Earlier I wanted to implement the MFCC algorithm in labview but after searching a bit online, I realized that it will become a complete project on its own and looking at the time constraint, I started looking at other alternatives. I found that we can use Microsoft Speech API or integrate some python code in the VI for Speech Recognition.
In this blog, I will try to implement keyword recognition in labview by integrating python code and using the pocketsphinx python library.
Requirements:
Python & Labview:
First of all we need the python and labview installed on our system. While trying to integrate python code in labview I was facing some error saying "The specified version of python cannot be loaded".
After doing some research I found that this error was due to difference in bitness of the two softwares. The Labview community version is available only in 32 bit version. But I had the 64 bit version of python 3.10 installed on my PC.
After installing the 32 bit version of python 3.10 and selecting it in my VI (discussed later in the blog), the issue got resolved.
Pocketsphinx Python library:
The pocketsphinx python library is an open source continuous speech recognition engine. I will be using this library in our project.
pip install pocketsphinx
Running speech recognition in Python:
Now we will try to create a simple single word speech recognition program and run it. It should be able to identify the keywords: 'fast' & 'slow'.
The python code is given below:
def kw_recog():
#Import LiveSpeech module
from pocketsphinx import LiveSpeech
#Providing the keyword list with sensitivity
speech = LiveSpeech(kws='E:/Python/spch_recog_pocketsphinx/key.list')
print("Start Speaking")
for phrase in speech:
print(phrase.segments(detailed=False))
kw = phrase.segments(detailed=False)
#Break out of the loop if it detects fast or slow
if ((kw[0] == 'fast ') or (kw[0] == 'slow ')):
break
kw_recog()
We are providing the keyword list and sensitivity in the key.list file. The program will only look for words mentioned in the keyword list. The lesser the sensitivity, the more accurate will be the results but we will have to shout louder. The keyword list should be in the below format:
The result is as follows:
Integrating Simple Python Code in Labview:
Now, we will try to integrate a simple python code in labview. I have created a simple add_num function in python to add two numbers in add_number.py file. The code is below:
def add_num(a, b):
c = a + b
return c
The python palette is available under Functions->Connectivity->Python.
Open Python Session - To open a python session. Python version needs to be provided.
Python Node - Runs a python function. Need to give the path of the python file where function is defined, function name, input parameters and return type (numeric constant in this case).
Close Python Session - To close the python session.
Implementing Keyword Recognition in Labview:
The code for kw_recog() function is given below:
def kw_recog():
#Import LiveSpeech module
from pocketsphinx import LiveSpeech
#Providing the keyword list with sensitivity
speech = LiveSpeech(kws='E:/Python/spch_recog_pocketsphinx/key.list')
print("Start Speaking")
for phrase in speech:
print(phrase.segments(detailed=False))
kw = phrase.segments(detailed=False)
#Break out of the loop if it detects fast or slow
if ((kw[0] == 'fast ') or (kw[0] == 'slow ')):
return kw[0][0:-1]
The VI:
Conclusion:
Labview Python palette provides an easy way to integrate and run python codes in labview. The program is not 100% accurate. Sometimes it identifies 'fast' as 'slow' & vice versa. Sometimes, I had to speak multiple times and louder for it to be able to recognize the keyword. The inaccuracy might be due to pronunciation & external noise. I think if it is tuned a little bit, it will work better.
Another alternative is to implement my own keyword recognition algorithm in labview which will be tuned to my pronunciation. I will try this in some other project.