首页 > 解决方案 > 如何从语音和键盘输入中获得第一个可用结果?

问题描述

我正在尝试制作一个简单的 python 程序,它能够监听来自多个来源的文本消息。目前我想使用标准输入和语音识别器。这个想法是用户能够使用语音或键盘插入文本,并且当准备好时返回值。

所以我有这样的事情:

def keyboard_input():
   return input()

def voice_input():
   return listener.listen()

def method():
   output = ''
   # Listen from keyboard_input and voice_input
   ...
   # Input received from one source, terminate the other one
   return output

我正在尝试使用线程,例如在单独的线程中运行两个输入法,但是我正在努力处理返回和终止部分。

已编辑,有关方法的更多详细信息:

import speech_recognition as sr

def listen():
    recognizer = sr.Recognizer()

    with sr.Microphone() as source:
        recognizer.adjust_for_ambient_noise(source)
        print("Listening:...")
        audio = recognizer.listen(source)
    try:
        text = recognizer.recognize_google(audio, language="it-IT")
        return text
    except Exception as e:
        print(e)

def write():
    print('Insert text...')
    text = input()
    print(text)
    return text


OUTPUT = None
# Code to run at the same time listen and write on a common variable OUTPUT,
#...
# If one of them gives the output the other method should terminate

print(OUTPUT)

标签: pythonmultithreadingpython-multithreading

解决方案


你安装 v_input 包了吗?
您可以通过在 Anaconda Prompt 上复制此代码来安装此软件包:

!pip-install -v_input

推荐阅读