无法将字符串变量从 python 函数传递到 return 数据,以便在 Python 脚本或程序中全局使用

Trouble passing string variable to return data from python function to be used globally anywhere in a Python script or program

我正在尝试获取流式音频并使用 Google 文本转语音将其转换为文本。然后将该文本作为输入传递给不在 Watson 上的对话。然后 Watson returns 它的答案。后半段效果不错

我遇到的问题是我无法让脚本将文本从录制的语音传递到我创建的 Watson 服务。

我没有收到错误,我什么也没收到。麦克风正在工作(我用另一个脚本测试了它)。该程序实际上表明我可以理解我的回答(我假设没有文字)。这是我的代码

import os
import watson_developer_cloud

import speech_recognition as sr
from gtts import gTTS
import watson_developer_cloud
import time

# Set up Assistant service.
service = watson_developer_cloud.AssistantV1(
  #username = 'USERNAME', # replace with service username
  #password = 'PASSWORD', # replace with service password

  iam_api_key = 'xxxxxxxxxx', # replace with service username
  url = 'xxxxxxxxxx', # replace with service password
  version = 'xxxxxxxxxx'
)
workspace_id = 'xxxxxxxxxxxxxx' # replace with workspace ID

def getaudiodevices():
    devices = os.popen("arecord -l")
    device_string = devices.read()
    device_string = device_string.split("\n")
    for line in device_string:
        if line.find("card") != -1:
            print("hw:" + line[line.find("card") + 5] + "," + line[line.find("device") + 7])

def speak(audiostring):
    print(audiostring)
    tts = gTTS(text=audiostring, lang='en')
    tts.save('audio.mp3')
    os.system('mpg321 audio.mp3')

def recordaudio():
    # Record Audio
    r = sr.Recognizer()
    with sr.Microphone(0) as source:
        print("Say something!")
        audio = r.listen(source,phrase_time_limit=10)
    # Speech recognition ******
    data = " "
    try:
        data = r.recognize_google(audio)
        print("You said: " + data)
    except sr.UnknownValueError:
        print("Google Speech Recognition could not understand audio")
    except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition service; {0}".format(e))

    return data

# Initialize with empty value to start the conversation.
user_input = ''
context = {}
current_action = ''

# Main input/output loop
while current_action != 'end_conversation':

  # Send message to Assistant service.
  response = service.message(
    workspace_id = workspace_id,
    input = {
      'text': user_input
    },
    context = context
  )

  # Print the output from dialog, if any.
  if response['output']['text']:
    print(response['output']['text'][0])
    speak(response['output']['text'][0])

  # Update the stored context with the latest received from the dialog.
  context = response['context']
  # Check for action flags sent by the dialog.
  if 'action' in response['output']:
    current_action = response['output']['action']
  # User asked what time it is, so we output the local system time.
  if current_action == 'display_time':
    print('The current time is ' + time.strftime('%I:%M:%S %p') + '.')
    speak('The current time is ' + time.strftime('%I:%M:%S %p') + '.')
  # If we're not done, prompt for next round of input.
  if current_action != 'end_conversation':
    user_input = input('>> ')

目前我可以用键盘写演讲稿,而且很管用。我希望用户输入来自使用 Google 文本转语音从转录音频生成的文本。我需要从录制的音频中获取数据到我的 Python 脚本的主要部分,它与 Watson 服务进行通信。

有些细节最好通过示例来探索,我将在下面提供。一个重要的关键字是'global'。您可以在函数内使用关键字 'global' 以确保 Python 知道您要使用具有该名称的全局变量,而不是声明局部变量。

列表和其他集合很有趣,因为 Python 可以理解您何时尝试修改现有集合而不是声明新集合。演示代码中包含一个示例。

# These are global variables accessible anywhere in the script.
# They do not belong to any function or class.
my_string = ""
my_list = [1]


def a():
    return "a"


def b():
    my_string = "b" # this *is not* the global my_string


def c():
    global my_string
    my_string = "c" # this *is* the global my_string


def d():
    my_list = [7] # this *is not* the global my_list


def e():
    my_list[0] = 4 # this *is* the global my_list


def f():
    global my_list
    my_list = [1, 2, 3] # this *is* the global my_list


my_string = a()
print(my_string) # my_string has been assigned "a"


b()
print(my_string) # my_string is still "a"


c()
print(my_string) # my_string is now "c"


d()
print(my_list) # my_list is still [1]


e()
print(my_list) # my_list is now [7]


f()
print(my_list) # my_list is now [1, 2, 3]

因此,@Denziloe 的解决方案是正确的,但又增加了一些内容。

主要是我在麦克风处于活动状态时放置了一个 while 循环,然后启动用户输入以启动服务连接。 然后我用 data = recordaudio() 放入另一个循环并将用户输入设置为 data.

现在可以使用了。

谢谢大家。