python - 如何从语音和键盘输入中获得第一个可用结果?
问题描述
我正在尝试制作一个简单的 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)
解决方案
你安装 v_input 包了吗?
您可以通过在 Anaconda Prompt 上复制此代码来安装此软件包:
!pip-install -v_input
推荐阅读
- qt - 当 QLineEdit 失焦时,QLineEdit 聚焦时启动的 QProcess 是否可以终止?
- json - 如何在fluter上的Firestore中序列化/建模模型json数据
- html - 如何调整 React Bootstrap 的 Carousel 以按比例调整大小?
- python - 如何在Django中处理上传的excel文件
- r - 变量选择,使用 glmmLasso 包中的 glmmLasso() 函数处理多重共线性
- laravel - Laravel Passport - 将默认未经身份验证的响应更改为自定义响应
- python - 为什么我的模型训练减少而验证不减少,训练准确度增加而验证准确度不增加?
- java - 查看mongo DB中已经执行的查询
- python - x_train /= 255.0 和 x_train = x_train/255.0 之间的标准化差异
- javascript - 如何使用javascript中的ajax调用将参数或ajax数据传递给python脚本并在成功时获得响应?