Mo *_*ham 11 python visual-c++ pyaudio python-3.x
我正在尝试制作一个基本的语音识别助手。当我运行代码时,它告诉我:
Traceback (most recent call last):
File "C:\Users\Mo.haytham\AppData\Local\Programs\Python\Python37\lib\site-packages\speech_recognition\__init__.py", line 108, in get_pyaudio
import pyaudio
ModuleNotFoundError: No module named 'pyaudio'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/Mo.haytham/.PyCharmCE2018.3/config/scratches/ALPHA_BASIC.py", line 22, in <module>
hear()
File "C:/Users/Mo.haytham/.PyCharmCE2018.3/config/scratches/ALPHA_BASIC.py", line 13, in hear
with sr.Microphone() as sourse:
File "C:\Users\Mo.haytham\AppData\Local\Programs\Python\Python37\lib\site-packages\speech_recognition\__init__.py", line 79, in __init__
self.pyaudio_module = self.get_pyaudio()
File "C:\Users\Mo.haytham\AppData\Local\Programs\Python\Python37\lib\site-packages\speech_recognition\__init__.py", line 110, in get_pyaudio
raise AttributeError("Could not find PyAudio; check installation")
AttributeError: Could not find PyAudio; check installation
Run Code Online (Sandbox Code Playgroud)
我尝试过,pip install pyaudio但随后出现此错误:
Running setup.py clean for pyaudio
Failed to build pyaudio
Installing collected packages: pyaudio
Running setup.py install for pyaudio ... error
ERROR: Complete output from command 'c:\users\mo.haytham\appdata\local\programs\python\python37\python.exe' -u -c 'import setuptools, tokenize;__file__='"'"'C:\\Users\\MO2D8C~1.HAY\\AppData\\Local\\Temp\\pip-install-o2
10x3zl\\pyaudio\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\MO2
D8C~1.HAY\AppData\Local\Temp\pip-record-hr7kket1\install-record.txt' --single-version-externally-managed --compile:
ERROR: running install
running build
running build_py
creating build
creating build\lib.win-amd64-3.7
copying src\pyaudio.py -> build\lib.win-amd64-3.7
running build_ext
building '_portaudio' extension
error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": https://visualstudio.microsoft.com/downloads/
----------------------------------------
ERROR: Command "'c:\users\mo.haytham\appdata\local\programs\python\python37\python.exe' -u -c 'import setuptools, tokenize;__file__='"'"'C:\\Users\\MO2D8C~1.HAY\\AppData\\Local\\Temp\\pip-install-o210x3zl\\pyaudio\\setup.p
y'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\MO2D8C~1.HAY\AppData\Local\
Temp\pip-record-hr7kket1\install-record.txt' --single-version-externally-managed --compile" failed with error code 1 in C:\Users\MO2D8C~1.HAY\AppData\Local\Temp\pip-install-o210x3zl\pyaudio\
Run Code Online (Sandbox Code Playgroud)
def hear():
import speech_recognition as sr
ear = sr.Recognizer()
with sr.Microphone() as sourse:
print("listening...")
audio = ear.listen(sourse)
try:
text = ear.recognize_google(audio)
print(text)
except:
print("i didn't get that...")
hear()
Run Code Online (Sandbox Code Playgroud)
小智 20
在终端类型
pip install pipwin
Run Code Online (Sandbox Code Playgroud)
然后
pipwin install pyaudio
Run Code Online (Sandbox Code Playgroud)
我还发现 PyAudio 安装可能会很痛苦,因为安装困难,甚至对某些最终用户来说也是个难题。原则上没有理由为什么speech_recognition.Recognizer.listen()不能从其他音频库,如采取其输入sounddevice或声卡或audiomath,所有这些都是通常更容易安装。幸运的是,虽然speech_recognition代码本身只提供了 PyAudio 实现,但在内部它只需要Microphone鸭子类型的几个属性才能使其listen()成功。具体来说:
source必须是speech_recognition.AudioSource子类的实例source.streamNone当源处于活动状态时必须是非source.CHUNK 必须是每个块的(整数)样本数source.SAMPLE_RATE 必须是采样率source.SAMPLE_WIDTH 必须是每个样本的字节数source.stream.read(numberOfSamples) 必须返回原始单声道音频数据这是使用audiomath的鸭式解决方案:
import audiomath; audiomath.RequireAudiomathVersion( '1.12.0' )
import speech_recognition # NB: python -m pip install SpeechRecognition
class DuckTypedMicrophone( speech_recognition.AudioSource ): # descent from AudioSource is required purely to pass an assertion in Recognizer.listen()
def __init__( self, device=None, chunkSeconds=1024/44100.0 ): # 1024 samples at 44100 Hz is about 23 ms
self.recorder = None
self.device = device
self.chunkSeconds = chunkSeconds
def __enter__( self ):
self.nSamplesRead = 0
self.recorder = audiomath.Recorder( audiomath.Sound( 5, nChannels=1 ), loop=True, device=self.device )
# Attributes required by Recognizer.listen():
self.CHUNK = audiomath.SecondsToSamples( self.chunkSeconds, self.recorder.fs, int )
self.SAMPLE_RATE = int( self.recorder.fs )
self.SAMPLE_WIDTH = self.recorder.sound.nbytes
return self
def __exit__( self, *blx ):
self.recorder.Stop()
self.recorder = None
def read( self, nSamples ):
sampleArray = self.recorder.ReadSamples( self.nSamplesRead, nSamples )
self.nSamplesRead += nSamples
return self.recorder.sound.dat2str( sampleArray )
@property
def stream( self ): # attribute must be present to pass an assertion in Recognizer.listen(), and its value must have a .read() method
return self if self.recorder else None
if __name__ == '__main__':
import speech_recognition as sr
r = sr.Recognizer()
with DuckTypedMicrophone() as source:
print('\nSay something to the %s...' % source.__class__.__name__)
audio = r.listen(source)
print('Got it.')
print('\nUnderstood: "%s"\n' % r.recognize_google(audio))
if 0: # plot and/or play back captured audio
s = audiomath.Sound(audio.get_wav_data(), fs=audio.sample_rate, nChannels=1)
s.Play()
s.Plot()
Run Code Online (Sandbox Code Playgroud)
如果您是 ubuntu 18.04 用户,请按照以下步骤操作
sudo apt-get install portaudio19-dev python-pyaudio
Run Code Online (Sandbox Code Playgroud)
然后
pip install PyAudio
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33784 次 |
| 最近记录: |