我正在为盲人写一个Python程序,帮助他们写和读邮件。
他们可以用声控方式写一封邮件,并将尚未阅读的邮件以语音的方式收听(gTTS)。
如果用户选择写电子邮件,他们必须逐字逐句地拼出收件人的电子邮件地址,而不是单词或短语,以避免任何错误。
例如,对于 "[email protected]",我希望程序一次只听一个字母字符,也就是说。"x","y","z","@","g","m","a","i","l",".","c","o","m",这样它就可以把它们加在一起来改造电子邮件地址。
当我试着发音一个字母时,它能识别一个单词。
我怎样才能让它识别一个字母而不是一个单词?
下面是代码。
import audioop
import pyaudio
import wave
from gtts import gTTS
import speech_recognition as sr
import os
import email, smtplib, ssl
r = sr.Recognizer()
r.energy_threshold=4000
def say(text):
mytext = text
language = 'en'
myobj = gTTS(text=mytext, lang=language, slow=False)
myobj.save("text_to_audio.mp3")
os.system("afplay 'text_to_audio.mp3'")
def listen_to_user():
with sr.Microphone() as source:
audio2=r.listen(source, timeout=0)
return(str(r.recognize_google(audio2,language ="en-US",
show_all=False)))
say("you chose to write an email")
say("What's the receiver's email address?")
receiver_email_address=listen_to_user()
print(receiver_email_address)
say(receiver_email_address)
say("is this correct?")
answer=listen_to_user()
不幸的是,这并不能如你所愿。用常规的google语音转文本,你很有可能做到像'H''I''M''O''M'这样的单字母,但你需要一个机器学习算法,可以通过说 "百分号 "或 "在符号 "来检测像@或%这样的特殊字符。如果你多做一些研究,我相信你能做到。