-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrec.py
28 lines (24 loc) · 844 Bytes
/
rec.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
from os import path
import speech_recognition as sr
from gtts import gTTS
def rec():
r = sr.Recognizer()
with sr.Microphone() as source:
#r.adjust_for_ambient_noise(source)
print('Say something!')
audio = r.listen(source)
try:
sttTXT_org = r.recognize_google(audio, language = 'en-US')
print("Google Speech Recognition thinks you said: " + sttTXT_org)
return sttTXT_org
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
return 0
except sr.RequestError as e:
print('Could not request results from Google Speech Recognition service; {0}'.format(e))
return 0
def t2speech(txt):
tts = gTTS(text=txt,lang='en-US').save('{}.mp3'.format(txt))