Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 创建并使用WAV文件作为Python对象_Python 3.x_Raspberry Pi_Speech Recognition_Wav_Wave - Fatal编程技术网

Python 3.x 创建并使用WAV文件作为Python对象

Python 3.x 创建并使用WAV文件作为Python对象,python-3.x,raspberry-pi,speech-recognition,wav,wave,Python 3.x,Raspberry Pi,Speech Recognition,Wav,Wave,我正在用Python创建一个个人助理。我用雪人录音,效果很好。Snowboy有一个saveMessage()方法,用于创建wav文件并将其写入磁盘。该wav文件稍后被读取,并通过语音识别用作音频文件对象。我发现程序必须将wav文件写入和读取到磁盘,这是非常低效的。我更希望wav文件作为对象传递,而不保存到磁盘 下面是我想重新编写的snowboy saveMessage()模块 def saveMessage(self): """ Save the message stored i

我正在用Python创建一个个人助理。我用雪人录音,效果很好。Snowboy有一个saveMessage()方法,用于创建wav文件并将其写入磁盘。该wav文件稍后被读取,并通过语音识别用作音频文件对象。我发现程序必须将wav文件写入和读取到磁盘,这是非常低效的。我更希望wav文件作为对象传递,而不保存到磁盘

下面是我想重新编写的snowboy saveMessage()模块

def saveMessage(self):
    """
    Save the message stored in self.recordedData to a timestamped file.
    """
    filename = 'output' + str(int(time.time())) + '.wav'
    data = b''.join(self.recordedData)

    #use wave to save data
    wf = wave.open(filename, 'wb')
    wf.setnchannels(1)
    wf.setsampwidth(self.audio.get_sample_size(
        self.audio.get_format_from_width(
            self.detector.BitsPerSample() / 8)))
    wf.setframerate(self.detector.SampleRate())
    wf.writeframes(data)
    wf.close()
    logger.debug("finished saving: " + filename)
    return filename #INSTEAD OF RETURNING filename I WANT THIS TO RETURN THE wav file object
请注意,AudioFile类要求必须将wave文件或“类似文件”的对象的路径传递给它。我不确定“类文件”对象是什么,因此我将为wav文件参数提供AudioFile断言语句:

assert isinstance(filename_or_fileobject, (type(""), type(u""))) or hasattr(filename_or_fileobject, "read"), "Given audio file must be a filename string or a file-like object"
我曾尝试使用BytesIO的一个实例来保存wav数据,BytesIO显然不是一个类似文件的对象。以下是我所尝试的:

def saveMessage(self):
    filename = 'output' + str(int(time.time())) + '.wav'
    data = b''.join(self.recordedData)

    #use wave to save data
    with io.BytesIO() as wav_file:
        wav_writer = wave.open(wav_file, "wb")
        try:
            wav_writer.setnchannels(1)
            wav_writer.setsampwidth(self.audio.get_sample_size(
                self.audio.get_format_from_width(
                    self.detector.BitsPerSample() / 8)))
            wav_writer.setframerate(self.detector.SampleRate())
            wav_writer.writeframes(data)
            wav_data = wav_file.getvalue()
        finally:
            wav_writer.close()
            logger.debug("finished saving: " + filename)
    return wav_data
我得到的错误是:
AssertionError:给定的音频文件必须是文件名字符串或类似文件的对象

我在运行Raspbian Buster Lite内核版本4.19.36的Raspberry PI 3B+上使用python 3.7

如果我能提供任何其他信息或澄清任何事情,请询问


非常感谢

像这样的方法应该会奏效:

from speech_recognition import AudioData

def saveMessage(self):
    filename = 'output' + str(int(time.time())) + '.wav'
    data = b''.join(self.recordedData)
    ad = AudioData(data, 16000, 2)
    result = recognizer.recognize_google(ad)

请注意,speech_recognition.listen可以在内部调用snowboy,因此您可能不必使用外部snowboy,只需使用带参数的listen即可。

这非常有效!对于任何正在做类似于我的项目的人来说,我只是简单地修改了snowboy的saveMessage方法以返回数据,然后使用数据创建了一个AudioData对象(就像Nikolay所做的那样)。然后,我将音频数据对象传递到语音识别器的recognizer\u google函数中,以获得我的输出。