Kivy语音识别不是将语音转换为文本

发布于 2025-01-18 13:08:56 字数 2494 浏览 3 评论 0原文

我的代码是录制语音,但由于某种原因,Google识别器或狮身人面像不会将语音转换为文本。如果我打印音频,则有声音。输出是“对不起,我没有得到”女巫默认是例外。您能查看我的代码,看看我在做什么错。在常规的python中,这种语音识别效果很好。 `

from kivy.app import App
from kivy.clock import Clock
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock, mainthread
import speech_recognition as sr
from kivy.lang import Builder
from plyer import stt
from plyer import tts

Builder.load_string("""
<Principal>:
    id: HomeScreen
    BoxLayout:
        orientation: 'vertical'
        Button:
            text:'speak'
            on_release: root.Btnclick()
        Label:
            id: lblMessage
            text: 'Hello' """)

class Principal(Screen):
    def Btnclick(self):
        self.ids.lblMessage.text = "Say something!"
        Clock.schedule_once(lambda d: self.GetAudio(), 0)
 
    def GetAudio(self):
        r = sr.Recognizer()
        r.energy_threshold =4000
        r.dynamic_energy_threshold = True
        
        with sr.Microphone() as source:
            r.adjust_for_ambient_noise(source,duration=5)
            #r.energy_threshold = 400  
            r.dynamic_energy_threshold = True 
            print("Say something!")  
            audio = r.listen(source)
        try:
            print("you Said: "+r.recognize_sphinx(audio, language = "en-US"))
            #tts.speak("you Said: "+r.recognize_sphinx(audio, language = "en-US"))
            self.ids.lblMessage.text = "you Said: "+r.recognize_sphinx(audio, language = "en-US")
        except:
            print("Sorry, I did not get that")
            #tts.speak("Sorry, I did not get that")
            self.ids.lblMessage.text = "Sorry, I did not get that"

class MainApp(App):
 
    def build(self):
        for i in sr.Microphone.list_microphone_names():
            print(i)
        sm = ScreenManager()
        self.sm = sm
        sm.add_widget(Principal(name='Principal'))
        return sm
 
    def on_pause(self):
        return False
                
if __name__ == '__main__':
    MainApp().run()

This is the output am getting

[INFO   ] [Window      ] auto add sdl2 input provider
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [Base        ] Start application main loop
[INFO   ] [GL          ] NPOT texture support is available
Say something!
<speech_recognition.AudioData object at 0x0000027124A64190>
Sorry, I did not get that

my code is recording voice, but for some reason google recognizer or sphinx not converting the voice to text. If i print the audio there is voice. the output is "Sorry, I did not get that" witch is the exception default. Can you look in to my code and see what I'm doing wrong. In regular Python this Voice recognition work fine. `

from kivy.app import App
from kivy.clock import Clock
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock, mainthread
import speech_recognition as sr
from kivy.lang import Builder
from plyer import stt
from plyer import tts

Builder.load_string("""
<Principal>:
    id: HomeScreen
    BoxLayout:
        orientation: 'vertical'
        Button:
            text:'speak'
            on_release: root.Btnclick()
        Label:
            id: lblMessage
            text: 'Hello' """)

class Principal(Screen):
    def Btnclick(self):
        self.ids.lblMessage.text = "Say something!"
        Clock.schedule_once(lambda d: self.GetAudio(), 0)
 
    def GetAudio(self):
        r = sr.Recognizer()
        r.energy_threshold =4000
        r.dynamic_energy_threshold = True
        
        with sr.Microphone() as source:
            r.adjust_for_ambient_noise(source,duration=5)
            #r.energy_threshold = 400  
            r.dynamic_energy_threshold = True 
            print("Say something!")  
            audio = r.listen(source)
        try:
            print("you Said: "+r.recognize_sphinx(audio, language = "en-US"))
            #tts.speak("you Said: "+r.recognize_sphinx(audio, language = "en-US"))
            self.ids.lblMessage.text = "you Said: "+r.recognize_sphinx(audio, language = "en-US")
        except:
            print("Sorry, I did not get that")
            #tts.speak("Sorry, I did not get that")
            self.ids.lblMessage.text = "Sorry, I did not get that"

class MainApp(App):
 
    def build(self):
        for i in sr.Microphone.list_microphone_names():
            print(i)
        sm = ScreenManager()
        self.sm = sm
        sm.add_widget(Principal(name='Principal'))
        return sm
 
    def on_pause(self):
        return False
                
if __name__ == '__main__':
    MainApp().run()

This is the output am getting

[INFO   ] [Window      ] auto add sdl2 input provider
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [Base        ] Start application main loop
[INFO   ] [GL          ] NPOT texture support is available
Say something!
<speech_recognition.AudioData object at 0x0000027124A64190>
Sorry, I did not get that

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文