Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speech recognition #19

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 48 additions & 18 deletions scripts/recognition/audio.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,48 @@
from .emotion import Emotion
from .phrase import Phrase

def emotionFromAudio(audio) -> Emotion:
"""
Extracts the emotion from a voice.
"""
emotion = Emotion.NEUTRAL
# TODO: implement this
return emotion

def phraseFromAudio(audio) -> Phrase:
"""
Extracts the phrase from a voice.
"""
phrase = Phrase.UNKNOWN
# TODO: implement this
return phrase
# from .emotion import Emotion
# from .phrase import Phrase

from vosk import Model, KaldiRecognizer

import pyaudio
import json

import signal
import sys


class Audio():

def __init__(self):
self.model = Model("../../data/vosk-model-small-en-us-0.15")
self.recognizer = KaldiRecognizer(self.model, 16000)
self.stream = pyaudio.PyAudio().open(format= pyaudio.paInt16, channels=1, rate= 16000, input=True, frames_per_buffer=8192)
self.stream.start_stream()

# def emotionFromAudio(self) -> Emotion:
# """
# Extracts the emotion from a voice.
# """
# emotion = Emotion.NEUTRAL
# # TODO: implement this
# return emotion

def phraseFromAudio(self):
"""
Extracts the phrase from the audio.
"""
phrase = ""
while True:
try:
data = self.stream.read(4096)
if self.recognizer.AcceptWaveform(data):
res = json.loads(self.recognizer.Result())
phrase = res["text"]
print(phrase)
except KeyboardInterrupt:
break
Comment on lines +34 to +42
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any ideas on how to integrate it with the rest of the code?


return phrase


audio = Audio()
audio.phraseFromAudio()
3 changes: 3 additions & 0 deletions scripts/recognition/nlp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import spacy

#TODO: Naïve Bayes Classifier
12 changes: 12 additions & 0 deletions scripts/recognition/phrase.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,15 @@ class Phrase(Enum):
HI = 1
BYE = 2
LOOK_AT_ME = 3

def __init__(self, phrase) -> None:
self.phrase = phrase

def setPhrase(self, phrase):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename to set?

self.phrase = self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.phrase = self
self.phrase = phrase


def getPhrase(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename to get?

return self.phrase

def emotionFromPhrase(self):
# TODO: implement this
Comment on lines +19 to +20
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be within nlp.py?

35 changes: 35 additions & 0 deletions scripts/setup.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
from core import Parser
from zipfile import ZipFile

import os
import requests

import sys
import subprocess #https://docs.python.org/3/library/subprocess.html


# Files to download into the data directory.
FILES = [
"https://raw.githubusercontent.com/kipr/opencv/master/data/haarcascades/haarcascade_frontalface_default.xml",
"https://raw.githubusercontent.com/kipr/opencv/master/data/haarcascades/haarcascade_eye_tree_eyeglasses.xml",
"https://alphacephei.com/vosk/models/vosk-model-small-en-us-0.15.zip"
]

PACKAGES = [
"vosk",
"spacy",
"PyAudio"
]

def setup(dataDir):
Expand All @@ -28,6 +40,28 @@ def setup(dataDir):
filename = os.path.basename(file)
path = os.path.join(dataDir, filename)
open(path, "wb").write(response.content)

# Loading the temp.zip and creating a zip object
with ZipFile("../data/vosk-model-small-en-us-0.15.zip", 'r') as zObject:

# Extracting the zip
zObject.extractall(path="../data")

# Delete the zip file
os.remove("../data/vosk-model-small-en-us-0.15.zip")
Comment on lines +45 to +51
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to generalize this and move it inside the for above it.
Check if the file extension is .zip, in which case you extract it and delete the .zip file.



def install_packages():
"""Install the required python libraries."""

for package in PACKAGES:
print("Downloading python library {}...".format(package))
try:
subprocess.check_call([sys.executable, "-m", "pip", "install", package], stdout=subprocess.DEVNULL)
except Exception as e:
print("Failed to download library {}".format(package))
print("Exception {}".format(e))
Comment on lines +54 to +63
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Installing python packages is not so straight forward on my distro, I would prefer us adding a requirements.txt with the necessary packages and just install them with pip.



if __name__ == "__main__":
# Default data directory is `../data` relative to this file.
Expand All @@ -42,3 +76,4 @@ def setup(dataDir):
print(parser.help())
else:
setup(options["data"])
install_packages()