-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: main
Are you sure you want to change the base?
Changes from all commits
2aa519e
2a6793e
a054d94
8ab7ea4
32036df
f869c90
5f9bab8
c121a52
0f6288a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
||
return phrase | ||
|
||
|
||
audio = Audio() | ||
audio.phraseFromAudio() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import spacy | ||
|
||
#TODO: Naïve Bayes Classifier |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe rename to |
||||||
self.phrase = self | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
def getPhrase(self): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe rename to |
||||||
return self.phrase | ||||||
|
||||||
def emotionFromPhrase(self): | ||||||
# TODO: implement this | ||||||
Comment on lines
+19
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be within |
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): | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
|
||
if __name__ == "__main__": | ||
# Default data directory is `../data` relative to this file. | ||
|
@@ -42,3 +76,4 @@ def setup(dataDir): | |
print(parser.help()) | ||
else: | ||
setup(options["data"]) | ||
install_packages() |
There was a problem hiding this comment.
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?