-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeech_recognizer.py
144 lines (111 loc) · 5.31 KB
/
speech_recognizer.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
import tkinter as tk
from threading import Thread
import time
from typing import Optional
import speech_recognition as sr
from ivy.ivy import IvyServer
class SpeechRecognizerAgent(IvyServer):
def __init__(self, agent_name: str):
IvyServer.__init__(self, 'SpeechRecognizerAgent')
self.name = agent_name
self.start('127.255.255.255:2010') # connexion de l'agent
self.bind_msg(self.send_color, '^color needed(.*)')
self.current_speech = ""
self.previous_speech = ""
self.draw_mode = "" # dessiner ou déplacer
self.form = ""
self.color = ""
self.here = ""
def send_color (self, agent, *args):
print("Souhaitez-vous préciser la couleur de l'objet ? Vous avez 20 secondes ")
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Veuillez dire quelque chose :")
recognizer.adjust_for_ambient_noise(source)
try:
audio_data = recognizer.listen(
source, timeout=20, phrase_time_limit=5)
self.current_speech = recognizer.recognize_google(audio_data, language='fr-FR')
print(f"Vous avez dit : {self.current_speech}")
print("Reponse :", "color answer" + self.current_speech)
self.send_msg("colorAnswer"+ self.current_speech)
except sr.WaitTimeoutError:
print("Délai d'attente écoulé : Aucun discours détecté. La couleur sera définie aléatoirement.")
self.send_msg("colorAnsweraleat")
self.erase_current_speech()
def erase_current_speech(self):
self.previous_speech = self.current_speech
self.current_speech = ""
self.draw_mode = ""
self.form = ""
self.color = ""
self.here = ""
def string_preprocessing(self, init_string: str):
formes_possibles = ["cercle", "triangle", "rectangle", "losange"]
couleurs = ["vert", "rouge", "jeune", "bleu", "maron", "orange", "violet"]
# Mode de fonctionnement ( Dessin, déplacement, supression, modification)
if any(mot in init_string for mot in ["créer", "crée", "dessiner", "dessiné", "dessinez", "dessine"]):
self.draw_mode = "dessiner"
elif any(mot in init_string for mot in ["déplacer", "déplace", "déplacé", "déplacement"]):
self.draw_mode = "deplacer "
elif any(mot in init_string for mot in ["supprimer", "supprimé", "suppression", "supprime"]):
self.draw_mode = "supprimer"
elif any(mot in init_string for mot in ["modofier", "modifié", "modification", "modifie"]):
self.draw_mode = "modifier"
else:
self.draw_mode = "dessiner"
print("Mode choisi : ", self.draw_mode) # debugg
# Couleur à envoyer
if any(couleur in init_string for couleur in couleurs):
self.color = [couleur for couleur in couleurs if couleur in init_string][0] + " "
print("couleur détectée : ", self.color) # debugg
else :
self.color = ""
# Forme choisie
if any(forme in init_string for forme in formes_possibles):
self.form = [forme for forme in formes_possibles if forme in init_string][0] + " "
print("forme détectée : ", self.form) # debugg
else:
self.form = "ceci"
# Position
if "ici" in init_string:
self.here = "ici"
else :
self.here = ""
def speech_recognizer(self):
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Veuillez dire quelque chose :")
recognizer.adjust_for_ambient_noise(source)
try:
audio_data = recognizer.listen(
source, timeout=20, phrase_time_limit=5)
self.current_speech = recognizer.recognize_google(audio_data, language='fr-FR')
print(f"Vous avez dit : {self.current_speech}")
self.string_preprocessing(self.current_speech)
self.send_msg("message : " + self.draw_mode + self.form + self.color + self.here)
except sr.WaitTimeoutError:
print("Délai d'attente écoulé : Aucun discours détecté.")
except sr.UnknownValueError:
print("Désolé, je n'ai pas compris l'audio.")
except sr.RequestError as e:
print(f"Impossible de demander des résultats ; {e}")
self.erase_current_speech()
class SpeechRecognizerApp(tk.Tk):
def __init__(self, agent: SpeechRecognizerAgent, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title("Speech Recognizer App")
self.agent = agent
self.button = tk.Button(self, text="Lancer la reconnaissance vocale", command=self.start_recognition)
self.button.pack(pady=20)
def start_recognition(self):
self.button.config(state="disabled")
recognition_thread = Thread(target=self.run_recognition)
recognition_thread.start()
def run_recognition(self):
self.agent.speech_recognizer()
self.button.config(state="active")
if __name__ == "__main__":
agent = SpeechRecognizerAgent('SpeechRecognizerAgent')
app = SpeechRecognizerApp(agent)
app.mainloop()