简体   繁体   English

Azure Speech to Text 麦克风不停止连续录音

[英]Azure Speech to Text mic does not stop for continuous recording

I am using Azure Speech to text for transcribing audio thru mic/audio files.我正在使用 Azure 语音转文本通过麦克风/音频文件转录音频。

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 is not working. stop_continuous_session 不工作。 Mic is continuously running in the back-end even after force stopping the script即使在强制停止脚本后,Mic 仍在后端持续运行

The problem was caused by infinite loop of the while not done code.问题是由while not done代码的无限循环引起的。 The code while not done is always true, so it's going to go on forever. while not done的代码始终为真,因此它将永远变为 go。 The stop code will never be executed.永远不会执行停止代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM