-
Notifications
You must be signed in to change notification settings - Fork 4
/
my_robot.py
222 lines (171 loc) · 5.15 KB
/
my_robot.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import pygame, sys, time, random
from pygame.locals import *
from time import *
import curses
#from curses.ascii import isdigit
import os
import _thread
import threading
import re
import triggerWord.trigger_word_from_audio_stream
from triggerWord.trigger_word_from_audio_stream import detect
from triggerWord.trigger_word_from_audio_stream import initDetecter
import queue
from voiceRecognition import listen
from gtts import gTTS
from pygame import mixer
from chatbot import main_simple_seq2seq
from chatbot.main_simple_seq2seq import predict
import shutil
import time
import pydub
mixer.init()
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255,255,0)
input_q = queue.Queue()
result_q = queue.Queue()
#init things...
pygame.init()
windowSurface = pygame.display.set_mode((390, 510), 0, 32)
pygame.display.set_caption("Bounce")
pygame.event.get()
info = pygame.display.Info()
sw = info.current_w
sh = info.current_h
y = 0
windowSurface.fill(WHITE)
myfont = pygame.font.SysFont("ComicSans", 35)
def load_all_imgs(directory):
return {str(f): pygame.image.load(directory + f) for f in os.listdir(directory)}
imgsMapper = load_all_imgs("./chatbot_imags/")
#graphic function
def sleeping():
displayImg(imgsMapper['sleeping.jpg'])
windowSurface.fill(BLACK)
def getAnswer(query):
answers = []
#random.seed(10)
answers = predict(query)
answers = [" ".join(answer.split()) for answer in answers if not 'unk' in answer]
#index = random.randint(0, len(answers))
if answers == []:
answers.append("bilmiyorum")
f = open("getAnswer.txt", 'w', encoding = 'utf-8')
f.write(answers[0])
f.close()
#graphic function
def listening():
displayImg(imgsMapper['ready.jpg'])
windowSurface.fill(BLACK)
def saySomething(answer, num):
sound = pydub.AudioSegment.from_mp3("temp/response"+ str(num) + ".mp3")
sound.export("temp/response"+ str(num) + ".wav",format='wav')
mixer.music.load("temp/response"+ str(num) + ".wav")
mixer.music.play()
def speaking(sentence, num):
tts = gTTS(text= str(sentence), lang='tr')
tts.save("temp/response"+ str(num) + ".mp3")
#sf = TemporaryFile()
#tts.write_to_fp("temp/response"+ str(num) + ".mp3")
try:
_thread.start_new_thread( saySomething,(sentence, num,))
except Exception as e:
print(e)
workingSentence = " "
sleep(0.24)
for word in sentence.split():
word = word.lower()
timePerChar = 0.244/float(len(word))
i = 0
for char in word:
if i == 0:
displayImg(imgsMapper['ready.jpg'])
elif i == 1:
displayImg(imgsMapper['speaking_1.jpg'])
elif i == 2:
displayImg(imgsMapper['speaking_2.jpg'])
elif i == 3:
displayImg(imgsMapper['speaking_1.jpg'])
else:
i = 0
i += 1
myfont = pygame.font.SysFont("ComicSans", 17)
workingSentence += char[0]
label = myfont.render(workingSentence, 1, WHITE)
windowSurface.blit(label, (5, 5))
pygame.display.update()
sleep(timePerChar + 0.1)
workingSentence += " "
displayImg(imgsMapper['ready.jpg'])
myfont = pygame.font.SysFont("ComicSans", 17)
label = myfont.render(workingSentence, 1, BLACK)
windowSurface.blit(label, (5, 5))
pygame.display.update()
sleep(0.04)
windowSurface.fill(BLACK)
myfont = pygame.font.SysFont("ComicSans", 17)
label = myfont.render(workingSentence, 1, BLACK)
windowSurface.blit(imgsMapper['ready.jpg'],(0,0))
windowSurface.blit(label, (5, 5))
pygame.display.update()
sleep(0.4)
def wakingUp():
displayImg(imgsMapper['wakingUp_1.jpg'])
sleep(0.1)
displayImg(imgsMapper['wakingUp_2.jpg'])
sleep(0.1)
displayImg(imgsMapper['wakingUp_3.jpg'])
sleep(0.1)
def displayImg(img):
global windowSurface
for evt in pygame.event.get():
if evt.type == pygame.QUIT:
pygame.quit()
sys.exit()
windowSurface.fill(BLACK)
windowSurface.blit(img, (0,0))
pygame.display.update()
pygame.display.flip()
def main():
if os.path.exists("triggered.txt"):
os.remove("triggered.txt")
if not os.path.exists("temp"):
os.makedirs("temp")
_thread.start_new_thread(detect, (1,))
#isinstance(result_q.get(), bool) and result_q.get() !=
while(not os.path.exists("triggered.txt")):
sleeping()
wakingUp()
random.seed(time.time())
i = random.randint(0, 20000)
run = True
while run:
try:
if os.path.exists("query.txt"):
os.remove("query.txt")
if os.path.exists("getAnswer.txt"):
os.remove("getAnswer.txt")
_thread.start_new_thread(listen, (1,))
while(not os.path.exists("query.txt")):
listening()
query = open("query.txt", "r", encoding = 'utf-8').read()
#get predictions and save it to a file
_thread.start_new_thread(getAnswer, (query,))
while not os.path.exists("getAnswer.txt"):
listening()
answer = open("./getAnswer.txt", "r", encoding = 'utf-8').read()
print(answer)
i = random.randint(0, 20000)
try:
speaking(answer, i)
except Exception as e:
print(e)
except (KeyboardInterrupt):
run = False
shutil.rmtree("./temp")
if __name__ == '__main__':
main()