Language model

For this project, we'll be using the following set of commands. To keep things simple, we're using only two sets of commands:

  • door open
  • door close

Follow the process that was discussed earlier and create a text file and just write the three unique words:

door open close 

Save it and upload it to the Sphinx knowledge base tool and compile it.

Once you have the compressed file downloaded, move on to the next step with this code:

import collections 
import mraa
import os
import sys
import time

# Import things for pocketsphinx
import pyaudio
import wave
import pocketsphinx as ps
import sphinxbase
# Import for Servo
from Servo import *

led = mraa.Gpio(13)
led.dir(mraa.DIR_OUT)
myServo = Servo("First Servo")
myServo.attach(6)

print("Starting")
while 1:
#PocketSphinx parameters
LMD = "/home/root/Voice-Recognition-using-Intel-Edison/8578.lm"
DICTD = "/home/root/Voice-Recognition-using-Intel-Edison/8578.dic"
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
RECORD_SECONDS = 3
PATH = 'Voice-Recognition-using-Intel-Edison'
p = pyaudio.PyAudio()
speech_rec = ps.Decoder(lm=LMD, dict=DICTD)
#Record audio
stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK)
print("* recording")
frames = []
fori in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
# Write .wav file
fn = "test.wav"
#wf = wave.open(os.path.join(PATH, fn), 'wb')
wf = wave.open(fn, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()

# Decode speech
#wav_file = os.path.join(PATH, fn)
wav_file=fn
wav_file = file(wav_file,'rb')
wav_file.seek(44)
speech_rec.decode_raw(wav_file)
result = speech_rec.get_hyp()
recognised= result[0]
print("* LED section begins")
print(recognised)
ifrecognised == 'DOOR OPEN':
led.write(1)
myServo.write(90)
else:
led.write(0)
myServo.write(0)
cm = 'espeak "'+recognised+'"'
os.system(cm)

The preceding code is more or less similar to the code for switching an LED on and off. The only difference is that the servo control mechanism is added into the existing code. In a simple if else block, we check for the door open and door close conditions. Finally based on what is triggered, we set the LED and the servo to a 90 degrees or 0 degree position.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.76.150