2017-07-12 28 views
1

我忙着做语音识别代码,我有这样的:OSX - #include“portaudio.h”1生成错误。错误:命令“在/ usr/bin中/铛”与退出状态失败1

from pocketsphinx.pocketsphinx import * 
from sphinxbase.sphinxbase import * 

import os 
import pyaudio 
import wave 
import audioop 
from collections import deque 
import time 
import math 

class SpeechDetector: 
    def __init__(self): 
     self.CHUNK = 1024 
     self.FORMAT = pyaudio.paInt16 
     self.CHANNELS = 1 
     self.RATE = 16000 

     self.SILENCE_LIMIT = 1 
     self.PREV_AUDIO = 0.5 

     self.THRESHOLD = 4500 
     self.num_phrases = -1 

     MODELDIR = "../../tools/pocketsphinx/model" 
     DATADIR = "../../tools/pocketsphinx/test/data" 

     config = Decoder.default_config() 
     config.set_string('-hmm', os.path.join(MODELDIR, 'en-us/en-us')) 
     config.set_string('-lm', os.path.join(MODELDIR, 'en-us/en-us.lm.bin')) 
     config.set_string('-dict', os.path.join(MODELDIR, 'en-us/cmudict-en-us.dict')) 

     self.decoder = Decoder(config) 

    def setup_mic(self, num_samples=50): 
     print "Getting intensity values from mic." 
     p = pyaudio.PyAudio() 
     stream = p.open(format=self.FORMAT, 
         channels=self.CHANNELS, 
         rate=self.RATE, 
         input=True, 
         frames_per_buffer=self.CHUNK) 

     values = [math.sqrt(abs(audioop.avg(stream.read(self.CHUNK), 4))) 
      for x in range(num_samples)] 
      values = sorted(values, reverse=True) 
     r = sum(values[:int(num_samples * 0.2)])/int(num_samples * 0.2) 
     print " Finished " 
     print " Average audio intensity is ", r 
     stream.close() 
     p.terminate() 

     if r < 3000: 
      self.THRESHOLD = 3500 
     else: 
      self.THRESHOLD = r + 100 

    def save_speech(self, data, p): 

     filename = 'output_'+str(int(time.time())) 
     data = ''.join(data) 
     wf = wave.open(filename + '.wav', 'wb') 
     wf.setnchannels(1) 
     wf.setsampwidth(p.get_sample_size(pyaudio.paInt16)) 
     wf.setframerate(16000) 
     wf.writeframes(data) 
     wf.close() 
     return filename + '.wav' 

def decode_phrase(self, wav_file): 
    self.decoder.start_utt() 
    stream = open(wav_file, "rb") 
    while True: 
     buf = stream.read(1024) 
     if buf: 
      self.decoder.process_raw(buf, False, False) 
      self.decoder.end_utt() 

      words = [] 
      [words.append(seg.word) for seg in self.decoder.seg()] 
      return words 

def run(self): 
    self.setup_mic() 

p = pyaudio.PyAudio() 
stream = p.open(format=self.FORMAT, 
       channels=self.CHANNELS, 
       rate=self.RATE, 
       input=True, 
       frames_per_buffer=self.CHUNK) 
print "* Mic set up and listening. " 

audio2send = [] 
cur_data = '' 
rel = self.RATE/self.CHUNK 
slid_win = deque(maxlen=self.SILENCE_LIMIT * rel) 
prev_audio = deque(maxlen=self.PREV_AUDIO * rel) 
started = False 

while True: 
    cur_data = stream.read(self.CHUNK) 
    slid_win.append(math.sqrt(abs(audioop.avg(cur_data, 4)))) 

    if sum([x > self.THRESHOLD for x in slid_win]) > 0: 
     if started == False: 
      print "Starting recording of phrase" 
      started = True 
      audio2send.append(cur_data) 
     elif started: 
      print "Finished recording, decoding phrase" 
      filename = self.save_speech(list(prev_audio) + audio2send, p) 
      r = self.decode_phrase(filename) 
      print "DETECTED: ", r 
      os.remove(filename) 

stream.close() 
p.terminate() 

if __name__ == "__main__": 
    sd = SpeechDetector() 
    sd.run() 

但我每次运行它的时候说:导入错误:没有名为pyaudio的模块。

然后我去端和我做的点子安装pyaudio 但随后它表明这一点:

#include "portaudio.h" 

     ^
1 error generated. 
error: command '/usr/bin/clang' failed with exit status 1 

我使用的是MacBook和MacOS版本山脉从10.12.5 2011年我看着每堆溢出页面,但没有与我合作。请帮帮我!

+0

你在一个virtualenv中?如果没有,你用sudo试过了吗? – Arount

+0

我尝试了brew安装,但它说了类似于:bash:命令“brew”未知,我尝试了pip安装,但是显示了上面的注释 – Lucool

+0

好吧,因此,让'osx'标记添加到您的问题中,默认情况下很多用户认为关于linux。 – Arount

回答

0

安装pyaudio之前需要安装portaudio。用自制

安装portaudio从pyaudio网站

Apple Mac OS X Use Homebrew to install the prerequisite portaudio library, then install PyAudio using pip:

brew install portaudio 
pip install pyaudio 

Notes:

If not already installed, download Homebrew. pip will download the PyAudio source and build it for your version of Python. Homebrew and building PyAudio also require installing the Command Line Tools for Xcode (more information).

+0

,如果我确实导入了PyAudio,它会显示: AttributeError:找不到PyAudio;检查安装 – Lucool

+0

我通过PIP安装了Homebrew,但它没有安装PyAudio,我还安装了xcode – Lucool

+0

的命令行工具,您需要通过pip和portaudio通过自制软件安装pyaudio才能使其正常工作。运行'pip install pyaudio'并检查输出是什么。你也可以通过运行'pip show pyaudio'来找到pyaudio。如果您在虚拟环境中运行应用程序,请确保在虚拟环境中进行pip安装。 – Sanju

相关问题