Seeed
Published

DIY Smart Home Assistant with Raspberry Pi and ReSpeaker

ReSpeaker Mic Array, as the “ear” of Raspberry Pi here, can listen to your speech commands and send them to Raspberry Pi.

IntermediateFull instructions provided3 hours3,345
DIY Smart Home Assistant with Raspberry Pi and ReSpeaker

Things used in this project

Hardware components

ReSpeaker Mic Array - Far-field w/ 7 PDM Microphones
Seeed Studio ReSpeaker Mic Array - Far-field w/ 7 PDM Microphones
×1
Seeed Studio Raspberry Pi 3 Model B+
×1
Wio Link Starter Kit
Seeed Studio Wio Link Starter Kit
×1
SD card
×1
Seeed Studio USB Cable Type A to B - 30CM Black
×2

Story

Read more

Code

Code snippet #6

Plain text
git submodule init
git submodule update
cd snowboy/swig/Python
sudo apt-get install python-dev libatlas-base-dev swig           # requiremetns to compile snowboy
echo 'from snowboydetect import *' >__init__.py     # create__init__.pyfor a python module
cd ../../..                                          # chang to the root directory of the repository
ln -s snowboy/swig/Python snowboydetect
python kws_doa.py

Code snippet #8

Plain text
from respeaker.bing_speech_api import BingSpeechAPI as Bingimport wave
from mic_array import MicArray
import Queue
from pixel_ring import pixel_ring
import sys
import numpy as np
import collections
from snowboydetect import SnowboyDetect
import time
import json
from urllib import urlencode
from urllib2 import Request, urlopen, URLError, HTTPError# write your Wio token hereWIO_TOKEN = "**************"# write your Bing key hereKEY = "**********"
bing = Bing(key=KEY)RATE = 16000
CHANNELS = 8
KWS_FRAMES = 10     # ms
DOA_FRAMES = 800    # msdetector = SnowboyDetect('snowboy/resources/common.res', 'snowboy/resources/snowboy.umdl')
detector.SetAudioGain(1)
detector.SetSensitivity('0.5')# about 5seconds
q = Queue.Queue(maxsize=768)def gen_queue(q):
    try:
        data = q.get(timeout=1)
        while data:
            yield data
            data = q.get(timeout=1)
    except Queue.Empty:
        passdef controlLED(onoff=0): 
    try:
        if onoff == 1:
            rgb_hex_string = '000080'
        else:
            rgb_hex_string = '000000'
        url = 'https://cn.wio.seeed.io/v1/node/GroveLedWs2812D0/clear/4/{}?access_token={}'.format(rgb_hex_string, WIO_TOKEN)
        request = Request(url, data='')
        response = urlopen(request)
        data = response.read()
        result = json.loads(data)
        if result['result'] == 'ok':
            return True
        else:
            return False
    except Exception as err:
        return Falsedef main():
    history = collections.deque(maxlen=int(DOA_FRAMES / KWS_FRAMES))
    global q    try:
        with MicArray(RATE, CHANNELS, RATE * KWS_FRAMES / 1000)  as mic:
            for chunk in mic.read_chunks():
                history.append(chunk)
                # Detect keyword from channel 0
                ans = detector.RunDetection(chunk[0::CHANNELS].tostring())
                if ans > 0:
                    print("wake up")
                    print("start recording")
                    pixel_ring.arc(12)
                    q.queue.clear()
                    for chunk in mic.read_chunks():
                        q.put(chunk[0::CHANNELS].tostring())
                        if q.full():
                            break
                    print "queue full"
                    pixel_ring.spin()
                    text = bing.recognize(gen_queue(q))   # data can be generator
                    if text:
                        print('{}'.format(text))
                        if 'turn on' in text:
                            controlLED(1)
                        if 'turn off' in text:
                            controlLED(0)
                    pixel_ring.off()    except KeyboardInterrupt:
        pass    pixel_ring.off()
    # except ValueError:
    #     passif __name__ == '__main__':
    main()

Credits

Seeed

Seeed

102 projects • 168 followers
Seeed R&D Team

Comments