我有“语音识别”问题

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

我正在尝试在视频上观看的一些代码,但是当我必须运行该程序时,它显示以下文本:“发生错误:模块'speech_recognition'没有属性'识别'”我使用了python

这是我的代码:

import openai

import pyttsx3
import speech_recognition as sr
print ("TEST")
import time 


# Initialize OpenAI API
openai.api_key = "myapikey"
# Initialize the text to speech engine 
engine=pyttsx3.init()


def transcribe_audio_to_test(filename):
    recogizer=sr.Recognizer()
    with sr.AudioFile(filename)as source:
        audio=recogizer.record(source) 
    try:
        return recogizer.recognize_google(audio)
    except:
        print("skipping unkown error")

def generate_response(prompt):
    response= openai.completion.create(
        engine="text-davinci-003",
        prompt=prompt,
        max_tokens=4000,
        n=1,
        stop=None,
        temperature=0.5,
    )
    return response ["Choices"][0]["text"]
def speak_text(text):
    engine.say(text)
    engine.runAndWait()

def main():
    while True:
        #Waith for user say "genius"
        print("Say 'Genius' to start recording your question")
        with sr.Microphone() as source:
            recognizer=sr.Recognizer()
            audio=recognizer.listen(source)
            try:
                transcription = recognizer.recognize_google(audio)
                if transcription.lower()=="genius":
                    #record audio
                    filename ="input.wav"
                    print("Say your question")
                    with sr.Microphone() as source:
                        recognizer=sr.recognize()
                        source.pause_threshold=1
                        audio=recognizer.listen(source,phrase_time_limit=None,timeout=None)
                        with open(filename,"wb")as f:
                            f.write(audio.get_wav_data())
                            
                            
                        
                        
                    #transcript audio to test 
                    text=transcribe_audio_to_test(filename)
                    if text:
                        print(f"yuo said {text}")
                        
                        #Generate the response
                        response = generate_response(text)
                        print(f"chat gpt 3 say {response}")
                            
                        #read resopnse using GPT3
                        speak_text(response)
            except Exception as e:
                
                print("An error ocurred : {}".format(e))
if __name__=="__main__":
    main()

我点击了在 Visual Studio 上运行,开始调试并选择了 Python,但是当我尝试说出我选择的单词时,它显示了“发生错误:模块‘speech_recognition’没有属性‘识别’”的错误

python speech-recognition voice-recognition
1个回答
0
投票

错误出现在这行代码中:

recognizer=sr.recognize()

应为:

recognizer=sr.Recognizer()

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