-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ana.py
130 lines (105 loc) · 3.68 KB
/
ana.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import speech_recognition as sr
from subprocess import call
from func import fbase
from func import criaaudio
import json
from playsound import playsound
import vlc
##### configurações #####
config = open("config/config.json", "r")
config_json = json.load(config)
hotword = config_json["hotword"]
idioma = config_json["language"]
with open(
"config/sistema-intelige-1537185170980-b86ebc917520.json"
) as credenciais_google:
credenciais_google = credenciais_google.read()
##### funções principais #####
def monitora_audio():
microfone = sr.Recognizer()
with sr.Microphone() as source:
while True:
microfone.adjust_for_ambient_noise(source)
print("Estou aqui: ")
audio = microfone.listen(source)
try:
trigger = microfone.recognize_google_cloud(
audio, credentials_json=credenciais_google, language=idioma
)
trigger = trigger.lower()
if hotword in trigger:
print("Comando: ", trigger)
responde("feedback")
executa_comandos(trigger)
break
except sr.UnknownValueError:
print("Google not understand audio")
# responde('naoentende')
except sr.RequestError as e:
print(
"Could not request results from Google Cloud Speech service; {0}".format(
e
)
)
responde("errodeconecao")
return trigger
def responde(arquivo):
p = vlc.MediaPlayer(f"audios/{arquivo}.mp3")
p.play()
# call(["ffplay", "-nodisp", "-autoexit", f"audios/{arquivo}.mp3"])
# playsound('audios/'+arquivo+'.mp3') #windows aqui so passa o audio
def executa_comandos(trigger):
if "notícias" in trigger:
fbase.ultimas_noticias()
elif "hora" in trigger:
fbase.hora()
elif "data" in trigger:
fbase.data()
elif "data" in trigger and "hora" in trigger:
fbase.dataehora()
elif "toca" in trigger or "toque" in trigger:
album = trigger.strip(hotword)
fbase.playlist(album)
elif "abra" in trigger or "abrir" in trigger or "abre" in trigger:
nome = trigger.strip(hotword)
fbase.abre_pagina(nome)
elif "coronavírus" in trigger or "covid" in trigger:
nome = trigger.strip(hotword)
fbase.status_covid(nome)
elif "tempo" in trigger and "agora" in trigger:
fbase.previsao_tempo(tempo=True)
elif "temperatura" in trigger and "hoje" in trigger:
fbase.previsao_tempo(minimax=True)
elif "previsão" in trigger and "tempo" in trigger:
fbase.previsao_tempo(todos=True)
elif "liga a lâmpada" in trigger or "ativa a lâmpada" in trigger:
fbase.publica_mqtt("rele/", "2")
elif (
"desativa a lâmpada" in trigger
or "desliga a lâmpada" in trigger
or "apaga a lâmpada" in trigger
):
fbase.publica_mqtt("rele/", "3")
elif "temperatura" in trigger:
fbase.le_temperatura("sala1/temp/")
elif "umidade" in trigger:
fbase.le_umidade("sala1/umi/")
elif "parar" in trigger and "execução" in trigger:
print("parado Ana")
else:
menssagem = trigger.strip(hotword)
criaaudio.cria_audio(menssagem)
print("Comando inválido ", menssagem)
responde("comanin")
##### função inicio #####
def main():
responde("init")
while True:
try:
monitora_audio()
except:
print("Erro ao executar o codigo")
if __name__ == "__main__":
main()