forked from dmmiller612/bert-extractive-summarizer
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathspeech2text.py
63 lines (50 loc) · 2.66 KB
/
speech2text.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import time
import json
import azure.cognitiveservices.speech as speechsdk
def speech_recognize_continuous_from_file(weatherfilename, secretsfilepath):
"""performs continuous speech recognition with input from an audio file"""
# Set up the subscription info for the Speech Service:
# Replace with your own subscription key and service region (e.g., "westus").
with open(secretsfilepath) as f:
data = json.load(f)
speech_key, service_region = data['key'], data['region']
# <SpeechContinuousRecognitionWithFile>
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
# configure to get word level timestamps
speech_config.request_word_level_timestamps()
audio_config = speechsdk.audio.AudioConfig(filename=weatherfilename)
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_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
# Connect callbacks to the events fired by the speech recognizer
# speech_recognizer.recognizing.connect(lambda evt: print('RECOGNIZING: {}'.format(evt)))
txt2timestamp = {}
timestamp2txt = {}
def handler(evt):
# print('JSON: {}'.format(evt.result.json))
# results.append({'offset': evt.result.offset, 'sentence': evt.result.text})
print(f'offset: {evt.result.offset}, text: {evt.result.text}')
txt2timestamp[evt.result.text] = evt.result.offset
timestamp2txt[evt.result.offset] = evt.result.text
speech_recognizer.recognized.connect(handler)
# speech_recognizer.recognized.connect(lambda evt: print('JSON: {}'.format(evt.result.json)))
speech_recognizer.session_started.connect(lambda evt: print('SESSION STARTED: {}'.format(evt)))
speech_recognizer.session_stopped.connect(lambda evt: print('SESSION STOPPED {}'.format(evt)))
def cancelled_handler(evt):
print('CANCELED {}'.format(evt))
speech_recognizer.canceled.connect(cancelled_handler)
# speech_recognizer.canceled.connect(lambda evt: print('CANCELED {}'.format(evt)))
# stop continuous recognition on either session stopped or canceled events
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(.5)
speech_recognizer.stop_continuous_recognition()
return txt2timestamp, timestamp2txt
# </SpeechContinuousRecognitionWithFile>