-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvoice2text.py
49 lines (42 loc) · 1.39 KB
/
voice2text.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
import pyaudio
import wave
import speech_recognition as sr
FORMAT = pyaudio.paInt16 #
CHANNELS = 1
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 30
def record_audio(filename="output.wav"):
audio = pyaudio.PyAudio()
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
print("Recording...")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("Finished recording.")
stream.stop_stream()
stream.close()
audio.terminate()
with wave.open(filename, 'wb') as wf:
wf.setnchannels(CHANNELS)
wf.setsampwidth(audio.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
def transcribe_audio(filename="output.wav"):
recognizer = sr.Recognizer()
with sr.AudioFile(filename) as source:
audio_data = recognizer.record(source)
try:
text = recognizer.recognize_google(audio_data)
print("Transcription: " + text)
except sr.UnknownValueError:
print("fail")
except sr.RequestError as e:
print("fail")
if __name__ == "__main__":
filename = "output.wav"
record_audio(filename)
transcribe_audio(filename)