Problem with threading and Microsoft's speech SDK

I am trying to update the real-time speech-to-text example to use Microsoft’s speech-to-text API instead of Deepspeech.

When creating the speech recognizer for real-time speech recognition, Microsoft’s speech SDK requires you to attach callback functions for different events (partial recognition, final recognition, and so on).

speech_recognizer.recognizing.connect(reco_recognizing)
speech_recognizer.recognized.connect(reco_recognized)
speech_recognizer.session_started.connect(reco_started)
speech_recognizer.session_stopped.connect(reco_ended)
speech_recognizer.canceled.connect(reco_ended)

Consider one of these callbacks, when recognition of an utterance is complete:

update_field = st.empty()

def reco_recognized(event):
    offset = event.result.offset // 10000
    duration = event.result.duration // 1000
    text = event.result.text      
    # this works
    print(offset, duration, text)
    # this doesn't
    update_field.write(text)

When these callbacks are called, however, they appear to be called within “dummy” threads, and the message below is printed:

2022-08-25 19:10:12.548 Thread 'Dummy-9': missing ScriptRunContext

From digging around, I understand for threads, I am supposed to pass in the ST context (1 2). However, in my situation, the thread is created in a library call, rather than in a thread I created directly. I am not sure how to pass this context when I didn’t create the thread, and furthermore, I’m a bit confused why the context isn’t available anyway.

Can someone help me solve this issue? Is it possible to accomplish what I want to do? Nothing I have tried seems to work, and since I don’t understand this very well, I’m not really sure what to do.

1 Like

One thing I’ve tried is saving the context as part of the function definition’s lexical context, and then setting that in the thread. The complaint about the missing ScriptRunContext goes away, but nothing is written in the app.

output_box = st.empty()
ctx = get_script_run_ctx()
def reco_recognized(event):
    add_script_run_ctx(threading.current_thread(), ctx)
    output_box.write(event.text)

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.