-
Notifications
You must be signed in to change notification settings - Fork 0
/
adialog_new.py
198 lines (163 loc) · 5.83 KB
/
adialog_new.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# -*- encoding: utf-8 -*-
# imports
import argparse
import sys
import time
import datetime
import csv
from naoqi import (
ALProxy,
ALBroker,
ALModule )
# proxies
Move = None
memory = None
asr = None
# other global objects
writer = None # writer of the csv file
f = None # file to write to
#inputTime = 0 # holds the time of the speech
robotIP = "nico.d.mtholyoke.edu"
# dictionary from text to digits
nums = {
"one" : 1,
"two" : 2,
"to" : 2,
"three" : 3,
"four" : 4,
"five" : 5
}
digits = [1, 2, 3, 4, 5]
# Module for Speech
class TestModule(ALModule):
def __init__(self, name):
ALModule.__init__(self, name)
global memory
memory = ALProxy("ALMemory")
# when these events change, the methods are called
'''memory.subscribeToEvent("forward", "Move", "onForward")
memory.subscribeToEvent("back", "Move", "onBack")
memory.subscribeToEvent("left", "Move", "onLeft")
memory.subscribeToEvent("right", "Move", "onRight")
memory.subscribeToEvent("turn", "Move", "onTurn")
memory.subscribeToEvent("other", "Move", "onOther")'''
memory.subscribeToEvent("Dialog/LastInput", "Move", "onInput")
memory.subscribeToEvent("ALTextToSpeech/CurrentSentence", "Move", "onCurrentSentence")
memory.subscribeToEvent("SpeechDetected", "Move", "onSpeechDetected")
global inputTime
inputTime = time.time()
global isDone
isDone = True
# when speech is detected, set inputTime's value to the current time
def onSpeechDetected(self, name, value):
global inputTime
global isDone
# if human is just starting to speak, set input time, otherwise don't
if isDone:
inputTime = time.time()
isDone = False
# when a word/phrase is recognized, write the recognized phrase to file
def onInput(self, name, value):
memory.unsubscribeToEvent("Dialog/LastInput", "Move")
global inputTime
global isDone
# once the phrase is recognized, isDone can be set to true
isDone = True
# put the timestamp into a readable format
st = datetime.datetime.fromtimestamp(inputTime).strftime('%a %h %d %H:%M:%S %Y')
# write the timestamp and the utterance into the csv file (if not empty)
if value != "":
writer.writerow({' Time': st, 'human': value})
memory.subscribeToEvent("Dialog/LastInput", "Move", "onInput")
# when the robot says something, write the phrase to file
def onCurrentSentence(self, name, value):
memory.unsubscribeToEvent("ALTextToSpeech/CurrentSentence", "Move")
# if the value is not empty, write to file
if value != "":
# get timestamp and convert to readable format
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%a %h %d %H:%M:%S %Y')
# write the timestamp and text into the csv file
writer.writerow({' Time': st, 'robot': value})
memory.subscribeToEvent("ALTextToSpeech/CurrentSentence", "Move", "onCurrentSentence")
# creates speech recognition proxy to add vocabulary to the robot
def createSpeechRecogProxy(robot_ip, robot_port):
# creates the speech recognition proxy
global asr
asr = ALProxy("ALSpeechRecognition", robot_ip, robot_port)
asr.setLanguage("English")
asr.pause(True)
# adds vocabulary, sets it, and enables word spotting
vocabulary = ["what is the slope of the line from"]
asr.setVocabulary(vocabulary, True)
asr.pause(False)
asr.subscribe("Test_ASR")
# creates csv file and its writer, takes in fName as input
def createFile(fName):
global f
# opens the file and creates a writer
f = open(fName, 'w')
global writer
# sets the fieldnames and writes the header
writer = csv.DictWriter(f, fieldnames=[' Time', 'human', 'robot'])
writer.writeheader()
# main method
def main(robot_ip, robot_port, topf_path, csvf):
# creates the speech recognition proxy and a csv file
createSpeechRecogProxy(robot_ip, robot_port)
createFile(csvf)
# creates dialog and posture proxies
dialog_p = ALProxy('ALDialog', robot_ip, robot_port)
postureProxy = ALProxy("ALRobotPosture", robot_ip, robot_port)
dialog_p.setLanguage("English")
tts = ALProxy('ALTextToSpeech', robot_ip, robot_port)
tts.say("starting")
postureProxy.goToPosture("StandInit", 0.5) # brings robot to standing pos.
# Load topic - absolute path is required
# TOPIC MUST BE ON ROBOT
topic = dialog_p.loadTopic(topf_path)
# Start dialog
dialog_p.subscribe('myModule')
# Activate dialog
dialog_p.activateTopic(topic)
# create broker
myBroker = ALBroker("myBroker", "0.0.0.0",
0, robot_ip, robot_port)
'''# creates a module called "Move"
global Move
Move = TestModule("Move")'''
# pressing key will unsubscribe from the topic
raw_input(u"Press 'Enter to exit.")
#speech recognition unsubscribing from test_asr wtf?:
asr.unsubscribe("Test_ASR")
# until interrupted, keep broker running
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print
print "Interrupted by user, shutting down"
myBroker.shutdown()
# Deactivate topic
dialog_p.deactivateTopic(topic)
# Unload topic
dialog_p.unloadTopic(topic)
# Stop dialog
dialog_p.unsubscribe('myModule')
# close file
f.close()
# exit
sys.exit(0)
if __name__ == '__main__':
global csvf
#csvf = 'test{}.csv'.format(time.time())
csvf = 'testApril20.csv'
filepath = "/home/nao/programs/move.top"
main(robotIP, 9559, filepath, csvf)
'''
TODO:
additional speaking feature
yay hip shake
http://doc.aldebaran.com/2-1/nao/nao_freeze.html#nao-freeze
repeat thing
'''