语音识别输出(Python)

2024-04-18 05:00:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个有语音识别模块的项目,由于我的语言不同,我想保存在文本文件中,这样我就可以阅读并有正确的输出;但是当我试图用sys模块保存文件时,它出现了一些错误。如果你能帮我解决这个问题,我将不胜感激 这是我的代码:

import speech_recognition as sr 
import sys

r = sr.Recognizer()
print('How can I help you?')
def my_first_sr():
    while True:
        try:
            with sr.Microphone() as mic:
                r.adjust_for_ambient_noise(mic)
                audio = r.listen(mic)
                text = r.recognize_google(audio, language = 'fa-IR')
            print(text)
        except sr.UnknownValueError:
            print('I didn`t understand!')

        except sr.RequestError:
            print('Sorry my service is down')


my_first_sr()
output = open('Speech Recognition.txt', 'w')
sys.stdout = output
print(text)
output.close()

Tags: 模块项目textimportoutputmyassys
1条回答
网友
1楼 · 发布于 2024-04-18 05:00:53

您必须将文本写入文件输出:

import speech_recognition as sr 

r = sr.Recognizer()
print('How can I help you?')
def my_first_sr():
    while True:
        try:
            with sr.Microphone() as mic:
                r.adjust_for_ambient_noise(mic)
                audio = r.listen(mic)
                text = r.recognize_google(audio, language = 'fa-IR')

            print(text)

            output = open('Speech Recognition.txt', 'w')
            output.write(text)#Write the text to the file
            output.close()

        except sr.UnknownValueError:
            print('I didn`t understand!')

        except sr.RequestError:
            print('Sorry my service is down')


my_first_sr()

相关问题 更多 >