-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
64 lines (48 loc) · 1.88 KB
/
app.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
from googletrans import Translator
from os import path
from gtts import gTTS
import speech_recognition as sr
def lines(path):
# Une fonction déstiner à lire dans un fichier
# puis retourner les résultats dans une variable de
# chaine de caractères
file = open(path, "r")
lines = file.readlines()
for line in lines:
line += line
file.close()
return line
# Obtenir l'audio à partir du micro
r = sr.Recognizer()
with sr.Microphone() as source:
print("Dites quelque chose... !")
audio = r.listen(source)
# Ecrire l'audio au format .raw
with open("voices/voice.raw", "wb") as f:
f.write(audio.get_raw_data())
# Ecrire l'audio au format .wav
# Un fichier binaire
with open("voices/voice.wav", "wb") as f:
f.write(audio.get_wav_data())
AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "voices/voice.wav")
rec = sr.Recognizer()
with sr.AudioFile(AUDIO_FILE) as source:
audio = rec.record(source) # lecture entière du fichier audio
# reconnaissance vocale utilisant Google Speech Recognition
try:
# utilisation d'une clé API par défaut pour le test
# si vous souhaitez utiliser une autre clé API, faites `rec.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# au lieu de `rec.recognize_google(audio)`
print("Google Speech Recognition penses que vous avez dit : " + rec.recognize_google(audio))
with open("texts/translate/essai.txt", "w") as f:
f.write(rec.recognize_google(audio))
except sr.UnknownValueError:
print("Google Speech Recognition n'a pas pu comprendre l'audio")
except sr.RequestError as e:
print("Requête échoué du service Google Speech Recognition : {0}".format(e))
translator = Translator()
line = lines('texts/translate/essai.txt')
lang = translator.detect(line)
print(lang.lang)
translate = translator.translate(line, src='en', dest='fr')
print(translate.text)