声音识别(将声音转换为文本)

huangapple go评论76阅读模式
英文:

Voice Recognition (converting voice to text)

问题

# 写入音频
with open("recorded.wav", "wb") as f:
    f.write(audio.get_wav_data())
英文:

I have code to convert voice to written text, I want to save the written text after it's converted to files that can be accessed later, how do I do it in the following code?

import speech_recognition as sr


def main():

    r = sr.Recognizer()

    with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source)

        print("Please say something to start recording the lecture ")

        audio = r.listen(source)

        print("Recognizing Now .... ")


        # recognize speech using google

        try:
            print("You have said \n" + r.recognize_google(audio))
            print("Audio Recorded Successfully \n ")


        except Exception as e:
            print("Error :  " + str(e))




        # write audio
        with open("recorded.wav", "wb") as f:
            f.write(audio.get_wav_data())


if __name__ == "__main__":
    main()

I tried to create another python file and run it as .txt but it saves the code not recoreded one

答案1

得分: 1

在你的 def main(): 函数中,你需要使用 open() 打开一个文本文件,类似于以下所示:

transcript = open('transcript.txt', 'w')

接下来,在你使用 print("You have said \n" + r.recognize_google(audio)) 的地方,你需要将从 r.recognize_google(audio) 获取到的数据写入上面提到的 transcript.txt 文本文件,使用 transcript 文件描述符,如下所示:

transcript.write(r.recognize_google(audio))

此外,你需要确保使用 transcript.close() 来关闭已打开的文件描述符。

稍微修改了你的代码以展示如何执行这些操作:

import speech_recognition as sr

def main():
    transcript = open('transcript.txt', 'w')
    r = sr.Recognizer()

    with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source)
        print("Please say something to start recording the lecture ")

        audio = r.listen(source)
        print("Recognizing Now .... ")

        try:
            print("You have said \n" + r.recognize_google(audio))
            transcript.write(r.recognize_google(audio))
            print("Audio Recorded Successfully \n ")
        except Exception as e:
            print("Error :  " + str(e))

        with open("recorded.wav", "wb") as f:
            f.write(audio.get_wav_data())
            f.close()

    transcript.close()

if __name__ == "__main__":
    main()
英文:

In your def main(): function you need to open a text file using open(), something similar to as shown:

transcript = open('transcript.txt', 'w')

after that where you are using print("You have said \n" + r.recognize_google(audio))
You have to write the data coming from r.recognize_google(audio) to the above transcript.txt text file using transcript file descriptor

transcript.write(r.recognize_google(audio))

Also you need to make sure to close the open file descriptor using transcript.close()

Modified your code slightly to show how you can do it

import speech_recognition as sr


def main():

    transcript = open('transcript.txt', 'w')
    r = sr.Recognizer()

    with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source)

        print("Please say something to start recording the lecture ")

        audio = r.listen(source)

        print("Recognizing Now .... ")


        # recognize speech using google

        try:
            print("You have said \n" + r.recognize_google(audio))
            transcript.write(r.recognize_google(audio))
            print("Audio Recorded Successfully \n ")
        except Exception as e:
            print("Error :  " + str(e))

        # write audio
        with open("recorded.wav", "wb") as f:
            f.write(audio.get_wav_data())
            f.close()
    
    transcript.close()


if __name__ == "__main__":
    main()

huangapple
  • 本文由 发表于 2023年1月9日 16:35:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054781.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定