29 from speech_recognition_sphinx4
import *
31 from rapp_utilities
import RappUtilities
34 from rapp_platform_ros_communications.srv
import (
35 SpeechRecognitionSphinx4TotalSrv,
36 SpeechRecognitionSphinx4TotalSrvResponse
54 rospy.get_param(
"rapp_speech_detection_sphinx4_threads")
56 if rospy.get_param(
"rapp_speech_detection_sphinx4_preconfigure"):
63 'configuration_hash': 0\
66 proc[
'configuration_hash'] = proc[
'sphinx'].getConfigurationHash()
71 'configuration_hash': 0\
75 self.
_lock = threading.Condition()
80 rospy.get_param(
"rapp_speech_detection_sphinx4_total_topic")
81 if(
not serv_batch_topic):
82 rospy.logerror(
"Sphinx4 Speech detection batch topic param not found")
86 serv_batch_topic, SpeechRecognitionSphinx4TotalSrv, \
99 RappUtilities.rapp_print(
'Fetcing preconfiguration names' )
102 rospy.get_param(
"rapp_speech_detection_sphinx4_preconfigure_number")
106 RappUtilities.rapp_print(
"Sphinx preconfigurations requested exceed " + \
107 "Sphinx processes. Truncating",
'WARN')
111 if rospy.has_param(
"rapp_speech_detection_sphinx4_preconfiguration"):
113 rospy.get_param(
"rapp_speech_detection_sphinx4_preconfiguration")
114 uniqueConfigurations = len( confDict )
115 if uniqueConfigurations > confNumber:
116 uniqueConfigurations = confNumber
118 RappUtilities.rapp_print(
"Preconfigurations requested, but none was " + \
124 for confIter
in range(confNumber):
125 preconf.append(confDict[ str(confIter % uniqueConfigurations) ])
127 for it
in range(self.
_threads - confNumber):
130 RappUtilities.rapp_print(str(preconf),
'DEBUG')
140 RappUtilities.rapp_print(
"Received service request",
'DEBUG')
141 res = SpeechRecognitionSphinx4TotalSrvResponse()
152 if proc[
'running'] ==
False and \
153 proc[
'configuration_hash'] == request_hash:
155 RappUtilities.rapp_print(
"Found Sphinx process with same configuration",\
157 proc[
'running'] =
True
159 res = proc[
'sphinx'].speechRecognitionBatch( req )
162 proc[
'running'] =
False
172 if proc[
'running'] ==
False:
174 proc[
'configuration_hash'] = request_hash
175 proc[
'running'] =
True
177 RappUtilities.rapp_print(
"Found Sphinx process",
'DEBUG')
179 res = proc[
'sphinx'].speechRecognitionBatch( req )
182 proc[
'running'] =
False
199 hash_object = hashlib.sha1()
200 hash_object.update( req.language )
201 for word
in req.words:
202 hash_object.update( word )
203 for gram
in req.grammar:
204 hash_object.update( gram )
205 for sent
in req.sentences:
206 hash_object.update( sent )
207 return hash_object.hexdigest()
210 if __name__ ==
"__main__":
211 rospy.init_node(
'SpeechRecognitionSphinx4')
213 rospack = rospkg.RosPack()
214 sphinx_class = rospack.get_path(
'rapp_speech_detection_sphinx4') + \
217 if not os.path.isfile(sphinx_class):
218 rospy.logerr(
"speech_recognition_sphinx4_handler_node: Sphinx.class file is missing. You can execute 'buildJava.sh'")
221 RappUtilities.rapp_print(
"Sphinx4 Handler node initialized",
'DEBUG')
def __init__
Initializes the subprocesses and the services (constructor)
_threadCounter
Total service callback threads waiting to execute.
_speech_recognition_batch_service
Ros service server for sphinx speech recognition.
def handleSpeechRecognitionCallback
The callback to perform speech recognition.
def _getPreconfigurationNames
Specifies the requested preconfiguration names.
Provides a complete Rapp Sphinx Entity.
_availableProcesses
The subprocesses structure that contains information used for the subprocess handling.
_lock
Thread conditional variable used for the subprocess scheduling.
def _calculateRequestHash
Calculates the service request sha1 hash for process handling purposes.
_threads
The number of child subprocesses.
Maintains Sphinx instances to perform speech recognition.