Skip to content

Commit

Permalink
Added hotword detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo-C committed Feb 13, 2020
1 parent 33ee753 commit a9dfbd6
Show file tree
Hide file tree
Showing 23 changed files with 52 additions and 209 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,14 @@ A speech to text to control an IoT object
To run the underlying rasa server __look at [this documentation](plant_intent_recognizer/README.md)__

The start script is [voice_controller.py](voice_controller.py)
The start script is [voice_controller.py](voice_controller.py)

To use is_wake_up_word_said function, you need to figure out the index of your mic, to do that, you can run:
```python
import pyaudio
p = pyaudio.PyAudio()
for i in range(p.get_device_count()):
print(p.get_device_info_by_index(i))
```

And choose the one with the `'name': 'default'`
39 changes: 39 additions & 0 deletions basic_speech_to_text.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,55 @@
"""
File to manage the speech to text for our plant.
"""
import struct
from contextlib import suppress
from time import time
from typing import Union

import pyaudio
import speech_recognition as sr
import pvporcupine


def is_wake_up_word_said(input_device_index=13, sensitivity=0.5, keyword='hey pico', timeout=10):
keyword_file_path = [pvporcupine.KEYWORD_FILE_PATHS[keyword]]
num_keywords = len(keyword_file_path)

porcupine = pvporcupine.create(
library_path=pvporcupine.LIBRARY_PATH,
model_file_path=pvporcupine.MODEL_FILE_PATH,
keyword_file_paths=keyword_file_path,
sensitivities=[sensitivity] * num_keywords)

pa = pyaudio.PyAudio()
audio_stream = pa.open(
rate=porcupine.sample_rate,
channels=1,
format=pyaudio.paInt16,
input=True,
frames_per_buffer=porcupine.frame_length,
input_device_index=input_device_index)

start = time()
keyword_said = False
while not keyword_said and time() - start < timeout:
pcm = audio_stream.read(porcupine.frame_length)
pcm = struct.unpack_from("h" * porcupine.frame_length, pcm)

if porcupine.process(pcm):
keyword_said = True
audio_stream.close()
porcupine.delete()
return keyword_said


def is_keyword_said(keyword="ok", noise_level: int = None) -> bool:
"""
Function called to check if the keyword is said
Work offline to preserve privacy at home
This function is DEPRECATED
:param: keyword the word to be said only to start (SHOULD BE CAREFULLY CHOSEN/TESTED)
:param: noise_level the level of ambient noise used to detect the end of a phrase
:return: Is the keyword said or not
Expand Down
2 changes: 0 additions & 2 deletions porcupine/keyword_files/linux/hey pico_linux.ppn

This file was deleted.

This file was deleted.

This file was deleted.

Binary file removed porcupine/keyword_files/windows/hey pico_windows.ppn
Binary file not shown.

This file was deleted.

20 changes: 0 additions & 20 deletions porcupine/lib/README.md

This file was deleted.

Binary file removed porcupine/lib/common/porcupine_compressed_params.pv
Binary file not shown.
Binary file removed porcupine/lib/common/porcupine_params.pv
Binary file not shown.
Binary file removed porcupine/lib/linux/x86_64/libpv_porcupine.a
Binary file not shown.
Binary file removed porcupine/lib/linux/x86_64/libpv_porcupine.so
Binary file not shown.
Binary file removed porcupine/lib/raspberry-pi/arm11/libpv_porcupine.a
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed porcupine/lib/windows/amd64/libpv_porcupine.a
Binary file not shown.
Binary file removed porcupine/lib/windows/amd64/libpv_porcupine.dll
Binary file not shown.
90 changes: 0 additions & 90 deletions porcupine/porcupine.py

This file was deleted.

91 changes: 0 additions & 91 deletions porcupine/util_porcupine.py

This file was deleted.

4 changes: 2 additions & 2 deletions voice_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from time import sleep, time
from typing import Callable, Dict, List

from basic_speech_to_text import speech_to_text, is_keyword_said
from basic_speech_to_text import speech_to_text, is_keyword_said, is_wake_up_word_said
from plant_intent_recognizer.detect_intent import RasaIntent, Intent

CALLBACK_INTENTS: Dict[Intent, List[Callable[[], None]]] = {}
Expand Down Expand Up @@ -70,7 +70,7 @@ def run(self):
elif time() - self.last_active_time > self.active_time_delay:
print("SLEEP MODE", flush=True)
self.active = False
elif is_keyword_said(noise_level=1500):
elif is_wake_up_word_said():
print("ACTIVE MODE", flush=True)
self.active = True
self.last_active_time = time()
Expand Down

0 comments on commit a9dfbd6

Please sign in to comment.