如何将网站上的声音转换为文本?当我单击网站中的按钮时会播放声音,但我的问题是如何在不使用麦克风的情况下将其转换为文本,仅使用网站和 python。
import speech_recognition as sr
r = sr.Recognizer()
with sr.AudioFile('my.wav') as source:
audio_text = r.listen(source)
try:
text = r.recognize_google(audio_text)
print('Converting audio transcripts into text ...')
print(text)
except:
print('Sorry.. run again...')
这是我的代码,但我没有 wav 文件,只有来自我尝试转换的网站的声音。
我想做的例子
当我单击网站中的按钮时,它会播放“hello”,并且 python 将从网站获取声音并打印出来。
首先尝试下载文件,我不知道您的音频文件的位置或格式,所以这是一个猜测:
编辑:向真实的音频文件添加一个网址并且它可以工作,但它会因音频质量较差而失败
import requests
import speech_recognition as sr
def download(url, path):
response = requests.get(url) # get the response of the url
with open(path, 'wb') as file: # create the file
file.write(response.content) # write response contents to the file
def transcribe(path):
r = sr.Recognizer()
with sr.AudioFile(path) as source:
audio_text = r.record(source)
text = r.recognize_google(audio_text)
print('Converting audio transcripts into text ...')
return text
audio_url = 'https://google.github.io/tacotron/publications/parrotron/audio/norm_vctk/03_norm_input.wav'
audio_path = './speech.wav'
download(audio_url, audio_path)
audio_text = transcribe(audio_path)
print(audio_text)
Output
Converting audio transcripts into text ...
this is a huge confidence boost
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)