-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeechReco.py
More file actions
83 lines (69 loc) · 2.27 KB
/
speechReco.py
File metadata and controls
83 lines (69 loc) · 2.27 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from naoqi import ALProxy, ALModule
import sys
import time
import threading
class MockWordlistReco(object):
"""Mock word recognition module reading words from standard input."""
def __init__(self):
self.word_list = []
def set_word_list(self, word_list):
self.word_list = word_list
def recognize(self):
word = sys.stdin.readline().strip()
# print word
if word in self.word_list:
# print word
return word
else:
return None
class WordlistSpeechReco(ALModule):
"""Word recognition module working with word lists."""
NAME = "Reco"
def __init__(self, broker, treshold = 0.3):
ALModule.__init__(self, WordlistSpeechReco.NAME)
self.broker = broker
self.treshold = treshold
self.word_list = []
self.asr = ALProxy("ALSpeechRecognition")
self.asr.setLanguage("English")
self.recognized_word = None
self.event = threading.Event()
self.event.clear()
global memory
if self.broker != None:
memory = ALProxy("ALMemory")
def set_word_list(self, word_list):
"""Set word list used for recognition."""
self.word_list = word_list
def recognize(self):
"""Return the recognized word."""
if self.word_list == []:
self.recognized_word = None
return
else:
self.asr.setWordListAsVocabulary(self.word_list)
print "subscribing", WordlistSpeechReco.NAME, self.name
memory.subscribeToEvent("WordRecognized",
WordlistSpeechReco.NAME,
"onWordRecognized")
print "waiting"
self.event.wait()
self.event.clear()
return self.recognized_word
def onWordRecognized(self, event, value, identifier):
"""Callback invoked when word is recognized by robots speech recognition.
Sets the recognized word and the flag in order to resume the execution thread."""
memory.unsubscribeToEvent("WordRecognized",
WordlistSpeechReco.NAME)
print value
self.recognized_word = self.__chooseRecognizedWord(value)
print self.recognized_word
self.event.set()
def __chooseRecognizedWord(self, value):
"""Choose the recognized word based on the value returned from speech recognition.
Value is a list, where items 2 * i are words and 2 * i + 1 probabilities.
Value is sorEnted in a descending order by probability. """
# Uncomment this for treshold testing
# if float(value[1]) < self.tresh:
# return None
return value[0]