属性错误:使用 AudioSegment get_file() 命令时引发“NoneType”对象没有属性“read”

问题描述 投票:0回答:1

我目前正在开展一个语音转文本项目。我尝试了以下视频中的代码 “https://www.youtube.com/watch?v=B5A1bMi4dJI”,并收到:

 Line 29, in <module>
    clip = AudioSegment().from_file(data)

  File "C:\Users\efeba\anaconda3\lib\site-packages\pydub\audio_segment.py", line 213, in __init__
    data = data if isinstance(data, (basestring, bytes)) else data.read()

AttributeError: 'NoneType' object has no attribute 'read'

这是我的整个代码:


import torch
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
import speech_recognition as sr
from pydub import AudioSegment
import io

r = sr.Recognizer()


tokenizer = Wav2Vec2Processor.from_pretrained("cahya/wav2vec2-base-turkish")
model = Wav2Vec2ForCTC.from_pretrained("cahya/wav2vec2-base-turkish")


with sr.Microphone(sample_rate=16000) as source: 
    print("You can start speaking now") 
    while True: 
        audio = r.listen(source) 
        data = io.BytesIO(audio.get_wav_data())
        
        clip = AudioSegment().from_file(data)
        x    = torch.FloatTensor(clip.get_array_of_samples())
        
        
        inputs = tokenizer(x, sampling_rate = 16000, return_tensors = 'pt', padding = 'longest' ).input_values
        logits = model(inputs).logits
        tokens = torch.argmax(logits, dim = -1)
        text   = tokenizer.batch_decode(tokens)
        
        print(str(text).lower())

刚开始说话就发生错误。我不明白是什么原因导致了这个错误,所以我无法解决。 任何帮助表示赞赏

python speech-recognition speech-to-text
1个回答
0
投票

请去掉行中的括号,

AudioSegment().from_file(数据)

AudioSegment 是一个方法,而不是一个函数, 谢谢

© www.soinside.com 2019 - 2024. All rights reserved.