简体   繁体   English

通过 Sphinx 引擎进行语音识别会产生 AttributeError: 'NoneType' object has no attribute 'close'

[英]Speech recognition via Sphinx engine produces AttributeError: 'NoneType' object has no attribute 'close'

I am trying to transcribe a video using the code below, but I received the following error:我正在尝试使用以下代码转录视频,但收到以下错误:

ERROR : transcribe_video_to_text
    audio.close()

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

Could you please share your ideas to solve this problem, or suggest alternative libraries or AI that could perform the transcription?您能否分享您解决这个问题的想法,或者建议可以执行转录的替代库或 AI?

Code:代码:

import moviepy.editor as mp
import speech_recognition as sr

def transcribe_video_to_text(video_path):
    # Extract audio from the video
    video = mp.VideoFileClip(video_path)
    audio = video.audio.write_audiofile("temp_audio.wav")

    # Initialize the speech recognition object
    recognizer = sr.Recognizer()

    # Read the audio file
    with sr.AudioFile("temp_audio.wav") as source:
        audio_data = recognizer.record(source)

    # Perform speech recognition on the audio
    text = recognizer.recognize_sphinx(audio_data, keyword_entries=[('whisper', 1.0)])

    # Remove the temporary audio file
    video.close()
    audio.close()
    os.remove("temp_audio.wav")

    return text

# Provide the path to your video file
video_path = "Gods Architect Antoni Gaudis glorious vision.mp4"

# Transcribe the video to text
transcription = transcribe_video_to_text(video_path)

# Print the resulting transcription
print("Video transcription:")
print(transcription)

You're receiving the error because the call to write_audiofile() returns None , and you are trying to call close() on it.您收到错误是因为对write_audiofile()的调用返回None ,而您正试图对其调用close()

You don't need to store the returned value in audio in the first place.您不需要首先将返回值存储在audio中。 The updated code below will transcribe your video:下面更新的代码将转录您的视频:

import os

import moviepy.editor as mp
import speech_recognition as sr

def transcribe_video_to_text(video_path):
    # Extract audio from the video
    video = mp.VideoFileClip(video_path)
    video.audio.write_audiofile("temp_audio.wav")

    # Initialize the speech recognition object
    recognizer = sr.Recognizer()

    # Read the audio file
    with sr.AudioFile("temp_audio.wav") as source:
        audio_data = recognizer.record(source)

    # Perform speech recognition on the audio
    text = recognizer.recognize_sphinx(audio_data)

    video.close()
    # Remove the temporary audio file
    os.remove("temp_audio.wav")

    return text

# Provide the path to your video file
video_path = "Gods Architect Antoni Gaudis glorious vision.mp4"

# Transcribe the video to text
transcription = transcribe_video_to_text(video_path)

# Print the resulting transcription
print("Video transcription:")
print(transcription)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:'NoneType'对象没有属性'close' - AttributeError: 'NoneType' object has no attribute 'close' Python AttributeError:NoneType对象没有属性'close' - Python AttributeError: NoneType object has no attribute 'close' 语音识别:AttributeError:模块“ speech_recognition”没有属性“ Recognizer” - Speech Recognition: AttributeError: module 'speech_recognition' has no attribute 'Recognizer' AttributeError: 'NoneType' 对象没有属性 - AttributeError: 'NoneType' object has no attribute AttributeError:“ NoneType”对象没有属性“ a” - AttributeError: 'NoneType' object has no attribute 'a' AttributeError:'NoneType'对象没有属性'mention' - AttributeError: 'NoneType' object has no attribute 'mention' Python,AttributeError:“ NoneType”对象没有属性“ show” - Python, AttributeError: 'NoneType' object has no attribute 'show' AttributeError:'NoneType'对象没有属性'_root' - AttributeError: 'NoneType' object has no attribute '_root' AttributeError: 'NoneType' 对象没有属性 'setText' - AttributeError: 'NoneType' object has no attribute 'setText' Python:AttributeError:'NoneType'对象没有属性'findNext' - Python: AttributeError: 'NoneType' object has no attribute 'findNext'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM