代码之家  ›  专栏  ›  技术社区  ›  ironmantis7x

传递字符串变量以从python函数返回数据时遇到问题,该函数将在python脚本或程序中的任何位置全局使用-为清晰起见,对其进行了编辑

  •  0
  • ironmantis7x  · 技术社区  · 6 年前

    我正在尝试获取流音频,并使用谷歌文本到语音转换为文本。然后将该文本作为输入传递到不在Watson上的对话。然后沃森返回答案。后半部分效果很好。

    我遇到的问题是,我无法让脚本将录制的语音中的文本传递给我创建的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文本到语音转换(text-to-speech)从音频转录生成的文本。我需要将录制的音频中的数据放入Python脚本的主要部分,在那里它与Watson服务通信。

    2 回复  |  直到 5 年前
        1
  •  1
  •   code beginner    6 年前

    列表和其他集合很有趣,因为当您试图修改现有集合而不是声明新集合时,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]
    
        2
  •  0
  •   ironmantis7x    5 年前

    在主程序中,我在麦克风处于活动状态时放置了一个while循环,然后启动用户输入以启动服务连接。

    现在可以了。

    谢谢大家。