sync_client.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 
00003 # import python libraries
00004 import urllib2
00005 import json
00006 import base64
00007 import socket
00008 import traceback
00009 import ssl
00010 
00011 # import local libraries
00012 from rospeex_core.validators import accepts
00013 from rospeex_core.validators import check_wave_data
00014 from rospeex_core.validators import check_language
00015 from rospeex_core import exceptions as ext
00016 from rospeex_core.sr.base import IClient
00017 
00018 
00019 class SyncClient(IClient):
00020     """ SpeechRecognitionClient_NICT class """
00021 
00022     AUDIO_LENGTH = 16000
00023     FRAMERATE = 16000
00024     CHANNELS = 1
00025     SAMPWIDTH = 2
00026     URL = 'http://rospeex.nict.go.jp/nauth_json/jsServices/VoiceTraSR'
00027     LANGUAGES = ['ja', 'en', 'zh', 'ko', 'id', 'my', 'th', 'vi', 'fr', 'es']
00028 
00029     @accepts(data=str, language=str, timeout=int)
00030     def request(
00031         self,
00032         data,
00033         language='ja',
00034         timeout=socket._GLOBAL_DEFAULT_TIMEOUT
00035     ):
00036         """ send speech recognition request to server
00037 
00038         @param data: speech binary data
00039         @type  data: str
00040         @param language: speech data language
00041         @type  language: str
00042         @param timeout: time out time[ms]
00043         @type  timeout: int
00044         """
00045         check_wave_data(
00046             data,
00047             self.FRAMERATE,
00048             self.CHANNELS,
00049             self.SAMPWIDTH,
00050             self.AUDIO_LENGTH
00051         )
00052         check_language(language, self.LANGUAGES)
00053 
00054         # load voice data and encord to base64
00055         voice_send = base64.b64encode(data)
00056 
00057         data = {
00058             'method': 'recognize',
00059             'params': [
00060                 '1.1',
00061                 {
00062                     'audio': voice_send,
00063                     'audioType': 'audio/x-wav',
00064                     'voiceType': '*',
00065                     'language': language,
00066                     'applicationType': 'rospeex'
00067                 }
00068             ]
00069         }
00070 
00071         # send request to server
00072         res_data = None
00073         try:
00074             req = urllib2.Request(self.URL)
00075             req.add_header('Content-Type', 'application/json')
00076             res = urllib2.urlopen(req, json.dumps(data), timeout=timeout)
00077             res_data = json.loads(res.read())
00078 
00079         except urllib2.URLError as err:
00080             if isinstance(err.reason, socket.timeout):
00081                 msg = 'request time out. Exception: {}'.format(str(err))
00082                 raise ext.RequestTimeoutException(msg)
00083 
00084             msg = 'request url error. Exception: {}'.format(str(err))
00085             raise ext.InvalidRequestException(msg)
00086 
00087         except urllib2.HTTPError as err:
00088             msg = 'http error. {} Exception: {}'.format(err.code, err.msg)
00089             raise ext.InvalidResponseException(msg)
00090 
00091         except (ssl.SSLError, socket.timeout) as err:
00092             raise ext.RequestTimeoutException(str(err))
00093 
00094         except:
00095             msg = 'unknown exception. Traceback: {}'.format(
00096                 traceback.format_exc()
00097             )
00098             raise ext.SpeechRecognitionException(msg)
00099 
00100         # get result
00101         if res_data['error'] is not None:
00102             if 'faultCode' in res_data['error']:
00103                 if res_data['error']['faultCode'] == 'Server.userException':
00104                     raise ext.InvalidResponseException(
00105                         'the audio format is not supported.'
00106                     )
00107             msg = 'server response error. msg:{}'.format(res_data['error'])
00108             raise ext.InvalidResponseException(msg)
00109 
00110         return res_data['result']
00111 
00112     def support_streaming(self):
00113         """
00114         check support streaming
00115         @return: True for support streaming / False for NOT support streaming
00116         """
00117         return False
00118 
00119     def add_streaming_packet(self, packet_type, packet_data):
00120         """ add streaming packet
00121         @param packet_type:
00122         @type  packet_type: int
00123         @param packet_data:
00124         @param packet_data: str
00125         """
00126         pass
00127 
00128     def register_streaming_cb(self, cb):
00129         """
00130         register streaming result callback
00131         @param cb:
00132         @type cb:
00133         """
00134         pass
00135 
00136     def unregister_streaming_cb(self, cb):
00137         """
00138         unregister streaming result callback
00139         @param cb:
00140         @type cb:
00141         """
00142         pass
00143 
00144     def set_streaming_config(self, language):
00145         """ set streaming config
00146         @param language:
00147         """
00148         pass
00149 
00150     def join(self, timeout=None):
00151         """
00152         join streaming client
00153         @param timeout:
00154         @type timeout:
00155         """
00156         pass


rospeex_core
Author(s): Komei Sugiura
autogenerated on Thu Jun 6 2019 18:53:10