语音识别器在第一个声音输入时不起作用

时间:2018-06-09 12:26:55

标签: python

我刚刚将python连接到arduino以使用声音输入。然而,主要问题不在于arduino,而在于python

import speech_recognition as sr
import time
while True:
r = sr.Recognizer()
with sr.Microphone() as source:
    audio = r.listen(source)

a = (r.recognize_google(audio))
print(a)

if a == 'light on':
    print('ON')

if a == 'switch off':
    print('OFF')

有时会返回NameError:a未定义,因此a不是None。它仅作为第一个输入发生。如果它识别出第一个命令(例如关闭),则不会在整个运行时中发生。 虽然有时它在运行时崩溃并且出现此错误:

File "C:\Program Files (x86)\Python36-32\lib\site-packages\speech_recognition\__init__.py", line 858, in recognize_google
    if not isinstance(actual_result, dict) or len(actual_result.get("alternative", [])) == 0: raise UnknownValueError()
speech_recognition.UnknownValueError

我可以看到它,当然,只有当我评论出来试试......除了......

1 个答案:

答案 0 :(得分:0)

library(purrr)
process_files <- function(file_names, condition)
    map(file_names, process_1_file, condition) %>%
        bind_rows()

dir(pattern="*.csv") %>% process_files(condition)

如果发生异常,try: a = (r.recognize_google(audio)) print(a) except: pass if a == 'light on': arduino.write('H'.encode()) 将不会在以后定义,程序将崩溃。

例如

a