This repository was archived by the owner on May 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathtranscribe.py
More file actions
executable file
·222 lines (190 loc) · 6.95 KB
/
transcribe.py
File metadata and controls
executable file
·222 lines (190 loc) · 6.95 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env python
#
# Copyright 2016 IBM
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import argparse
import base64
import configparser
import json
import threading
import time
import pyaudio
import websocket
from websocket._abnf import ABNF
CHUNK = 1024
FORMAT = pyaudio.paInt16
# Even if your default input is multi channel (like a webcam mic),
# it's really important to only record 1 channel, as the STT service
# does not do anything useful with stereo. You get a lot of "hmmm"
# back.
CHANNELS = 1
# Rate is important, nothing works without it. This is a pretty
# standard default. If you have an audio device that requires
# something different, change this.
RATE = 44100
RECORD_SECONDS = 5
FINALS = []
LAST = None
REGION_MAP = {
'us-east': 'gateway-wdc.watsonplatform.net',
'us-south': 'stream.watsonplatform.net',
'eu-gb': 'stream.watsonplatform.net',
'eu-de': 'stream-fra.watsonplatform.net',
'au-syd': 'gateway-syd.watsonplatform.net',
'jp-tok': 'gateway-syd.watsonplatform.net',
}
def read_audio(ws, timeout):
"""Read audio and sent it to the websocket port.
This uses pyaudio to read from a device in chunks and send these
over the websocket wire.
"""
global RATE
p = pyaudio.PyAudio()
# NOTE(sdague): if you don't seem to be getting anything off of
# this you might need to specify:
#
# input_device_index=N,
#
# Where N is an int. You'll need to do a dump of your input
# devices to figure out which one you want.
RATE = int(p.get_default_input_device_info()['defaultSampleRate'])
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("* recording")
rec = timeout or RECORD_SECONDS
for i in range(0, int(RATE / CHUNK * rec)):
data = stream.read(CHUNK)
# print("Sending packet... %d" % i)
# NOTE(sdague): we're sending raw binary in the stream, we
# need to indicate that otherwise the stream service
# interprets this as text control messages.
ws.send(data, ABNF.OPCODE_BINARY)
# Disconnect the audio stream
stream.stop_stream()
stream.close()
print("* done recording")
# In order to get a final response from STT we send a stop, this
# will force a final=True return message.
data = {"action": "stop"}
ws.send(json.dumps(data).encode('utf8'))
# ... which we need to wait for before we shutdown the websocket
time.sleep(1)
ws.close()
# ... and kill the audio device
p.terminate()
def on_message(self, msg):
"""Print whatever messages come in.
While we are processing any non trivial stream of speech Watson
will start chunking results into bits of transcripts that it
considers "final", and start on a new stretch. It's not always
clear why it does this. However, it means that as we are
processing text, any time we see a final chunk, we need to save it
off for later.
"""
global LAST
data = json.loads(msg)
if "results" in data:
if data["results"][0]["final"]:
FINALS.append(data)
LAST = None
else:
LAST = data
# This prints out the current fragment that we are working on
print(data['results'][0]['alternatives'][0]['transcript'])
def on_error(self, error):
"""Print any errors."""
print(error)
def on_close(ws):
"""Upon close, print the complete and final transcript."""
global LAST
if LAST:
FINALS.append(LAST)
transcript = "".join([x['results'][0]['alternatives'][0]['transcript']
for x in FINALS])
print(transcript)
def on_open(ws):
"""Triggered as soon a we have an active connection."""
args = ws.args
data = {
"action": "start",
# this means we get to send it straight raw sampling
"content-type": "audio/l16;rate=%d" % RATE,
"continuous": True,
"interim_results": True,
# "inactivity_timeout": 5, # in order to use this effectively
# you need other tests to handle what happens if the socket is
# closed by the server.
"word_confidence": True,
"timestamps": True,
"max_alternatives": 3
}
# Send the initial control message which sets expectations for the
# binary stream that follows:
ws.send(json.dumps(data).encode('utf8'))
# Spin off a dedicated thread where we are going to read and
# stream out audio.
threading.Thread(target=read_audio,
args=(ws, args.timeout)).start()
def get_url():
config = configparser.RawConfigParser()
config.read('speech.cfg')
# See
# https://console.bluemix.net/docs/services/speech-to-text/websockets.html#websockets
# for details on which endpoints are for each region.
region = config.get('auth', 'region')
host = REGION_MAP[region]
return ("wss://{}/speech-to-text/api/v1/recognize"
"?model=en-US_BroadbandModel").format(host)
def get_auth():
config = configparser.RawConfigParser()
config.read('speech.cfg')
apikey = config.get('auth', 'apikey')
return ("apikey", apikey)
def parse_args():
parser = argparse.ArgumentParser(
description='Transcribe Watson text in real time')
parser.add_argument('-t', '--timeout', type=int, default=5)
# parser.add_argument('-d', '--device')
# parser.add_argument('-v', '--verbose', action='store_true')
args = parser.parse_args()
return args
def main():
# Connect to websocket interfaces
headers = {}
userpass = ":".join(get_auth())
headers["Authorization"] = "Basic " + base64.b64encode(
userpass.encode()).decode()
url = get_url()
# If you really want to see everything going across the wire,
# uncomment this. However realize the trace is going to also do
# things like dump the binary sound packets in text in the
# console.
#
# websocket.enableTrace(True)
ws = websocket.WebSocketApp(url,
header=headers,
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.on_open = on_open
ws.args = parse_args()
# This gives control over the WebSocketApp. This is a blocking
# call, so it won't return until the ws.close() gets called (after
# 6 seconds in the dedicated thread).
ws.run_forever()
if __name__ == "__main__":
main()