From cae7436c46d786d94ed04ebe8c1d10fdb7b4afb6 Mon Sep 17 00:00:00 2001 From: RonaldsonBellande Date: Thu, 25 Jul 2024 18:47:34 -0400 Subject: [PATCH] speech --- .../CMakeLists.txt | 13 +++- .../config/configs.json | 16 ++++ .../bellande_speech_detection.launch.py | 71 ++++++++++++++++++ .../ros1/bellande_speech_detection.launch | 30 ++++++++ .../setup.py | 11 +++ .../src/bellande_speech_detection.py | 73 +++++++++++++++++++ 6 files changed, 212 insertions(+), 2 deletions(-) create mode 100644 ros_web_api_bellande_speech_detection/launch/bellande_speech_detection.launch.py create mode 100644 ros_web_api_bellande_speech_detection/launch/ros1/bellande_speech_detection.launch create mode 100755 ros_web_api_bellande_speech_detection/setup.py create mode 100755 ros_web_api_bellande_speech_detection/src/bellande_speech_detection.py diff --git a/ros_web_api_bellande_speech_detection/CMakeLists.txt b/ros_web_api_bellande_speech_detection/CMakeLists.txt index 8a55503..7b186fe 100644 --- a/ros_web_api_bellande_speech_detection/CMakeLists.txt +++ b/ros_web_api_bellande_speech_detection/CMakeLists.txt @@ -39,13 +39,22 @@ if($ENV{ROS_VERSION} EQUAL 1) ) endif() +# Install Python scripts for both ROS 1 +if($ENV{ROS_VERSION} EQUAL 1) + catkin_install_python( + PROGRAMS + src/bellande_speech_detection.py + DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} + ) +endif() + # Install Python scripts, configuration files, and launch files if($ENV{ROS_VERSION} EQUAL "1") - install(PROGRAMS src/bellande_2d_computer_vision_api_2d.py DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}) + install(PROGRAMS src/bellande_speech_detection.py DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}) install(DIRECTORY config/ DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/config) install(DIRECTORY launch/ DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/launch) elseif($ENV{ROS_VERSION} EQUAL "2") - install(PROGRAMS src/bellande_2d_computer_vision_api_2d.py DESTINATION lib/${PROJECT_NAME}) + install(PROGRAMS src/bellande_speech_detection.py DESTINATION lib/${PROJECT_NAME}) install(DIRECTORY config/ DESTINATION share/${PROJECT_NAME}/config) install(DIRECTORY launch/ DESTINATION share/${PROJECT_NAME}/launch) endif() diff --git a/ros_web_api_bellande_speech_detection/config/configs.json b/ros_web_api_bellande_speech_detection/config/configs.json index 3ebfee6..86b7497 100644 --- a/ros_web_api_bellande_speech_detection/config/configs.json +++ b/ros_web_api_bellande_speech_detection/config/configs.json @@ -1,4 +1,20 @@ { + "license": [ + "Copyright (C) 2024 Bellande Robotics Sensors Research Innovation Center, Ronaldson Bellande", + "This program is free software: you can redistribute it and/or modify", + "it under the terms of the GNU General Public License as published by", + "the Free Software Foundation, either version 3 of the License, or", + "(at your option) any later version.", + "", + "This program is distributed in the hope that it will be useful,", + "but WITHOUT ANY WARRANTY; without even the implied warranty of", + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the", + "GNU General Public License for more details.", + "", + "You should have received a copy of the GNU General Public License", + "along with this program. If not, see .", + "GNU General Public License v3.0 or later" + ], "url": "https://bellanderoboticssensorsresearchinnovationcenter-kot42qxp.b4a.run", "endpoint_path": { "prediction": "/api/Bellande_Speech_Detection/bellande_prediction", diff --git a/ros_web_api_bellande_speech_detection/launch/bellande_speech_detection.launch.py b/ros_web_api_bellande_speech_detection/launch/bellande_speech_detection.launch.py new file mode 100644 index 0000000..2b5d079 --- /dev/null +++ b/ros_web_api_bellande_speech_detection/launch/bellande_speech_detection.launch.py @@ -0,0 +1,71 @@ +# Copyright (C) 2024 Bellande Robotics Sensors Research Innovation Center, Ronaldson Bellande +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import os +import sys +import subprocess +from launch import LaunchDescription +from launch_ros.actions import Node +from launch.actions import DeclareLaunchArgument +from launch.substitutions import LaunchConfiguration + + +def ros1_launch_description(): + # Get command-line arguments + args = sys.argv[1:] + # Construct the ROS 1 launch command + roslaunch_command = ["roslaunch", "ros_web_api_bellande_speech_detection", "bellande_speech_detection.launch"] + args + # Execute the launch command + subprocess.call(roslaunch_command) + + +def ros2_launch_description(): + # Declare launch arguments + audio_data_arg = DeclareLaunchArgument('audio_data') + sample_rate_arg = DeclareLaunchArgument('sample_rate') + language_arg = DeclareLaunchArgument('language') + + # Create a list to hold all nodes to be launched + nodes_to_launch = [] + + # ROS2 specific configurations + ros_launch_arguments = [ + audio_data_arg, sample_rate_arg, language_arg, + ] + + nodes_to_launch.append(Node( + package='ros_web_api_bellande_speech_detection', + executable='bellande_speech_detection.py', + name='bellande_speech_detection_node', + output='screen', + parameters=[ + {'audio_data': LaunchConfiguration('audio_data')}, + {'sample_rate': LaunchConfiguration('sample_rate')}, + {'language': LaunchConfiguration('language')}, + ], + )) + + # Return the LaunchDescription containing all nodes and arguments + return LaunchDescription(ros_launch_arguments + nodes_to_launch) + +if __name__ == "__main__": + ros_version = os.getenv("ROS_VERSION") + if ros_version == "1": + ros1_launch_description() + elif ros_version == "2": + ros2_launch_description() + else: + print("Unsupported ROS version. Please set the ROS_VERSION environment variable to '1' for ROS 1 or '2' for ROS 2.") + sys.exit(1) diff --git a/ros_web_api_bellande_speech_detection/launch/ros1/bellande_speech_detection.launch b/ros_web_api_bellande_speech_detection/launch/ros1/bellande_speech_detection.launch new file mode 100644 index 0000000..7c0eea3 --- /dev/null +++ b/ros_web_api_bellande_speech_detection/launch/ros1/bellande_speech_detection.launch @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + diff --git a/ros_web_api_bellande_speech_detection/setup.py b/ros_web_api_bellande_speech_detection/setup.py new file mode 100755 index 0000000..283e16c --- /dev/null +++ b/ros_web_api_bellande_speech_detection/setup.py @@ -0,0 +1,11 @@ +from distutils.core import setup +from catkin_pkg.python_setup import generate_distutils_setup + +# fetch values from package.xml +setup_args = generate_distutils_setup( + scripts=['src/bellande_speech_detection.py'], + packages=['ros_web_api_bellande_speech_detection'], + package_dir={'': 'src'}, +) + +setup(**setup_args) diff --git a/ros_web_api_bellande_speech_detection/src/bellande_speech_detection.py b/ros_web_api_bellande_speech_detection/src/bellande_speech_detection.py new file mode 100755 index 0000000..3389fee --- /dev/null +++ b/ros_web_api_bellande_speech_detection/src/bellande_speech_detection.py @@ -0,0 +1,73 @@ +# Copyright (C) 2024 Bellande Robotics Sensors Research Innovation Center, Ronaldson Bellande +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import json +import os +import requests + +def main(): + # Get the absolute path to the config file + config_file_path = os.path.join(os.path.dirname(__file__), '../config/configs.json') + + # Check if the config file exists + if not os.path.exists(config_file_path): + print("Config file not found:", config_file_path) + return + + # Read configuration from config.json + with open(config_file_path, 'r') as config_file: + config = json.load(config_file) + url = config['url'] + endpoint_path = config['endpoint_path']["detection"] + + # Get the parameters from the ROS parameter server + audio_data = rospy.get_param('audio_data', '') + sample_rate = rospy.get_param('sample_rate', 16000) + language = rospy.get_param('language', 'en-US') + + # JSON payload + payload = { + "audio_data": audio_data, + "sample_rate": sample_rate, + "language": language + } + + # Headers + headers = { + 'accept': 'application/json', + 'Content-Type': 'application/json' + } + + # Make POST request + try: + response = requests.post( + url + endpoint_path, + json=payload, + headers=headers + ) + response.raise_for_status() # Raise an error for unsuccessful responses + data = response.json() + print("Detected Speech:", data['detected_speech']) + + except requests.exceptions.RequestException as e: + print("Error:", e) + +if __name__ == '__main__': + ros_version = os.getenv("ROS_VERSION") + if ros_version == "1": + import rospy + elif ros_version == "2": + import rclpy + main()