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

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

Voice Recognition (converting voice to text)

问题

  1. # 写入音频
  2. with open("recorded.wav", "wb") as f:
  3. 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?

  1. import speech_recognition as sr
  2. def main():
  3. r = sr.Recognizer()
  4. with sr.Microphone() as source:
  5. r.adjust_for_ambient_noise(source)
  6. print("Please say something to start recording the lecture ")
  7. audio = r.listen(source)
  8. print("Recognizing Now .... ")
  9. # recognize speech using google
  10. try:
  11. print("You have said \n" + r.recognize_google(audio))
  12. print("Audio Recorded Successfully \n ")
  13. except Exception as e:
  14. print("Error : " + str(e))
  15. # write audio
  16. with open("recorded.wav", "wb") as f:
  17. f.write(audio.get_wav_data())
  18. if __name__ == "__main__":
  19. 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() 打开一个文本文件,类似于以下所示:

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

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

  1. transcript.write(r.recognize_google(audio))

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

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

  1. import speech_recognition as sr
  2. def main():
  3. transcript = open('transcript.txt', 'w')
  4. r = sr.Recognizer()
  5. with sr.Microphone() as source:
  6. r.adjust_for_ambient_noise(source)
  7. print("Please say something to start recording the lecture ")
  8. audio = r.listen(source)
  9. print("Recognizing Now .... ")
  10. try:
  11. print("You have said \n" + r.recognize_google(audio))
  12. transcript.write(r.recognize_google(audio))
  13. print("Audio Recorded Successfully \n ")
  14. except Exception as e:
  15. print("Error : " + str(e))
  16. with open("recorded.wav", "wb") as f:
  17. f.write(audio.get_wav_data())
  18. f.close()
  19. transcript.close()
  20. if __name__ == "__main__":
  21. main()
英文:

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

  1. 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

  1. 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

  1. import speech_recognition as sr
  2. def main():
  3. transcript = open('transcript.txt', 'w')
  4. r = sr.Recognizer()
  5. with sr.Microphone() as source:
  6. r.adjust_for_ambient_noise(source)
  7. print("Please say something to start recording the lecture ")
  8. audio = r.listen(source)
  9. print("Recognizing Now .... ")
  10. # recognize speech using google
  11. try:
  12. print("You have said \n" + r.recognize_google(audio))
  13. transcript.write(r.recognize_google(audio))
  14. print("Audio Recorded Successfully \n ")
  15. except Exception as e:
  16. print("Error : " + str(e))
  17. # write audio
  18. with open("recorded.wav", "wb") as f:
  19. f.write(audio.get_wav_data())
  20. f.close()
  21. transcript.close()
  22. if __name__ == "__main__":
  23. 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:

确定