致命的Python错误:运行我自己的语音助手程序时出现分段错误

时间:2019-03-26 13:26:18

标签: python raspberry-pi3 google-aiy

在Raspberry Pi 3B上运行自定义语音助手程序时出现错误。此脚本由示例中的两个程序(assistant_library_demo.py和cloud_speech.py​​)组成。因此,脚本可以执行这些示例中的这两个功能。程序代码如下所示:

这是针对语音助手的,它结合了Google语音助手和Google Cloudspeech API的功能

import argparse
import locale
import logging
import RPi.GPIO as GPIO
from gpiozero import Servo
import signal
import sys
from google.assistant.library.event import EventType
from aiy.assistant import auth_helpers
from aiy.assistant.library import Assistant
from aiy.board import Board, Led
from aiy.cloudspeech import CloudSpeechClient
import aiy.voice.tts


def get_hints(language_code):
    if language_code.startswith("en_"):
        return (
            "turn on the light",
            "turn off the light",
            "blink the light",
            "goodbye",
            "repeat after me",
            "lights on",
            "lights off",
            "minimum",
            "middle",
            "maximum",
        )
    return None


def locale_language():
    language, _ = locale.getdefaultlocale()
    return language


def process_event(led, event):
    logging.basicConfig(level=logging.INFO)
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    GPIO.setup(24, GPIO.OUT)
    servo = Servo(6)
    parser = argparse.ArgumentParser(description="Assistant service example.")
    parser.add_argument("--language", default=locale_language())
    args = parser.parse_args()
    logging.info("Initializing for language %s...", args.language)
    hints = get_hints(args.language)
    client = CloudSpeechClient()
    logging.info(event)
    text = client.recognize(language_code=args.language, hint_phrases=hints)
    if "lights on" in text:
        GPIO.output(24, GPIO.HIGH)
    elif "lights off" in text:
        GPIO.output(24, GPIO.LOW)
    elif "maximum" in text:
        servo.max()
    elif "minimum" in text:
        servo.min()
    elif "middle" in text:
        servo.mid()
    if event.type == EventType.ON_START_FINISHED:
        led.state = Led.BEACON_DARK  # Ready.
        logging.info('Say "OK, Google" then speak, or press Ctrl+C to quit...')
    elif event.type == EventType.ON_CONVERSATION_TURN_STARTED:
        led.state = Led.ON  # Listening.
    elif event.type == EventType.ON_END_OF_UTTERANCE:
        led.state = Led.PULSE_QUICK  # Thinking.
    elif (
        event.type == EventType.ON_CONVERSATION_TURN_FINISHED
        or event.type == EventType.ON_CONVERSATION_TURN_TIMEOUT
        or event.type == EventType.ON_NO_RESPONSE
    ):
        led.state = Led.BEACON_DARK
    elif (
        event.type == EventType.ON_ASSISTANT_ERROR
        and event.args
        and event.args["is_fatal"]
    ):
        main()


def main():
    credentials = auth_helpers.get_assistant_credentials()
    with Board() as board, Assistant(credentials) as assistant:
        for event in assistant.start():
            process_event(board.led, event)


if name == "main":
    main()

这是错误文本的样子:从python shell运行草图时的错误文本:

Backend terminated (returncode: -11) 
Fatal Python error: Segmentation fault

Thread 0x580fe470 (most recent call first): File "/usr/lib/python3.5/threading.py", line 297 in wait File "/usr/lib/python3.5/threading.py", line 549 in wait File "/opt/aiy/projects-python/src/aiy/board.py", line 208 in _run File "/usr/lib/python3.5/threading.py", line 862 in run File "/usr/lib/python3.5/threading.py", line 914 in _bootstrap_inner File "/usr/lib/python3.5/threading.py", line 882 in _bootstrap

Thread 0x76f66640 (most recent call first): File "/usr/local/lib/python3.5/dist-packages-linux-armv7l/google/assistant/library/assistant.py", 
line 114 in exit File "/opt/aiy/projects-python/src/examples/voice/myassistant.py", 
line 96 in main File "/opt/aiy/projects-python/src/examples/voice/myassistant.py",
 line 99 in File "/usr/lib/python3/dist-packages/thonny/backend.py",
 line 1232 in _execute_prepared_user_code File "/usr/lib/python3/dist-packages/thonny/backend.py", 
line 1158 in wrapper File "/usr/lib/python3/dist-packages/thonny/backend.py", 
line 1171 in wrapper File "/usr/lib/python3/dist-packages/thonny/backend.py", 
line 1219 in execute_source File "/usr/lib/python3/dist-packages/thonny/backend.py",
 line 853 in _execute_source File "/usr/lib/python3/dist-packages/thonny/backend.py", 
line 840 in _execute_file File "/usr/lib/python3/dist-packages/thonny/backend.py", 
line 400 in _cmd_Run File "/usr/lib/python3/dist-packages/thonny/backend.py",
line 217 in handle_command File "/usr/lib/python3/dist-packages/thonny/backend.py", 
line 162 in mainloop File "/usr/lib/python3/dist-packages/thonny/backend_launcher.py", 
line 70 in Use 'Stop/Restart' to restart the backend ...

我从终端Segmentation fault运行草图时的错误文本

1 个答案:

答案 0 :(得分:0)

感谢大家的帮助,我通过使用不同的Google演示重写整个代码来解决了问题