test_rospeex_sr.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 
00004 # import ros packages
00005 try:
00006     import roslib; roslib.load_manifest('rospeex')
00007 except:
00008     pass
00009 
00010 import rospy
00011 import unittest
00012 import rostest
00013 
00014 # Import other libraries
00015 from rospeex_if import ROSpeexInterface
00016 
00017 # import python libraries
00018 import os
00019 import sys
00020 
00021 class TestRospeexSR(unittest.TestCase):
00022     """ Speech Synthesis Test class """
00023     def setUp(self):
00024         self._message = ''
00025         self._ready = 1
00026         self._interface = None
00027         self._wait_flag = True
00028         self._wait_count = 0
00029 
00030     def _sr_response(self, message):
00031         """
00032         speech synthesis response callback
00033 
00034         @param message: message class
00035         @type  message: str
00036         """
00037         self._message = message
00038         self._ready = 1
00039         rospy.loginfo('Message Received :' + message)
00040         self._wait_count -= 1
00041 
00042     def _read_input_data(self, target_file):
00043         """
00044         read input data
00045 
00046         @param target_file: target test data
00047         @type  target_file: str
00048         """
00049         data = None
00050         with open(target_file, 'rb') as f_log:
00051             data = f_log.read()
00052 
00053         if data is None:
00054             rospy.logwarn('[%s] is not found.' % target_file)
00055         return data
00056 
00057 
00058     def _execute_case(self, voice_data, target_lang, target_engine):
00059         """
00060         execute test case
00061 
00062         @param message: voice data (binary wav)
00063         @type  message: str
00064         @param target_lang: recognition ranguage
00065         @type  target_lang: str
00066         @param target_engine: target speech recognition engine
00067         @type  target_engine: str (nict | google)
00068         """
00069         self._ready = 0
00070         self._interface.recognize(voice_data, target_lang, target_engine)
00071         self._wait_count += 1
00072         while not self._ready and self._wait_flag:
00073             rospy.sleep(0.1)  # wait until callback response
00074 
00075     def test_run(self):
00076         self._ready = 1
00077         rospy.init_node('test_rospeex_sr')
00078         self._interface = ROSpeexInterface()
00079         self._interface.init()
00080         self._interface.register_sr_response(self._sr_response)
00081         rospy.sleep(0.5)
00082 
00083         target_dir = rospy.get_param('~target_dir', '../testdata')
00084         target_engine = rospy.get_param('~target_engine', 'nict')
00085         target_engine_list = list(rospy.get_param('~target_engine_list', ''))
00086         target_language = rospy.get_param('~target_language', 'ja')
00087         target_files = list(rospy.get_param('~target_files', ''))
00088         self._wait_flag = rospy.get_param('~wait_flag', True)
00089 
00090         if len(target_engine_list) == 0:
00091             target_engine_list.append(target_engine)
00092 
00093         for engine in target_engine_list:
00094             if not rospy.is_shutdown():
00095                 for target_file in target_files:
00096                     rospy.loginfo(target_file)
00097                     filename = os.path.join(target_dir, target_file)
00098                     voice_data = self._read_input_data(filename)
00099                     if voice_data:
00100                         self._execute_case(voice_data, target_language, engine)
00101                     else:
00102                         rospy.logerror('invalid voice data')
00103 
00104         while self._wait_count > 0:
00105                 rospy.sleep(0.1)
00106 
00107 if __name__ == '__main__':
00108     rostest.rosrun('rospeex_if', 'rospeex_sr_test', TestRospeexSR, sys.argv)
00109 


rospeex_if
Author(s): Komei Sugiura
autogenerated on Wed Aug 26 2015 16:10:32