-
Notifications
You must be signed in to change notification settings - Fork 0
/
speech2txt.py
42 lines (29 loc) · 1.08 KB
/
speech2txt.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
# -*- coding: utf-8 -*-
import speech_recognition as sr
def speech2txt(audioPath):
# initialize the recognizer
r = sr.Recognizer()
try:
# open the file
with sr.AudioFile(audioPath) as source:
# listen for the data (load audio to memory)
audio_data = r.record(source)
# recognize (convert from speech to text)
text = r.recognize_google(audio_data, language='ar')
print(text)
print("Text recognized:", text)
except sr.UnknownValueError:
print("Speech could not be recognized")
text = ""
return text
def save_file(path, txt):
with open(path, 'w') as file:
file.write(txt)
user_audio_path = '/home/nadasamir/Desktop/cognitive/audio/audio.wav'
activation_txt_path = '/home/nadasamir/Desktop/cognitive/audio/activation.txt'
english_txt = speech2txt(user_audio_path)
# if english_txt == "" :
# english_txt = "Speech could not be recognized"
if english_txt == '':
english_txt = "لم أسمعُكَ جيداً"
save_file(activation_txt_path, english_txt)