-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathotto.py
80 lines (64 loc) · 2.61 KB
/
otto.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
###############################################################################
#Otto is an emotional robot that answers accordingly to what you say to her
###############################################################################
from lib import record, speech_to_text, tone_analyzer, eyes, camera, output , toArd
import threading, Queue
import sys
#calculate ambient silence threshold
silence_threshold=record.get_trs()
#set eyes thread and display default eyes
#NOTES:-this thread keeps running until the end of the program.
# -to change eyes_emotion, eyes_on.put(on) needs to be issued first
eyes_on=Queue.Queue()
eyes_emotion=Queue.Queue()
eyes_on.put(True)
eyes_emotion.put("neutral")
eyes_thr=threading.Thread(target=eyes.displayEyes, args=(eyes_on, eyes_emotion))
eyes_thr.start()
#set camera thread
#NOTE: -this thread on/off is triggered with a camera_stop.put(bool)
camera_stop=Queue.Queue()
camera_emotion=Queue.Queue()
camera_thr=threading.Thread(target=camera.detectFaces, args=(camera_stop, camera_emotion))
camera_thr.start()
while (1):
try:
#start looking for faces
camera_stop.put(False)
#record what the user has to say and save to ./records/user-record.wav
record.detectVoice(silence_threshold, camera_emotion)
camera_stop.put(False)
#if camera got emotion
if (not camera_emotion.empty()):
emotion=camera_emotion.get()
#if the microphone got emotion
else:
#set loading eyes
eyes_emotion.put("thinking")
eyes_on.put(True)
#send the audio to the ibm speech-to-text api and get their json response
transcript=speech_to_text.stt()
#if noise was recorded, record again
if (transcript == False):
#get back to default eyes state
eyes_emotion.put("neutral")
eyes_on.put(True)
continue
#use otto-lexicon and ibm tone-analyzer to get the emotion
emotion=tone_analyzer.getPredominantEmotion(transcript)
#otto reacts with his eyes
eyes_emotion.put(emotion)
eyes_on.put(True)
#otto's reaction to the emotion
output.react(emotion)
#get back to default eyes state
eyes_emotion.put("neutral")
eyes_on.put(True)
#terminate threads when keyboard interrupts occur
except(KeyboardInterrupt, SystemExit):
print("Wrapping eyes thread up...")
eyes_on.put(False)
camera_stop.put(True)
eyes_thr.join()
camera_thr.join()
sys.exit()