Repository navigation
Expand file tree
/
Copy pathcasper.py
More file actions
66 lines (57 loc) · 1.58 KB
/
Copy pathcasper.py
File metadata and controls
66 lines (57 loc) · 1.58 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
#!/usr/bin/env
import speech_recognition as sr
import io
from subprocess import Popen, PIPE
from gtts import gTTS
from getpass import getuser
username = getuser()
Triger_assistent = [
'hey Casper',
'eh Casper',
'hey casper',
'eh casper'
'Hey Casper',
'Eh Casper',
'Hey casper',
'Eh casper']
def listen_for_command():
r = sr.Recognizer()
with sr.Microphone() as source:
print('Say Something!')
audio = r.listen(source)
print('Done!')
try:
text = r.recognize_google(audio)
except sr.UnknownValueError:
pass
return None
else:
print(text)
return text
def text_to_speech(text):
tts = gTTS(text=text, lang='en', slow=False)
audio_buffer = io.BytesIO()
tts.write_to_fp(audio_buffer)
audio_buffer.seek(0)
proc =Popen(['mpg123', '-o', 'oss:/dev/dsp4', '--quiet', '-'], stdin=PIPE)
proc.communicate(audio_buffer.read())
def what_to_do():
text_to_speech('What you want to do?')
while True:
answer = listen_for_command()
if answer is None:
text_to_speech('I did not understand')
else:
break
answer_list = answer.split()
if answer_list[0] in ['open', 'Open']:
text_to_speech(f'Opening {answer_list[1]}')
Popen(answer_list[1].lower(), shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
elif answer.lower() == 'goodbye':
text_to_speech('OK goodbye')
exit()
while True:
text = listen_for_command()
if text is not None:
if text in Triger_assistent:
what_to_do()