latest pushes

This commit is contained in:
2024-07-25 21:54:17 -04:00
parent b78cce875e
commit f047386e47
26 changed files with 1057 additions and 44 deletions

View File

@@ -16,53 +16,78 @@
import json
import os
import requests
from std_msgs.msg import String
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
def speech_detection(audio_data, sample_rate, language):
payload = {
"audio_data": audio_data,
"sample_rate": sample_rate,
"language": language
}
# Headers
headers = {
"Authorization": f"Bearer {api_access_key}",
'accept': 'application/json',
'Content-Type': 'application/json'
}
response = requests.post(api_url, json=payload, headers=headers)
if response.status_code == 200:
result = response.json()
return result['detected_speech']
else:
print(f"Error: {response.status_code} - {response.text}")
return None
# 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'])
def audio_callback(msg):
sample_rate = rospy.get_param('sample_rate', 16000)
language = rospy.get_param('language', 'en-US')
except requests.exceptions.RequestException as e:
print("Error:", e)
detected_speech = speech_detection(msg.data, sample_rate, language)
if detected_speech is not None:
output_msg = String()
output_msg.data = detected_speech
pub.publish(output_msg)
def main():
global api_url, api_access_key, pub
config_file_path = os.path.join(os.path.dirname(__file__), '../config/configs.json')
if not os.path.exists(config_file_path):
print("Config file not found:", config_file_path)
return
with open(config_file_path, 'r') as config_file:
config = json.load(config_file)
url = config['url']
endpoint_path = config['endpoint_path']["speech_detection"]
api_access_key = config["Bellande_Framework_Access_Key"]
# Initialize ROS node
if ros_version == "1":
rospy.init_node('speech_detection_node', anonymous=True)
pub = rospy.Publisher('detected_speech', String, queue_size=10)
sub = rospy.Subscriber('audio_data', String, audio_callback)
elif ros_version == "2":
rclpy.init()
node = rclpy.create_node('speech_detection_node')
pub = node.create_publisher(String, 'detected_speech', 10)
sub = node.create_subscription(String, 'audio_data', audio_callback, 10)
api_url = f"{url}{endpoint_path}"
try:
print("Speech detection node is running. Ctrl+C to exit.")
if ros_version == "1":
rospy.spin()
elif ros_version == "2":
rclpy.spin(node)
except KeyboardInterrupt:
print("Shutting down speech detection node.")
except Exception as e:
print(f"An error occurred: {str(e)}")
finally:
if ros_version == "2":
node.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
ros_version = os.getenv("ROS_VERSION")