-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
120 lines (107 loc) · 3.57 KB
/
Copy pathtest.py
File metadata and controls
120 lines (107 loc) · 3.57 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
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
import pyttsx3
import speech_recognition as sr
import json
import playsound
from gtts import gTTS
import random
import os
import Detectorr
from pydub import AudioSegment
list_of_variables = ['.user.','.date.','.time.']
list_of_commands = {}
def say(s):
print('SSAM : ' + s)
sound = AudioSegment
for x in s.split():
paxx = 'SpeechData\\'+x+'.mp3'
print(paxx)
if os.path.exists(paxx):
DS= open(paxx,'r')
c = AudioSegment.from_mp3(paxx)
sound = sound + c
else:
gTTS(text=x, lang='hi').save(paxx)
c = AudioSegment.from_mp3(paxx)
sound = sound + c
sound.export('SpeechData\\' + 'temp' + ".mp3")
playsound.playsound('SpeechData\\' + 'temp' + ".mp3")
def say_n_listen(s):
say(s)
return listen_audio()
def listen_audio():
# get audio from the microphone
print("wait")
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
print("Set minimum energy threshold to {}".format(r.energy_threshold))
print("Speak!!!")
audio = r.listen(source)
print("processing...")
# print(r.)
# r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")
try:
speech = r.recognize_google(audio)
print("You : " + speech)
return speech
except sr.UnknownValueError:
return "Could not understand audio"
return ''
except sr.RequestError as e:
print("Could not request results; {0}".format(e))
return 'sorry can\'t reach the internet'
current_answer = 0
q = []
fin = 0
def refresh_file():
fin = open('QB.dat', 'r')
for n in fin:
simple_json = json
s = simple_json.loads(n)
q.append(s)
fin.close()
refresh_file()
engine = pyttsx3.init()
while 1:
user = Detectorr.get_face()
main_command = listen_audio()
current_answer = {'Question':'','Answer':'','command':''}
if main_command == 'bye':
say('Have a nice day, Master')
break
if main_command == '':
say('Please Say something')
for x in q:
if x['Question'].upper() == main_command.upper():
current_answer = x
if current_answer in q:
if current_answer['command'].find('.user.') != -1:
current_answer['command'] = current_answer['command'].replace('.user.', user)
if current_answer['Answer'].find('.user.') != -1:
current_answer['Answer'] = current_answer['Answer'].replace('.user.', user)
if current_answer['Question'].find('Open') != -1:
current_answer['Answer'] = 'The work is under construction here'
say(current_answer['Answer'])
if current_answer['command'] == '':
pass
else:
os.system(current_answer['command'])
else:
say('Sorry I dont know the answer')
say('would you like to teach me just say yes or no')
choise = listen_audio()
if choise == 'yes':
qna = {'Question': main_command, 'Answer': say_n_listen('Enter the answer of the question'),
'command': ''}
out = open('QB.dat', 'a')
simple_json = json
n = simple_json.dumps(qna)
out.write(n)
out.write('\n')
out.close()
refresh_file()
pass
elif choise == 'no':
continue
else:
say('Sorry you have to respose in yes or no')