forked from tuomaskivioja/3-automation-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoice_to_text.py
43 lines (36 loc) · 1.31 KB
/
voice_to_text.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
import speech_recognition as sr
import sounddevice as sd
import numpy as np
import scipy.io.wavfile as wav
test_filenmae = ""
FILENAME_FROM_MIC = "RECORDING.WAV"
VOICE_TEXT_FILENAME = "VOICE_AS_TEXT.txt"
# initialize the recognizer
r = sr.Recognizer()
def recognize_from_file(filename):
# open the file
with sr.AudioFile(filename) 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)
return text
def recognize_from_microphone(file_to_write):
SAMPLE_RATE=44100
duration = 5 # seconds
audio_recording = sd.rec(duration * SAMPLE_RATE, samplerate=SAMPLE_RATE, channels=1, dtype='int32')
print("Recording Audio")
sd.wait()
print("Audio recording complete , Play Audio")
sd.play(audio_recording, SAMPLE_RATE)
sd.wait()
print("Play Audio Complete")
wav.write(file_to_write, SAMPLE_RATE, audio_recording)
def save_text_to_file(text, filename):
with open(filename, 'w') as f:
f.write(text)
if __name__ == "__main__":
#print(recognize_from_file(test_filenmae))
recognize_from_microphone(FILENAME_FROM_MIC)
text_from_voice = recognize_from_file(FILENAME_FROM_MIC)
save_text_to_file(text_from_voice, VOICE_TEXT_FILENAME)