【问题标题】:How can I check pronunciation of speech?如何检查语音的发音?
【发布时间】:2021-07-30 11:59:58
【问题描述】:

我正在做一个项目,程序将获取语音输入,然后检查其发音是否正确(我将它用于语言学习网站)。 我试图使用谷歌翻译来检查发音,但我认为它没有那个功能。有什么想法吗?

这是我的语音识别脚本:

import speech_recognition
import pyttsx3

recognizer = speech_recognition.Recognizer() 

while True:
    try:
        with speech_recognition.Microphone() as mic:
            recognizer.adjust_for_ambient_noise(mic, duration=0.2)
            audio = recognizer.listen(mic) 

            text = recognizer.recognize_google(audio)
            text = text.lower()
            print(text)

所以我再次需要一些可以检查单词发音的东西。

【问题讨论】:

  • 这不应该用“简单”的语音识别 API 来解决,因为他们的目标是相反的:将尽可能多的发音映射到相同的结果。

标签: python speech-recognition


【解决方案1】:
import pyttsx3

import speech_recognition as sr

engine = pyttsx3.init("sapi5")

voices = engine.getProperty("voices")

engine.setProperty("voice", voices[1].id)


def speak(audio):
    
    engine.say(audio)
    
    engine.runAndWait()


def takeCommand():
    
    listener = sr.Recognizer()
    
    with sr.Microphone() as source:
        
        print("Listening.......")
        
        listener.pause_threshold = 1
        
        listener.energy_threshold = 400  # This will listen to the louder voice only, so all you need to do is to speak loudly to give the command.
        
        audio = listener.listen(source, timeout=1, phrase_time_limit=10)  # It will give you ten seconds to give the argument otherwise it will run again
        
        try:
            
            print(" Recognising.....")
            
            query = listener.recognize_google(audio, language="en-in")  # Here you can choose the language of your choice
            
            print(f" User said --> {query} \n")
            
        except Exception as e:
            
            # print(e)
            
            print("Sorry I didn't Recognise. Please say it again.....")
            
            return "None"
        
        return query

if __name__ == '__main__':
    
    speak("Hello boss, How're you?")
    
    while True:
        
        takeCommand()

【讨论】:

  • 非常感谢你的帮助,但我不知道为什么我在第 6 行得到这个错误 (engine.setProperty("voice", voices[1].id)),这是错误:IndexError:列表索引超出范围
  • 没关系,我解决了我必须用 [0] 替换 [1] 的问题。再次感谢您的帮助。
  • 您应该描述解决方案。而且你应该正确格式化它,因为它不可读。
  • 对不起!但我会确保下次我很好地发布我的解决方案。我很高兴我的解决方案对您的项目有所帮助。
  • edit您的问题提供支持信息和解释。此外,没有理由在代码中的每一行之后放置一个空行;它使阅读变得困难,并且对于那些必须删除所有这些行的复制粘贴代码的人来说会很烦人。
猜你喜欢
  • 2010-10-28
  • 2017-04-14
  • 2012-01-12
  • 2012-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多