diff --git a/ros_web_api_bellande_2d_computer_vision/CMakeLists.txt b/ros_web_api_bellande_2d_computer_vision/CMakeLists.txt
index 764ae3c..23a4482 100644
--- a/ros_web_api_bellande_2d_computer_vision/CMakeLists.txt
+++ b/ros_web_api_bellande_2d_computer_vision/CMakeLists.txt
@@ -46,6 +46,7 @@ if($ENV{ROS_VERSION} EQUAL 1)
src/bellande_2d_computer_vision_prediction.py
src/bellande_2d_computer_vision_face_detection.py
src/bellande_2d_computer_vision_object_detection.py
+ src/bellande_2d_computer_vision_instance_segmentation.py
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)
endif()
@@ -55,12 +56,14 @@ 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(PROGRAMS src/bellande_2d_computer_vision_instance_segmentation.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(PROGRAMS src/bellande_2d_computer_vision_instance_segmentation.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_instance_segmentation.launch.py b/ros_web_api_bellande_2d_computer_vision/launch/bellande_2d_computer_vision_instance_segmentation.launch.py
new file mode 100644
index 0000000..1dc94fa
--- /dev/null
+++ b/ros_web_api_bellande_2d_computer_vision/launch/bellande_2d_computer_vision_instance_segmentation.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_face_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_instance_segmentation.launch b/ros_web_api_bellande_2d_computer_vision/launch/ros1/bellande_2d_computer_vision_instance_segmentation.launch
new file mode 100644
index 0000000..c9f2a51
--- /dev/null
+++ b/ros_web_api_bellande_2d_computer_vision/launch/ros1/bellande_2d_computer_vision_instance_segmentation.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 140f1b2..298274f 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', 'src/bellande_2d_computer_vision_object_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', 'src/bellande_2d_computer_vision_instance_segmentation.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_instance_segmentation.py b/ros_web_api_bellande_2d_computer_vision/src/bellande_2d_computer_vision_instance_segmentation.py
new file mode 100755
index 0000000..a1c4315
--- /dev/null
+++ b/ros_web_api_bellande_2d_computer_vision/src/bellande_2d_computer_vision_instance_segmentation.py
@@ -0,0 +1,109 @@
+# 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 instance_segmentation(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
+ }
+
+ 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 instance in result['instances']:
+ mask = np.array(instance['mask'], dtype=np.uint8)
+ color = np.random.randint(0, 255, 3).tolist()
+ cv_image = cv2.addWeighted(cv_image, 1, cv2.applyColorMap(mask, cv2.COLORMAP_JET), 0.5, 0)
+
+ 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 = instance_segmentation(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']["instance_segmentation"]
+ api_access_key = config["Bellande_Framework_Access_Key"]
+
+ if ros_version == "1":
+ rospy.init_node('instance_segmentation_node', anonymous=True)
+ pub = rospy.Publisher('instance_segmentation_result', Image, queue_size=10)
+ sub = rospy.Subscriber('camera/image_raw', Image, image_callback)
+ elif ros_version == "2":
+ rclpy.init()
+ node = rclpy.create_node('instance_segmentation_node')
+ pub = node.create_publisher(Image, 'instance_segmentation_result', 10)
+ sub = node.create_subscription(Image, 'camera/image_raw', image_callback, 10)
+
+ api_url = f"{url}{endpoint_path}"
+
+ try:
+ print("Instance segmentation 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 instance segmentation 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_object_detection.py b/ros_web_api_bellande_2d_computer_vision/src/bellande_2d_computer_vision_object_detection.py
index 387366b..0545bcf 100755
--- 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
@@ -32,8 +32,7 @@ def object_detection(image):
img_base64 = base64.b64encode(img_encoded).decode('utf-8')
payload = {
- "image": img_base64,
- "task": "object_detection"
+ "image": img_base64
}
headers = {