语音识别只能运行几次

时间:2018-06-22 15:28:04

标签: python kivy voice-recognition

我对python很陌生,所以感谢您的帮助:)

因此,我正在尝试使用语音识别和猕猴桃。该应用程序实际上运行得很好,当它被识别时会显示“网球”或“足球”。但是,当我运行该程序时,它只能连续或多或少地被识别3次。之后,感觉好像程序卡住了。目标是像Alex / Echo一样使用它。因此,它应该在监听几分钟,然后等待命令。

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.image import Image
from time import sleep

import threading
from kivy.clock import mainthread

import speech_recognition as sr

r = sr.Recognizer()
mic = sr.Microphone()


def record_speech_from_mic(recognizer, microphone):
    print("talk now..")

    with microphone as source:
        audio = recognizer.listen(source)

    try:
        response = recognizer.recognize_google(audio)
    except sr.RequestError:
        response = "False"
    except sr.UnknownValueError:
        response = "False"

    return response


class Screen(GridLayout):

    compare1 = 'tennis'
    compare2 = 'football'

    def __init__(self, **kwargs):
        super(Screen, self).__init__(**kwargs)
        self.cols = 3
        self.add_widget(Button(text='Record', on_release=self.button_press))

        self.output_text = Label(text='Text')
        self.add_widget(self.output_text)

        self.output_image = Image(source='Start.png')
        self.add_widget(self.output_image)

    def update_threading(self,event):
        threading.Thread(target=self.update).start()

    def update(self):
        while True:
            recorded = record_speech_from_mic(r, mic)
            print("recorded", recorded)

            words = recorded.split(" ")

            for word in words:
                if word.lower() == self.compare1.lower():
                    # print("match", word)
                    self.output_text.text = word
                    self.update_image('c1.jpg')

                elif word.lower() == self.compare2.lower():
                    self.output_text.text = word
                    self.update_image('c2.jpg')
                    self.output_image.reload()
                    break

    @mainthread
    def update_image(self,src):
        self.output_image.source=src

    def button_press(self, event):
        self.output_text.text = "Recording"
        self.update_threading(self)


class MyApp(App):
    def build(self):

        with mic as source:
            r.adjust_for_ambient_noise(source)

        return Screen()


if __name__ == '__main__':
    MyApp().run()

0 个答案:

没有答案