[PYTHON] Use raspberryPi and Julius (speech recognition). ④ L Chika

Related article

Use raspberryPi and julus (speech recognition). ① Microphone Use julius (speech recognition) on raspberry Pi. ② Installation Use raspberry Pi and Julius (speech recognition). ③ Dictionary creation Use raspberry Pi and Julius (speech recognition). ④ L Chika Use raspberry Pi and Julius (speech recognition). ⑤ i2c character display

What to use

raspberryPi 3B+ USB microphone LED Resistance 220Ω Two jumper wires (male-female)

Try running Julius in module mode

Go to the directory where you want to save it and prepare the code for socket communication with python.

$ sudo vim testnet001.py

testnet001.py



import socket
import time

HOST = '127.0.0.1'   #IP address of julius server
PORT = 10500         #julius server listening port
DATESIZE = 1024     #Number of received data bytes

class Julius:

    def __init__(self):

        self.sock = None

    def run(self):

        #Connect to julius server with socket communication
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as self.sock:
            self.sock.connect((HOST, PORT))

            strTemp = "" #Variable to store spoken words
            fin_flag = False #End of story flag

            while True:

                #Receive data from julius server
                data = self.sock.recv(DATESIZE).decode('utf-8')

                for line in data.split('\n'):
                    #From the received data<WORD>Extract the words written after and store them in a variable.
                    # <WORD>After, the spoken words are listed.
                    index = line.find('WORD="')
                    if index != -1:
                        #Store the words spoken in strTemp
                        strTemp = strTemp + line[index+6:line.find('"',index+6)]
                        
                    #For received data</RECOGOUT>'If there is, the story ends ⇒ Set the flag to True
                    if '</RECOGOUT>' in line:
                        fin_flag = True

                #Execute a print statement for each word spoken
                if fin_flag == True:
                    if 'Thank you' in strTemp:
                        print("You are welcome")
                    elif 'Hello' in strTemp:
                        print("Good evening")
                    else:
                        print("Spoken words:" + strTemp)
                    
                    fin_flag = False
                    strTemp = ""

if __name__ == "__main__":

    julius = Julius()
    julius.run()

Exit the editor after creating it. Call your own dictionary in module mode.

$ cd
$ julius -C ~/julius/julius-4.6/julius-kit/dictation-kit-4.5/am-gmm.jconf -nostrip -gram ~/julius/dict/test -input mic -module

image.png Julius is waiting in module mode, so leave it as it is and launch another terminal.

$cd Python code destination directory
$ sudo python3 testnet001.py

image.png

If you talk and respond, socket communication is complete.

Make the LED stuck in pin 18 shine ### Pin layout of Raspberry Pi (I borrowed the image that is easy to understand by google) ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/504329/ad493a34-ceea-203e-0565-72bcf136d1a4.png)

If you check the LED wiring, someone's article will come out, so omit it. The pin uses GPIO18.

testnet002.py


import RPi.GPIO as GPIO
import time
import socket
import string


def main():
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(18, GPIO.OUT)
    for i in range(5):
        GPIO.output(18, True)
        time.sleep(0.1)
        GPIO.output(18, False)
        time.sleep(0.1)
        GPIO.output(18, GPIO.LOW)

HOST = '127.0.0.1'   #IP address of julius server
PORT = 10500         #julius server listening port
DATESIZE = 1024     #Number of received data bytes

class Julius:

    def __init__(self):

        self.sock = None

    def run(self):

        #Connect to julius server with socket communication
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as self.sock:
            self.sock.connect((HOST, PORT))

            strTemp = "" #Variable to store spoken words
            fin_flag = False #End of story flag

            while True:

                #Receive data from julius server
                data = self.sock.recv(DATESIZE).decode('utf-8')

                for line in data.split('\n'):
                    #From the received data<WORD>Extract the words written after and store them in a variable.
                    # <WORD>After, the spoken words are listed.
                    index = line.find('WORD="')
                    if index != -1:
                        #Store the words spoken in strTemp
                        strTemp = strTemp + line[index+6:line.find('"',index+6)]
                        
                    #For received data</RECOGOUT>'If there is, the story ends ⇒ Set the flag to True
                    if '</RECOGOUT>' in line:
                        fin_flag = True

                #Execute a print statement for each word spoken
                if fin_flag == True:
                    if 'Thank you' in strTemp:
                        print("You are welcome")
                        main()
                    elif 'Hello' in strTemp:
                        print("Good evening")
                    else:
                        print("Spoken words:" + strTemp)
                    
                    fin_flag = False
                    strTemp = ""

if __name__ == "__main__":

    julius = Julius()
    julius.run()


It glows when called in module mode and the above code is executed in another terminal.

L Chika completed by voice recognition

I was able to get Julius to work in module mode. Next, recognize the words and go to the i2c display display edition. Use raspberry Pi and Julius (speech recognition). ⑤ i2c character display

Reference [Start julius in module mode and get the words spoken in python](https://www.raspberrypirulo.net/entry/julius-python) [Operate the camera like a smart speaker with Raspberry Pi x Julius and Python](https://www.pc-koubou.jp/magazine/20637) [How to resolve that error“:TypeError: can only concatenate str (not ”bytes“) to str”](https://stackoverflow.com/questions/61962436/how-to-resolve-that-errortypeerror-can-only-concatenate-str-not-bytes-to) [Try using voice recognition with Raspberry Pi](https://ueqareer.net/2463)

Recommended Posts

Use raspberryPi and Julius (speech recognition). ④ L Chika
Use raspberryPi and julius (speech recognition). ① Microphone edition
Use raspberryPi and Julius (speech recognition). ⑤ i2c character display
Use raspberry Pi and Julius (speech recognition). ③ Dictionary creation
RaspberryPi L Chika with Python and C #
Speech synthesis and speech recognition by Microsoft Project Oxford
Installation of Docker on Raspberry Pi and L Chika