Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def speech_to_text(
- file_name, auth_file='auth.json', output_dir='output', opt_out=False,
- tokenauth=None,
- model='en-US_BroadbandModel', content_type='audio/wav',
- threads=1
- ):
- """
- Convert given audio file to text.
- """
- # logging
- # log.startLogging(sys.stdout)
- # add audio files to the processing queue
- q = Queue.Queue()
- file_number = 0
- q.put((file_number, file_name))
- hostname = "stream.watsonplatform.net"
- headers = {}
- if (opt_out is True):
- headers['X-WDC-PL-OPT-OUT'] = '1'
- try:
- with open(auth_file) as authfile:
- auth_data = json.load(authfile)
- username = auth_data['stt']['username']
- password = auth_data['stt']['password']
- except:
- raise
- # authentication header
- if tokenauth:
- headers['X-Watson-Authorization-Token'] = (
- Utils.getAuthenticationToken(
- "https://" + hostname, 'speech-to-text',
- username, password))
- else:
- string = username + ":" + password
- headers["Authorization"] = "Basic " + base64.b64encode(string)
- # create a WS server factory with our protocol
- url = "wss://" + hostname + "/speech-to-text/api/v1/recognize?model="
- + model
- summary = {}
- factory = WSInterfaceFactory(q, summary, output_dir, content_type,
- model, url, headers, debug=False)
- factory.protocol = WSInterfaceProtocol
- for i in range(min(int(threads), q.qsize())):
- factory.prepareUtterance()
- # SSL client context: default
- if factory.isSecure:
- context_factory = ssl.ClientContextFactory()
- else:
- context_factory = None
- connectWS(factory, context_factory)
- reactor.run()
- value = summary[0]
- if value['status']['code'] == 1000:
- return value['hypothesis'].encode('utf-8')
- else:
- return False
Add Comment
Please, Sign In to add comment