diff --git a/ros_web_api_bellande_2d_computer_vision/CMakeLists.txt b/ros_web_api_bellande_2d_computer_vision/CMakeLists.txt index c120fe1..764ae3c 100644 --- a/ros_web_api_bellande_2d_computer_vision/CMakeLists.txt +++ b/ros_web_api_bellande_2d_computer_vision/CMakeLists.txt @@ -45,6 +45,7 @@ if($ENV{ROS_VERSION} EQUAL 1) PROGRAMS src/bellande_2d_computer_vision_prediction.py src/bellande_2d_computer_vision_face_detection.py + src/bellande_2d_computer_vision_object_detection.py DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} ) endif() @@ -53,11 +54,13 @@ endif() if($ENV{ROS_VERSION} EQUAL "1") install(PROGRAMS src/bellande_2d_computer_vision_prediction.py DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}) install(PROGRAMS src/bellande_2d_computer_vision_face_detection.py DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}) + install(PROGRAMS src/bellande_2d_computer_vision_object_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) else() install(PROGRAMS src/bellande_2d_computer_vision_prediction.py DESTINATION lib/${PROJECT_NAME}) install(PROGRAMS src/bellande_2d_computer_vision_face_detection.py DESTINATION lib/${PROJECT_NAME}) + install(PROGRAMS src/bellande_2d_computer_vision_object_detection.py DESTINATION lib/${PROJECT_NAME}) install(DIRECTORY config/ DESTINATION share/${PROJECT_NAME}/config) install(DIRECTORY launch/ DESTINATION share/${PROJECT_NAME}/launch) ament_package() diff --git a/ros_web_api_bellande_2d_computer_vision/launch/bellande_2d_computer_vision_object_detection.launch.py b/ros_web_api_bellande_2d_computer_vision/launch/bellande_2d_computer_vision_object_detection.launch.py new file mode 100644 index 0000000..16ae336 --- /dev/null +++ b/ros_web_api_bellande_2d_computer_vision/launch/bellande_2d_computer_vision_object_detection.launch.py @@ -0,0 +1,101 @@ +# 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 commandi + roslaunch_command = ["roslaunch", "ros_web_api_bellande_2d_computer_vision", "bellande_2d_computer_vision_object_detection.launch"] + args + + roslaunch_command.extend([ + "usb_cam", "usb_cam_node", "name:=camera", + "video_device:=/dev/video0", + "image_width:=640", + "image_height:=480", + "pixel_format:=yuyv", + "camera_frame_id:=usb_cam", + "io_method:=mmap" + ]) + + roslaunch_command.extend([ + "ros_web_api_bellande_2d_computer_vision", "bellande_2d_computer_vision_face_detection.py", "name:=face_detection_node" + ]) + + roslaunch_command.extend([ + "rviz", "rviz", "name:=rviz", + "args:=-d $(find ros_web_api_bellande_2d_computer_vision)/rviz/visualization.rviz" + ]) + + # Execute the launch command + subprocess.call(roslaunch_command) + + + +def ros2_launch_description(): + nodes_to_launch = [] + + nodes_to_launch.append(Node( + package='usb_cam', + executable='usb_cam_node', + name='camera', + output='screen', + parameters=[{ + 'video_device': '/dev/video0', + 'image_width': 640, + 'image_height': 480, + 'pixel_format': 'yuyv', + 'camera_frame_id': 'usb_cam', + 'io_method': 'mmap' + }] + )) + + nodes_to_launch.append(Node( + package='ros_web_api_bellande_2d_computer_vision', + executable='bellande_2d_computer_vision_face_detection.py', + name='face_detection_node', + output='screen', + remappings=[('camera/image_raw', '/usb_cam/image_raw')] + )) + + nodes_to_launch.append(Node( + package='rviz2', + executable='rviz2', + name='rviz', + arguments=['-d', '$(find ros_web_api_bellande_2d_computer_vision)/rviz/visualization.rviz'] + )) + + return LaunchDescription(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_2d_computer_vision/launch/ros1/bellande_2d_computer_vision_object_detection.launch b/ros_web_api_bellande_2d_computer_vision/launch/ros1/bellande_2d_computer_vision_object_detection.launch new file mode 100644 index 0000000..81b6a59 --- /dev/null +++ b/ros_web_api_bellande_2d_computer_vision/launch/ros1/bellande_2d_computer_vision_object_detection.launch @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + diff --git a/ros_web_api_bellande_2d_computer_vision/setup.py b/ros_web_api_bellande_2d_computer_vision/setup.py index 5d671f5..140f1b2 100755 --- a/ros_web_api_bellande_2d_computer_vision/setup.py +++ b/ros_web_api_bellande_2d_computer_vision/setup.py @@ -18,7 +18,7 @@ from catkin_pkg.python_setup import generate_distutils_setup # fetch values from package.xml setup_args = generate_distutils_setup( - scripts=['src/bellande_2d_computer_vision_prediction.py', 'src/bellande_2d_computer_vision_face_detection.py'], + scripts=['src/bellande_2d_computer_vision_prediction.py', 'src/bellande_2d_computer_vision_face_detection.py', 'src/bellande_2d_computer_vision_object_detection.py'], packages=['ros_web_api_bellande_2d_computer_vision'], package_dir={'': 'src'}, ) diff --git a/ros_web_api_bellande_2d_computer_vision/src/bellande_2d_computer_vision_object_detection.py b/ros_web_api_bellande_2d_computer_vision/src/bellande_2d_computer_vision_object_detection.py new file mode 100755 index 0000000..387366b --- /dev/null +++ b/ros_web_api_bellande_2d_computer_vision/src/bellande_2d_computer_vision_object_detection.py @@ -0,0 +1,115 @@ +# 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 +import cv2 +import numpy as np +import base64 +from sensor_msgs.msg import Image +from cv_bridge import CvBridge + + + +def object_detection(image): + bridge = CvBridge() + cv_image = bridge.imgmsg_to_cv2(image, desired_encoding="bgr8") + + _, img_encoded = cv2.imencode('.jpg', cv_image) + img_base64 = base64.b64encode(img_encoded).decode('utf-8') + + payload = { + "image": img_base64, + "task": "object_detection" + } + + headers = { + "Authorization": f"Bearer {api_access_key}" + } + + response = requests.post(api_url, json=payload, headers=headers) + + if response.status_code == 200: + result = response.json() + for obj in result['objects']: + label = obj['label'] + confidence = obj['confidence'] + x, y, w, h = obj['bbox'] + cv2.rectangle(cv_image, (x, y), (x+w, y+h), (0, 255, 0), 2) + cv2.putText(cv_image, f"{label}: {confidence:.2f}", (x, y-10), + cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2) + + return bridge.cv2_to_imgmsg(cv_image, encoding="bgr8") + else: + print(f"Error: {response.status_code} - {response.text}") + return None + + + +def image_callback(msg): + processed_img = object_detection(msg) + if processed_img: + pub.publish(processed_img) + + + +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']["object_detection"] + api_access_key = config["Bellande_Framework_Access_Key"] + + if ros_version == "1": + rospy.init_node('object_detection_node', anonymous=True) + pub = rospy.Publisher('object_detection_result', Image, queue_size=10) + sub = rospy.Subscriber('camera/image_raw', Image, image_callback) + elif ros_version == "2": + rclpy.init() + node = rclpy.create_node('object_detection_node') + pub = node.create_publisher(Image, 'object_detection_result', 10) + sub = node.create_subscription(Image, 'camera/image_raw', image_callback, 10) + + api_url = f"{url}{endpoint_path}" + + try: + print("Object 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 object 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") + if ros_version == "1": + import rospy + elif ros_version == "2": + import rclpy + main() diff --git a/ros_web_api_bellande_2d_computer_vision/src/bellande_2d_computer_vision_prediction.py b/ros_web_api_bellande_2d_computer_vision/src/bellande_2d_computer_vision_prediction.py index 77f5580..1562019 100755 --- a/ros_web_api_bellande_2d_computer_vision/src/bellande_2d_computer_vision_prediction.py +++ b/ros_web_api_bellande_2d_computer_vision/src/bellande_2d_computer_vision_prediction.py @@ -59,21 +59,19 @@ def image_callback(msg): def main(): - global api_url, pub + 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']["prediction"] api_access_key = config["Bellande_Framework_Access_Key"] - # Initialize ROS node if ros_version == "1": rospy.init_node('prediction_node', anonymous=True) pub = rospy.Publisher('prediction_result', String, queue_size=10)