Skip to content

Commit

Permalink
#16-Adding speech recognition
Browse files Browse the repository at this point in the history
  • Loading branch information
MahmoudRizk committed Jul 1, 2017
1 parent da0c2ee commit 439a221
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
22 changes: 22 additions & 0 deletions alfred/speech_recognition/_speech_recognition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import speech_recognition as sr

class SpeechRecognition():
def __init__(self):
self.r = sr.Recognizer()
self.m = sr.Microphone()

def listen(self):
try:
with self.m as source: self.r.adjust_for_ambient_noise(source)
with self.m as source: self.audio = self.r.listen(source)
message = self.r.recognize_google(self.audio)
print("Google Speech Recognition thinks you said " + message)
return message
m.__exit__()
r.__exit__()
except sr.UnknownValueError:
#print("Google Speech Recognition could not understand audio")
return ''
except sr.RequestError as e:
#print("Could not request results from Google Speech Recognition service; {0}".format(e))
return ''
Binary file added alfred/speech_recognition/male.wav
Binary file not shown.
18 changes: 17 additions & 1 deletion alfred/views/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from alfred import data_rc
import alfred.alfred_globals as ag

from alfred.speech_recognition._speech_recognition import SpeechRecognition

class MainWidget(QDialog, Ui_Dialog):
text_changed = pyqtSignal('QString')
Expand All @@ -22,23 +23,38 @@ def __init__(self, bridge_obj):
self.last_text = ''
self.lineEdit.returnPressed.connect(self.send_text)

self.mic.clicked.connect(self.listen)

self.channel = QWebChannel(self.webView.page())
self.webView.page().setWebChannel(self.channel)

self.channel.registerObject("web_bridge", bridge_obj)


@pyqtSlot()
def send_text(self):
msg = self.lineEdit.text()
if msg != '' and self.last_text != msg:
self.text_changed.emit(msg)
self.last_text = msg

@pyqtSlot()
def listen(self):
print ('start voice listening') ##Debug
self.lineEdit.clear()
self.lineEdit.setPlaceholderText("Start speaking, I am listening...")
s = SpeechRecognition()
msg = s.listen()
if msg == '':
print("Didn't hear it!!")
print (msg) ##Debug
self.lineEdit.setText(msg)
self.send_text()

@pyqtSlot(list)
def set_view(self, components):
temp = ag.main_components_env.get_template("base.html")
html = temp.render(componenets=components)
# print(html)
self.webView.page().setHtml(html)

@pyqtSlot(str)
Expand Down
6 changes: 5 additions & 1 deletion alfred/views/ui/widget_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# PyQt imports
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
from PyQt5.QtWidgets import QVBoxLayout, QHBoxLayout, QWidget
from PyQt5.QtWidgets import QVBoxLayout, QHBoxLayout, QWidget, QPushButton
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineSettings
from PyQt5.QtCore import QUrl, QMetaObject, QRect, QFileInfo

Expand Down Expand Up @@ -64,8 +64,12 @@ def setupUi(self, Dialog):
self.lineEdit.setCursorMoveStyle(QtCore.Qt.VisualMoveStyle)
self.lineEdit.setObjectName("lineEdit")

self.mic = QPushButton('mic')
self.mic.setAutoDefault(False)

self.horizontalLayout.addWidget(self.bot_status_icon)
self.horizontalLayout.addWidget(self.lineEdit)
self.horizontalLayout.addWidget(self.mic)

self.verticalLayout_2.addLayout(self.horizontalLayout)

Expand Down

0 comments on commit 439a221

Please sign in to comment.