簡體   English   中英

python錯誤TypeError:'NoneType'類型的參數不可迭代

[英]The python error TypeError: argument of type 'NoneType' is not iterable

我正在使用 Azure 語音識別服務,但無法創建命令。

我正在使用解釋器 Python 3.10.4

代碼:

def takeCommand():
    
    speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
    speech_config.speech_recognition_language="tr-TR"

    audio_config = speechsdk.audio.AudioConfig(use_default_microphone=True)
    speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)

    text = "Dinliyorum..."
    speech_synthesizer.speak_text_async(text).get()
    query = speech_recognizer.recognize_once_async().get()
    
    if query.reason == speechsdk.ResultReason.RecognizedSpeech:
        print("Recognized: {}".format(query.text))
    elif query.reason == speechsdk.ResultReason.NoMatch:
        print("No speech could be recognized: {}".format(query.no_match_details))
    elif query.reason == speechsdk.ResultReason.Canceled:
        cancellation_details = query.cancellation_details
        print("Speech Recognition canceled: {}".format(cancellation_details.reason))
        if cancellation_details.reason == speechsdk.CancellationReason.Error:
            print("Error details: {}".format(cancellation_details.error_details))
            print("Did you set the speech resource key and region values?")

當我檢查來自麥克風的輸入並要求它響應時,我收到此錯誤。

if __name__ == '__main__':
      wishMe()
      
      while True:
            
            query = takeCommand()
            
            if 'Nasılsın' in query:
                  text = "Teşekkürler, İyiyim Sen Nasılsın?"
                  speech_synthesizer.speak_text_async(text).get()
          

我收到此錯誤:

回溯(最近一次通話最后):

文件“------------”,第 58 行,在
如果查詢中的“Nasılsın”:
TypeError:“NoneType”類型的參數不可迭代

感謝您試用認知語音服務 Python SDK!

正如上面提到的評論,您需要添加

return query

到您的 takeCommand() 方法的末尾。 這將返回 SpeechRecognitionResult。

然后,由於您想匹配識別的文本,您需要修改您的條件以檢查返回的 SpeechRecognitionResult 中的“文本”屬性:

if 'Nasılsın' in query.text:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM