Ошибка при преобразовании аудио в текст в Python - PullRequest
0 голосов
/ 17 октября 2018

Я пытаюсь преобразовать аудиофайл в текст.Аудио сохраняется как 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

Может кто-нибудь помочь, пожалуйстаменя об этом.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...