forked from mansi1710/Assistant-for-visually-impaired
-
Notifications
You must be signed in to change notification settings - Fork 0
/
speech.py
69 lines (60 loc) · 2.54 KB
/
speech.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import speech_recognition as sr
import pyttsx3
from google.oauth2 import service_account
from nltk.stem.porter import PorterStemmer
from nltk.stem.wordnet import WordNetLemmatizer
import re
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import RegexpTokenizer
from nltk.stem.wordnet import WordNetLemmatizer
class speech_to_text():
def __init__(self):
en_voice_id = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0"
ru_voice_id = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_RU-RU_IRINA_11.0"
self.recognizer = sr.Recognizer()
self.microphone = sr.Microphone()
self.engine = pyttsx3.init();
self.engine.setProperty('voice', en_voice_id)
self.credentials = service_account.Credentials.from_service_account_file('api-key.json')
def recognize_speech_from_mic(self):
print("Start...")
with self.microphone as source:
self.recognizer.adjust_for_ambient_noise(source)
audio = self.recognizer.listen(source)
print("Found mic")
response = {
"success": True,
"error": None,
"transcription": None
}
try:
response["transcription"] = self.recognizer.recognize_google(audio)
except sr.RequestError:
# API was unreachable or unresponsive
response["success"] = False
response["error"] = "API unavailable"
except sr.UnknownValueError:
# speech was unintelligible
response["error"] = "Unable to recognize speech"
if(response["transcription"]=="None"):
print("Speech not detected! Pls try again!")
return response["transcription"]
def clean(self, text):
lem = WordNetLemmatizer()
stem = PorterStemmer()
stop_words = set(stopwords.words("english"))
new_words = ["hey", "hi", "hello", "what's up", "i", "please", "help", "using", "show", "result", "large",
"also", "iv", "one", "two", "new", "previously", "shown"]
stop_words = stop_words.union(new_words) - {"whom", "who"}
text = text.lower()
text = text.split()
ps = PorterStemmer()
lem = WordNetLemmatizer()
text = [lem.lemmatize(word) for word in text if not word in
stop_words]
text = " ".join(text)
return text
def text_speech(self, cleaned_text):
self.engine.say(cleaned_text);
self.engine.runAndWait();