0

我对这段代码有疑问:

class LAB:
    def __int__(self):
        self.r = sr.Recognizer()
        self.audioFilePath = 'C:\\Users\\user\\Music\\WAV\\male.wav'

    def test(self):
        with sr.AudioFile(self.audioFilePath) as sourceFile:
            audio = self.r.record(sourceFile)
        try:
            print("sourceFile contains: " + self.r.recognize_google(audio))
        except sr.UnknownValueError:
            print("problem raised")

if __name__ == '__main__':
    Assistant = LAB()
    Assistant.test()

当我运行它时:

  File "D:/Assistant/myLab.py", line 20, in <module>
    Assistant.test()
  File "D:/Assistant/myLab.py", line 10, in test
    with sr.AudioFile(self.audioFilePath) as sourceFile:
AttributeError: 'LAB' object has no attribute 'audioFilePath'

解决办法是什么?我尝试在不使用 OOP 的情况下编写上述代码,它对我来说很好。

4

0 回答 0