Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
sirambd committed Feb 12, 2020
2 parents d45b858 + 359824a commit 33ee753
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A speech to text to control an IoT object
>This project is launched with a python 3.6
> On Ubuntu, you need to run:
> `sudo apt update && sudo apt-get install portaudio19-dev`
> `sudo apt update && sudo apt-get install portaudio19-dev swig libpulse-dev libasound2-dev`
> `pip install --upgrade pyaudio`
To run the underlying rasa server __look at [this documentation](plant_intent_recognizer/README.md)__
Expand Down
21 changes: 21 additions & 0 deletions basic_speech_to_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@
import speech_recognition as sr


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
: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
"""
r = sr.Recognizer()
with sr.Microphone() as source:
if noise_level:
r.energy_threshold = noise_level
else:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
text = None
with suppress(sr.UnknownValueError, sr.RequestError):
text = r.recognize_sphinx(audio)
return keyword in text if text else False


def speech_to_text(noise_level: int = None) -> Union[None, str]:
"""
Function called to listen and convert to text the answer of the user.
Expand Down
2 changes: 1 addition & 1 deletion plant_intent_recognizer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ We do not use the full capacities of rasa but [only the NLU part](https://rasa.c
- Start the server with:
`rasa run -m models --enable-api --cors “*” --debug`
- Interact with the server with an HTTP POST request like:
`curl localhost:5005/model/parse -d '{"text":"hello"}`
`curl localhost:5005/model/parse -d '{"text":"hello"}'`
58 changes: 47 additions & 11 deletions voice_controller.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import threading
from time import sleep, time
from typing import Callable, Dict, List

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

CALLBACK_INTENTS: Dict[Intent, List[Callable[[], None]]] = {}
Expand Down Expand Up @@ -33,17 +35,47 @@ def _trigger_function_on_intent(intent: Intent):

class VoiceController:

def __init__(self):
self.rasa_intent = RasaIntent()
def __init__(self, active_time_delay=10):
"""
:param active_time_delay time in seconds after the keyword was said before being not "active"
"""
self._rasa_intent = RasaIntent()
self.active = False
self._stop = False
self.active_time_delay = active_time_delay
self.last_active_time = None

self._thread = threading.Thread(target=self.run, args=())
self._thread.daemon = True # Daemonize thread

def stop(self):
"""Stopping gracefully, might take a few seconds"""
self._stop = True
self._thread.join()

def start(self):
self._thread.start() # Call run()

def run(self):
while True:
text = speech_to_text()
print(f"text: {text}")
if text:
intent = self.rasa_intent.detect_intent(text)
print(f"intent: {intent}\n")
_trigger_function_on_intent(intent)
self._stop = False
while not self._stop:
if self.active: # We actively listen to user command
text = speech_to_text()
print(f"text: {text}", flush=True)
if text:
intent = self._rasa_intent.detect_intent(text)
print(f"intent: {intent}\n", flush=True)
_trigger_function_on_intent(intent)
self.last_active_time = time()
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):
print("ACTIVE MODE", flush=True)
self.active = True
self.last_active_time = time()
else:
print("😴", end='', flush=True) # Still in sleep mode


if __name__ == '__main__':
Expand All @@ -64,4 +96,8 @@ def goodbye():


vc = VoiceController()
vc.run()
vc.start()
print("I can continue to do stuff")
sleep(60)
print("Time to stop")
vc.stop()

0 comments on commit 33ee753

Please sign in to comment.