您好我正在尝试使用 Google Web Speech API 进行语音识别,我想将音频文件转换为文本。我已经能够在我的树莓派 4 上编译此代码,但是当我在我的 Windows 计算机上编译它时,我得到一个找不到文件的错误。
这是错误:
File "C:/Users/pc/Desktop/py_scripts/omg.py", line 15, in <module>
sound = pydub.AudioSegment.from_file('sample.mp3')
File "C:\Users\pc\AppData\Local\Programs\Python\Python38\lib\site-packages\pydub\audio_segment.py", line 651, in from_file
file, close_file = _fd_or_path_or_tempfile(file, 'rb', tempfile=False)
File "C:\Users\pc\AppData\Local\Programs\Python\Python38\lib\site-packages\pydub\utils.py", line 60, in _fd_or_path_or_tempfile
fd = open(fd, mode=mode)
FileNotFoundError: [Errno 2] No such file or directory: 'sample.mp3'
这是我一直在使用的代码:
import ffmpy, urllib, pydub
sound = pydub.AudioSegment.from_file('sample.mp3')
sound.export('sample.wav', format="wav")
sample_audio = sr.AudioFile('sample.wav')
print("audio saved")
r = sr.Recognizer()
r.energy_threshold = 500
with sample_audio as source:
r.adjust_for_ambient_noise(source, 0.25)
audio = r.record(source)
key = r.recognize_google(audio)
print("key is : ", end="")
print(key)