Python自我错误

时间:2015-04-26 12:23:32

标签: python function python-2.7 self

我正在研究Python程序,并且我正在使用jasper项目中包含" self"的函数。该功能应该识别何时说出一个单词。这是我的计划:

import logging
import tempfile
import wave
import audioop
import pyaudio
import speech_recognition as sr

def stt(file):
    r = sr.Recognizer()
    with sr.WavFile(file) as source:              # use "test.wav" as the audio source
        audio = r.record(source)                        # extract audio data from the file
        return r.recognize(audio)   # recognize speech using Google Speech Recognition


def passiveListen(self, PERSONA):
    """
    Listens for PERSONA in everyday sound. Times out after LISTEN_TIME, so
    needs to be restarted.
        """

    THRESHOLD_MULTIPLIER = 1.8
    RATE = 16000
    CHUNK = 1024

    # number of seconds to allow to establish threshold
    THRESHOLD_TIME = 1

    # number of seconds to listen before forcing restart
    LISTEN_TIME = 10

    # prepare recording stream
    stream = self._audio.open(format=pyaudio.paInt16,
                              channels=1,
                              rate=RATE,
                              input=True,
                              frames_per_buffer=CHUNK)

    # stores the audio data
    frames = []

    # stores the lastN score values
    lastN = [i for i in range(30)]

    # calculate the long run average, and thereby the proper threshold
    for i in range(0, RATE / CHUNK * THRESHOLD_TIME):

        data = stream.read(CHUNK)
        frames.append(data)

        # save this data point as a score
        lastN.pop(0)
        lastN.append(self.getScore(data))
        average = sum(lastN) / len(lastN)

    # this will be the benchmark to cause a disturbance over!
    THRESHOLD = average * THRESHOLD_MULTIPLIER

    # save some memory for sound data
    frames = []

    # flag raised when sound disturbance detected
    didDetect = False

    # start passively listening for disturbance above threshold
    for i in range(0, RATE / CHUNK * LISTEN_TIME):

        data = stream.read(CHUNK)
        frames.append(data)
        score = self.getScore(data)

        if score > THRESHOLD:
            didDetect = True
            break

    # no use continuing if no flag raised
    if not didDetect:
        print "No disturbance detected"
        stream.stop_stream()
        stream.close()
        return (None, None)

    # cutoff any recording before this disturbance was detected
    frames = frames[-20:]

    # otherwise, let's keep recording for few seconds and save the file
    DELAY_MULTIPLIER = 1
    for i in range(0, RATE / CHUNK * DELAY_MULTIPLIER):

        data = stream.read(CHUNK)
        frames.append(data)

    # save the audio data
    stream.stop_stream()
    stream.close()

    with tempfile.NamedTemporaryFile(mode='w+b') as f:
        wav_fp = wave.open(f, 'wb')
        wav_fp.setnchannels(1)
        wav_fp.setsampwidth(pyaudio.get_sample_size(pyaudio.paInt16))
        wav_fp.setframerate(RATE)
        wav_fp.writeframes(''.join(frames))
        wav_fp.close()
        f.seek(0)
        # check if PERSONA was said
        transcribed = stt(f)

    if any(PERSONA in phrase for phrase in transcribed):
        return (THRESHOLD, PERSONA)

    return (False, transcribed)

print passiveListen(self.persona)

现在问题是我从python开始,我不知道如何使用该功能。实际上我收到了这个错误:Traceback (most recent call last): File "Prueba.py", line 112, in <module> print passiveListen(self.persona) NameError: name 'self' is not defined 我也试过print passiveListen(self.persona),但无论如何它都没有用。我一直在查看github项目,看看这个函数是如何使用的,但是我无法获得任何信息(https://github.com/jasperproject/jasper-client/)。 请注意&#34; persona&#34;应该是名字或单词,例如:Alexa。 有人能帮帮我吗?非常感谢

1 个答案:

答案 0 :(得分:2)

听取错误。您尚未将self定义为变量。该错误源自您的最后一行,您在该函数外使用self