def transcribe(self, fp):
"""
Performs STT via the Watson Speech-to-Text API, transcribing an audio
file and returning an English string.
Arguments:
fp -- the path to the .wav file to be transcribed
"""
if not self.username:
self._logger.critical('Username missing, transcription request aborted.')
return []
elif not self.password:
self._logger.critical('Password missing, transcription request aborted.')
return []
wav = wave.open(fp, 'rb')
frame_rate = wav.getframerate()
wav.close()
data = fp.read()
headers = {
'content-type': 'audio/l16; rate={0}; channels=1'.format(frame_rate)
}
r = self._http.post(
'https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?continuous=true',
data=data, headers=headers, auth=(self.username, self.password)
)
try:
r.raise_for_status()
except requests.exceptions.HTTPError as e:
self._logger.critical('Request failed with http status %d',
r.status_code)
if r.status_code == requests.codes['forbidden']:
self._logger.warning('Status 403 is probably caused by invalid credentials.')
return []
r.encoding = 'utf-8'
results = []
try:
response = r.json()
if not response['results']:
raise ValueError('Nothing has been transcribed.')
results = tuple([alt['transcript'].strip().upper() for alt in response['results'][0]['alternatives']])
self._logger.info('Transcribed: %r', results)
except ValueError as e:
self._logger.critical('Empty response: %s', e.args[0])
except (KeyError, IndexError):
self._logger.critical('Cannot parse response.', exc_info=True)
return results
评论列表
文章目录