我需要从麦克风录制音频并将其转换为文本。我已经使用从网上下载的几个音频剪辑尝试了此转换过程,效果很好。但是,当我尝试转换从麦克风录制的音频剪辑时,出现以下错误。
回溯(最近一次调用最后一次):
文件“C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\site-packages\speech_recognition__init__.py”,第 203 行,位于enterself.audio_reader = wave.open(self.filename_or_fileobject, "rb")
文件“C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\wave.py”,第 510 行,打开
返回 Wave_read(f)
文件“C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\wave.py”,第 164 行,位于initself.initfp(f)
文件“C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\wave.py”,第 144 行,位于 initfp 中
self._read_fmt_chunk(块)
文件“C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\wave.py”,第 269 行,位于 _read_fmt_chunk 中
引发错误('未知格式:%r'%(wFormatTag,))
波。错误:未知格式:3
我正在尝试的代码如下。
import speech_recognition as sr
import sounddevice as sd
from scipy.io.wavfile import write
# recording from the microphone
fs = 44100 # Sample rate
seconds = 3 # Duration of recording
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
sd.wait() # Wait until recording is finished
write('output.wav', fs, myrecording) # Save as WAV file
sound = "output.wav"
recognizer = sr.Recognizer()
with sr.AudioFile(sound) as source:
recognizer.adjust_for_ambient_noise(source)
print("Converting audio file to text...")
audio = recognizer.listen(source)
try:
text = recognizer.recognize_google(audio)
print("The converted text:" + text)
except Exception as e:
print(e)
我查看了已回答的类似问题,他们说我们需要将其转换为不同的 wav 格式。有人可以为我提供可用于此转换的代码或库吗?先感谢您。
您以浮点格式编写了文件:
soxi output.wav
Input File : 'output.wav'
Channels : 2
Sample Rate : 44100
Precision : 25-bit
Duration : 00:00:03.00 = 132300 samples = 225 CDDA sectors
File Size : 1.06M
Bit Rate : 2.82M
Sample Encoding: 32-bit Floating Point PCM
并且wave模块无法读取它。
要存储 int16 格式,请执行以下操作:
import numpy as np
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
sd.wait() # Wait until recording is finished
write('output.wav', fs, myrecording.astype(np.int16)) # Save as WAV file in 16-bit format
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)