为什么在这种情况下使用OOP使用语音识别?

发布于 2025-01-21 06:23:58 字数 1627 浏览 1 评论 0原文

我正在尝试使用OOP使用语音识别。我尝试从此github 转换过程代码。

import speech_recognition as sr #recognise speech
import time


class SpeechRecognition():
    def __init__(self):
        self.r = sr.Recognizer() #initialize recognizer that is responsible for recognizing the speech
            
    # listen for audio and convert it to text:
    def record_audio(self, ask=False):
        with sr.Microphone() as source: # microphone as source
            if ask: 
                print(ask)
            audio = self.r.listen(source) # listen for the audio via source
            voice_data = ''
            try:
                voice_data = self.r.recognize_google(audio, language='sv-SE') # convert audio to text
            except sr.UnknownValueError: # error: recognizer does not understand
                print('Hörde inte det') 
            except sr.RequestError: # error: recognizer is not connected
                print('Ledsen, tjänsten är nere')
            print(f">> {voice_data.lower()}") # print what user said
            return voice_data.lower()

    def init_speech_recognizer(self):
        time.sleep(1)
        print('Hur kan jag hjälpa dig?')

        while 1: #this will let it keep listen 
            voice_data = self.record_audio("Recording") # get the voice input
            print("Done")
            
speechObj = SpeechRecognition()
speechObj.init_speech_recognizer()

输出是:

Hur kan jag hjälpa dig?
Recording

但是我说什么都没打印。为什么这是?

I am trying to use speech recognition with OOP. I try to convert the procedural code from this github.

import speech_recognition as sr #recognise speech
import time


class SpeechRecognition():
    def __init__(self):
        self.r = sr.Recognizer() #initialize recognizer that is responsible for recognizing the speech
            
    # listen for audio and convert it to text:
    def record_audio(self, ask=False):
        with sr.Microphone() as source: # microphone as source
            if ask: 
                print(ask)
            audio = self.r.listen(source) # listen for the audio via source
            voice_data = ''
            try:
                voice_data = self.r.recognize_google(audio, language='sv-SE') # convert audio to text
            except sr.UnknownValueError: # error: recognizer does not understand
                print('Hörde inte det') 
            except sr.RequestError: # error: recognizer is not connected
                print('Ledsen, tjänsten är nere')
            print(f">> {voice_data.lower()}") # print what user said
            return voice_data.lower()

    def init_speech_recognizer(self):
        time.sleep(1)
        print('Hur kan jag hjälpa dig?')

        while 1: #this will let it keep listen 
            voice_data = self.record_audio("Recording") # get the voice input
            print("Done")
            
speechObj = SpeechRecognition()
speechObj.init_speech_recognizer()

The output is:

Hur kan jag hjälpa dig?
Recording

But whatever I say nothing gets printed. Why is this?

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

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

发布评论

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