“NoneType”类型的参数不是可迭代的覆盆子pi

时间:2018-01-08 00:12:00

标签: python

我正在编写如下语音控制代码:

import RPi.GPIO as GPIO
import os
import time
import aiy.audio
import aiy.cloudspeech
import aiy.voicehat
GPIO.setmode(GPIO.BCM)
GPIO.setup(26, GPIO.OUT)
GPIO.setup(6, GPIO.OUT)
GPIO.output(26, GPIO.HIGH)
GPIO.output(6, GPIO.HIGH)
def main():
    recognizer = aiy.cloudspeech.get_recognizer()
    recognizer.expect_phrase('OK Google"')
    recognizer.expect_phrase('turn off the light')
    recognizer.expect_phrase('turn on the light')
    recognizer.expect_phrase('blink')
    recognizer.expect_phrase('repeat after me')
    recognizer.expect_phrase('new light')
    button = aiy.voicehat.get_button()
    led = aiy.voicehat.get_led()
    aiy.audio.get_recorder().start()

    while True:
        text = recognizer.recognize()
        while 'OK Google' not in text:
            text = recognizer.recognize()
        text  = text.replace('OK Google ', '', 1)
        if text is None:
            print('Sorry, I did not hear you.')
        else:
            print('You said "', text, '"')
            if 'turn on the light' in text:
                led.set_state(aiy.voicehat.LED.ON)
            elif 'turn off the light' in text:
                led.set_state(aiy.voicehat.LED.OFF)
            elif 'blink' in text:
                led.set_state(aiy.voicehat.LED.BLINK)
            elif 'repeat after me' in text:
                to_repeat = text.replace('repeat after me', '', 1)
                aiy.audio.say(to_repeat)
            elif 'new light' in text:
                GPIO.output(6, GPIO.LOW)
                time.sleep(.75)
                GPIO.output(6, GPIO.HIGH)
            elif 'goodbye' in text:
                os._exit(0)
        led.set_state(aiy.voicehat.LED.OFF)
if __name__ == '__main__':
    main()

它适用于Raspberry pi上的python,它应该听OK Google然后返回文本并可能做一个动作。通常,它工作正常;但如果我暂时没有说什么,它会给我一个错误,说:

Traceback (most recent call last):
  File "copyspeech.py", line 56, in <module>
    main()
  File "copyspeech.py", line 31, in main
    while 'OK Google' not in text:
TypeError: argument of type 'NoneType' is not iterable

任何人都知道为什么? 我在python上相当新。

0 个答案:

没有答案