在Python中将音频转换为文本时出错

时间:2018-10-17 15:00:49

标签: python nlp speech-recognition speech-to-text

我正在尝试将音频文件转换为text。音频在我的桌面中另存为Wel.wav

下面是代码:

import speech_recognition as sr

r = sr.Recognizer()

audio = 'Wel.wav'

with sr.AudioFile(audio) as source:
audio = r.record(source)
print ('Done!')

try:
text = r.recognize_google(audio)
print (text)

except Exception as e:
print (e)

我收到以下错误:

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-27-432c6c552a8c> in <module>()
  5 audio = 'Wel.wav'
  6 
 ----> 7 with sr.AudioFile(audio) as source:
  8     audio = r.record(source)
  9     print ('Done!')

 C:\Users\user\Anaconda3\lib\site-packages\speech_recognition\__init__.py in 
 __enter__(self)
201         try:
202             # attempt to read the file as WAV
--> 203             self.audio_reader = 
wave.open(self.filename_or_fileobject, "rb")
204             self.little_endian = True  # RIFF WAV is a little-endian 
format (most ``audioop`` operations assume that the frames are stored in 
little-endian form)
205         except (wave.Error, EOFError):

C:\Users\user\Anaconda3\lib\wave.py in open(f, mode)
497             mode = 'rb'
498     if mode in ('r', 'rb'):
--> 499         return Wave_read(f)
500     elif mode in ('w', 'wb'):
501         return Wave_write(f)

C:\Users\user\Anaconda3\lib\wave.py in __init__(self, f)
157         self._i_opened_the_file = None
158         if isinstance(f, str):
 --> 159             f = builtins.open(f, 'rb')
160             self._i_opened_the_file = f
161         # else, assume it is an open file object already

FileNotFoundError: [Errno 2] No such file or directory: 'Wel.wav'

现在,当我尝试为其提供目录位置时,

我将音频命令更改为

audio = 'C:\\Users\\user\\Desktop\\Wel.wav'

它抛出以下错误:

Error                                     Traceback (most recent call last)
C:\Users\user\Anaconda3\lib\site-packages\speech_recognition\__init__.py in 
__enter__(self)
202             # attempt to read the file as WAV
--> 203             self.audio_reader = 
wave.open(self.filename_or_fileobject, "rb")
204             self.little_endian = True  # RIFF WAV is a little-endian 
format (most ``audioop`` operations assume that the frames are stored in 
little-endian form)

C:\Users\user\Anaconda3\lib\wave.py in open(f, mode)
498     if mode in ('r', 'rb'):
 --> 499         return Wave_read(f)
500     elif mode in ('w', 'wb'):

C:\Users\user\Anaconda3\lib\wave.py in __init__(self, f)
162         try:
--> 163             self.initfp(f)
164         except:

C:\Users\user\Anaconda3\lib\wave.py in initfp(self, file)
129         if self._file.getname() != b'RIFF':
--> 130             raise Error('file does not start with RIFF id')
131         if self._file.read(4) != b'WAVE':

Error: file does not start with RIFF id

During handling of the above exception, another exception occurred:

Error                                     Traceback (most recent call last)
C:\Users\user\Anaconda3\lib\site-packages\speech_recognition\__init__.py in 
   __enter__(self)
207                 # attempt to read the file as AIFF
--> 208                 self.audio_reader = 
aifc.open(self.filename_or_fileobject, "rb")
209                 self.little_endian = False  # AIFF is a big-endian 
format

 C:\Users\user\Anaconda3\lib\aifc.py in open(f, mode)
889     if mode in ('r', 'rb'):
--> 890         return Aifc_read(f)
891     elif mode in ('w', 'wb'):

C:\Users\user\Anaconda3\lib\aifc.py in __init__(self, f)
339         # else, assume it is an open file object already
--> 340         self.initfp(f)
341 

C:\Users\user\Anaconda3\lib\aifc.py in initfp(self, file)
304         if chunk.getname() != b'FORM':
--> 305             raise Error('file does not start with FORM id')
306         formdata = chunk.read(4)

Error: file does not start with FORM id

During handling of the above exception, another exception occurred:

EOFError                                  Traceback (most recent call last)
C:\Users\user\Anaconda3\lib\site-packages\speech_recognition\__init__.py in 
__enter__(self)
233                 try:
--> 234                     self.audio_reader = aifc.open(aiff_file, "rb")
235                 except (aifc.Error, EOFError):

C:\Users\user\Anaconda3\lib\aifc.py in open(f, mode)
889     if mode in ('r', 'rb'):
 --> 890         return Aifc_read(f)
891     elif mode in ('w', 'wb'):

C:\Users\user\Anaconda3\lib\aifc.py in __init__(self, f)
339         # else, assume it is an open file object already
 --> 340         self.initfp(f)
341 

C:\Users\user\Anaconda3\lib\aifc.py in initfp(self, file)
302         self._file = file
 --> 303         chunk = Chunk(file)
304         if chunk.getname() != b'FORM':

 C:\Users\user\Anaconda3\lib\chunk.py in __init__(self, file, align, 
 bigendian, inclheader)
 62         if len(self.chunkname) < 4:
 ---> 63             raise EOFError
 64         try:

 EOFError: 

 During handling of the above exception, another exception occurred:

 ValueError                                Traceback (most recent call last)
 <ipython-input-29-5bee547ee15e> in <module>()
  5 audio = 'C:\\Users\\user\\Desktop\\Wel.wav'
  6 
   ----> 7 with sr.AudioFile(audio) as source:
  8     audio = r.record(source)
  9     print ('Done!')

  C:\Users\user\Anaconda3\lib\site-packages\speech_recognition\__init__.py 
  in __enter__(self)
234                     self.audio_reader = aifc.open(aiff_file, "rb")
235                 except (aifc.Error, EOFError):
 --> 236                     raise ValueError("Audio file could not be read 
as PCM WAV, AIFF/AIFF-C, or Native FLAC; check if file is corrupted or in 
another format")
237                 self.little_endian = False  # AIFF is a big-endian 
format
238         assert 1 <= self.audio_reader.getnchannels() <= 2, "Audio must 
be mono or stereo"

ValueError: Audio file could not be read as PCM WAV, AIFF/AIFF-C, or Native 
FLAC; check if file is corrupted or in another format

有人可以帮助我吗?

0 个答案:

没有答案