AttributeError:找不到PyAudio;检查安装...无法使用语音识别

时间:2019-05-04 15:38:16

标签: python python-3.x visual-c++ pyaudio

我正在尝试制作基本的语音识别助手。当我运行代码时,它告诉我:

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 

我尝试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\
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()

9 个答案:

答案 0 :(得分:4)

如果您是ubuntu 18.04用户,请按照以下步骤操作

<uses-permission android:name="android.permission.INTERNET"/>

然后

CREATE TABLE public.my_table
(
  id bigint,
  condition boolean,
  fk_user_id bigint,
  created_date date
)
WITH (
  OIDS=FALSE
);
ALTER TABLE public.my_table
  OWNER TO postgres;

INSERT INTO public.my_table(
            id, condition, fk_user_id, created_date)
    VALUES 
    (137, FALSE, 23, '2019-08-28'),
    (107, FALSE, 23, '2019-05-13'),
    (83, TRUE, 23, '2019-04-28'),
    (78, FALSE, 23, '2019-04-07'),
    (67, TRUE, 23, '2019-03-18'),
    (32, FALSE, 23, '2019-01-19'),
    (181, FALSE, 57, '2019-11-04'),
    (158, TRUE, 57, '2019-09-27'),
    (146, FALSE, 57, '2019-09-16'),
    (125, FALSE, 57, '2019-07-24'),
    (378, TRUE, 71, '2020-02-16'),
    (228, TRUE, 71, '2019-12-13'),
    (179, FALSE, 71, '2019-10-06'),
    (130, FALSE, 71, '2019-08-19'),
    (114, TRUE, 71, '2019-06-29'),
    (593, FALSE, 92, '2020-03-02'),
    (320, FALSE, 92, '2020-01-30'),
    (187, FALSE, 92, '2019-11-23'),
    (180, TRUE, 92, '2019-10-17'),
    (124, FALSE, 92, '2019-08-05');

答案 1 :(得分:2)

在终端类型

  

pip安装pipwin

然后

  

pipwin安装pyaudio

答案 2 :(得分:1)

由于没有C ++构建工具来安装pyaudio,因此安装pyaudio会出错。

要安装Mircosoft visual C ++ 14.0,请考虑以下链接Searching the tree

然后, 安装pyaudio。

如果您在anaconda提示符下使用jupyter笔记本,则

conda install pyaudio

如果您使用的是使用cmd的jupyter笔记本,然后在jupyter单元上

import sys
!{sys.executable} -m pip install pyaudio

如果您正在cmd上运行python文件,

pip3 install pyaudio #for python3

答案 3 :(得分:0)

看来您缺少一些构建pyaudio所需的文件。

从您的错误日志中,

  

需要Microsoft Visual C ++ 14.0。使用“ Microsoft Visual C ++ Build>工具”获取它:https://visualstudio.microsoft.com/downloads/

您需要安装Microsoft Visual C++ Build Tools

答案 4 :(得分:0)

我还发现,由于安装困难,PyAudio的安装可能很痛苦,即使对于某些最终用户而言,也是一件麻烦事。原则上,speech_recognition.Recognizer.listen()没有理由不能从其他音频库(例如sounddevicesoundcardaudiomath)中获取输入,所有这些通常都更容易安装。幸运的是,尽管speech_recognition代码本身仅提供PyAudio实现,但在内部它只需要对Microphone的几个属性进行鸭式输入即可使其成功listen()。具体来说:

  • source必须是speech_recognition.AudioSource子类的实例
  • 源处于活动状态时,
  • source.stream必须不为None
  • source.CHUNK必须是每个块的(整数)样本数
  • source.SAMPLE_RATE必须是采样率
  • source.SAMPLE_WIDTH必须是每个样本的字节数
  • source.stream.read(numberOfSamples)必须返回原始的单通道音频数据

这是使用audiomath的鸭子式解决方案:

import time, numpy, audiomath, speech_recognition
class DuckTypedMicrophone( speech_recognition.AudioSource ): # parent class required purely to pass assertion in Recognizer.listen
    def __init__( self, device=None, chunkSeconds=1024/44100.0 ):
        self.recorder = None
        self.device = device
        self.chunkSeconds = chunkSeconds
    def __enter__( self ):
        self.recorder = audiomath.Recorder( audiomath.Sound( 5, nchan=1 ), loop=True, device=self.device )
        self.CHUNK = int( audiomath.SecondsToSamples( self.chunkSeconds, self.recorder.stream.fs ) )
        self.SAMPLE_RATE = int( self.recorder.stream.fs )
        self.SAMPLE_WIDTH = self.recorder.sound.bytes
        self.nSamplesRead = 0
        return self
    def __exit__( self, *blx ):
        self.recorder.Stop()
        self.recorder = None
    def read( self, nSamples ):
        targetHead = audiomath.SamplesToSeconds( self.nSamplesRead + nSamples, self.SAMPLE_RATE )
        while self.recorder.head < targetHead: time.sleep( 0.001 )
        snd = self.recorder.sound
        startSamples = self.nSamplesRead % snd.nSamples
        chunk = snd.y[ startSamples : startSamples + nSamples, : ]
        wrapped = nSamples - chunk.shape[ 0 ]
        chunk = numpy.concatenate( ( chunk, snd.y[ :wrapped, : ] ), axis=audiomath.ACROSS_SAMPLES )
        self.nSamplesRead += nSamples
        return snd.dat2str( chunk )
    @property
    def stream( self ): # property required purely to pass assertion in Recognizer.listen
        return self if self.recorder else None

if __name__ == '__main__':

    import speech_recognition as sr

    r = sr.Recognizer()
    with DuckTypedMicrophone() as source: # replaces the PyAudio-dependent class sr.Microphone()
        print('Say something to the %s...' % source.__class__.__name__)
        audio = r.listen(source)
    print('Got it.')
    print('Understood: "%s"' % r.recognize_google(audio))

    if 0: # plot and/or play back captured audio
        import audiomath as am
        s = audiomath.Sound(audio.get_wav_data(), fs=audio.sample_rate, nchan=1)
        s.Play()
        s.Plot()

答案 5 :(得分:0)

即使在安装 pipwin 之后我也遇到了问题,所以我发现在安装 PyAudio 之前执行下面的解决方案

!apt install libasound2-dev portaudio19-dev libportaudio2 libportaudiocpp0 ffmpeg

答案 6 :(得分:-1)

sudo apt-get install libportaudio-dev(首先尝试使用此命令) sudo apt-get install portaudio19-dev(代替使用此) 稍后安装pyaudio(python -m pip安装PyAudio)

答案 7 :(得分:-1)

对于表示下载C ++构建工具的错误,我也遇到了相同的错误。我下载了Microsoft Visual Studio运行时,但无法正常工作。然后,我下载了带有anaconda插件的pycharm社区版。我下载了anaconda,将其激活,然后使用conda的python.exe配置了conda解释器。然后输入以下内容: 康达安装PyAudio 而且它对我来说一切都很好。我建议这样做。

答案 8 :(得分:-2)

Python 2.7、3.4、3.5和3.6(32位和64位)都支持PyAudio。您可能必须安装上述任何python才能使PyAudio正常工作。

相关问题