【问题标题】:Azure Speech to Text mic does not stop for continuous recordingAzure Speech to Text 麦克风不会停止连续录音
【发布时间】:2020-09-09 16:36:14
【问题描述】:

我正在使用 Azure Speech to text 通过麦克风/音频文件转录音频。

def speech_continuous_recognize_from_mic():
"""performs one-shot speech recognition from the default microphone"""
# <SpeechRecognitionWithMicrophone>
speech_config = speechsdk.SpeechConfig(subscription=configuration["speech_api"]["speech_key"], region=configuration["speech_api"]["service_region"])
# Creates a speech recognizer using microphone as audio input.
# The default language is "en-us".
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config)

done = False

def stop_cb(evt):
    """callback that signals to stop continuous recognition upon receiving an event `evt`"""
    print('CLOSING on {}'.format(evt))
    nonlocal done
    done = True

all_results = []
def handle_final_result(evt):
    all_results.append(evt.result.text)

speech_recognizer.recognized.connect(handle_final_result)        

speech_recognizer.session_started.connect(lambda evt: print('\nSESSION STARTED: {}'.format(evt)))
speech_recognizer.session_stopped.connect(lambda evt: print('\nSESSION STOPPED {}'.format(evt)))

speech_recognizer.session_stopped.connect(stop_cb)
speech_recognizer.canceled.connect(stop_cb)

# Start continuous speech recognition
**speech_recognizer.start_continuous_recognition()
while not done:
    time.sleep(1)
print ("####################################################################################")
speech_recognizer.stop_continuous_recognition()
speech_recognizer.session_started.disconnect_all()
speech_recognizer.recognized.disconnect_all()
speech_recognizer.session_stopped.disconnect_all()**

stop_continuous_session 不工作。即使在强制停止脚本后,麦克风仍在后端持续运行

【问题讨论】:

  • @V Shriram 您可能想看看官方示例here 中提到的对 stop_continuous_recognition() 的调用顺序。一位用户在 github 上发布了类似的issue,在更正调用顺序后得到了解决。

标签: python-3.x azure microsoft-cognitive


【解决方案1】:

问题是由while not done 代码的无限循环引起的。代码while not done 始终为真,因此它将永远持续下去。停止代码永远不会被执行。

【讨论】:

    猜你喜欢
    • 2023-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 2021-08-13
    • 2014-11-23
    • 1970-01-01
    相关资源
    最近更新 更多