cv
This commit is contained in:
		| @@ -47,6 +47,7 @@ if($ENV{ROS_VERSION} EQUAL 1) | ||||
|     src/bellande_2d_computer_vision_face_detection.py | ||||
|     src/bellande_2d_computer_vision_object_detection.py | ||||
|     src/bellande_2d_computer_vision_instance_segmentation.py | ||||
|     src/bellande_2d_computer_vision_semantic_segmentation.py | ||||
|     DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} | ||||
|   ) | ||||
| endif() | ||||
| @@ -57,6 +58,7 @@ if($ENV{ROS_VERSION} EQUAL "1") | ||||
|     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(PROGRAMS src/bellande_2d_computer_vision_semantic_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() | ||||
| @@ -64,6 +66,7 @@ else() | ||||
|     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(PROGRAMS src/bellande_2d_computer_vision_semantic_segmentation.py DESTINATION lib/${PROJECT_NAME}) | ||||
|     install(DIRECTORY config/ DESTINATION share/${PROJECT_NAME}/config) | ||||
|     install(DIRECTORY launch/ DESTINATION share/${PROJECT_NAME}/launch) | ||||
|     ament_package() | ||||
|   | ||||
| @@ -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 <https://www.gnu.org/licenses/>. | ||||
|  | ||||
| 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_semantic_segmentation.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_semantic_segmentation.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_semantic_segmentation.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) | ||||
| @@ -0,0 +1,36 @@ | ||||
| <?xml version="1.0"?> | ||||
| <!-- | ||||
| Copyright (C) 2024 Bellande Robotics Sensors Research Innovation Center, Ronaldson Bellande | ||||
|  | ||||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||||
| use this file except in compliance with the License. You may obtain a copy of | ||||
| the License at | ||||
|  | ||||
| http://www.apache.org/licenses/LICENSE-2.0 | ||||
|  | ||||
| Unless required by applicable law or agreed to in writing, software | ||||
| distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||||
| WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||||
| License for the specific language governing permissions and limitations under | ||||
| the License. | ||||
| --> | ||||
| <launch> | ||||
|   <node name="camera" pkg="usb_cam" type="usb_cam_node" output="screen"> | ||||
|     <param name="video_device" value="/dev/video0" /> | ||||
|     <param name="image_width" value="640" /> | ||||
|     <param name="image_height" value="480" /> | ||||
|     <param name="pixel_format" value="yuyv" /> | ||||
|     <param name="camera_frame_id" value="usb_cam" /> | ||||
|     <param name="io_method" value="mmap" /> | ||||
|   </node> | ||||
|  | ||||
|   <node name="face_detection_node" pkg="ros_web_api_bellande_2d_computer_vision" | ||||
|     type="bellande_2d_computer_vision_semantic_segmentation.py" | ||||
|     output="screen"> | ||||
|     <remap from="camera/image_raw" to="/usb_cam/image_raw" /> | ||||
|   </node> | ||||
|  | ||||
|   <node name="rviz" pkg="rviz" type="rviz" | ||||
|     args="-d $(find ros_web_api_bellande_2d_computer_vision)/rviz/visualization.rviz" /> | ||||
|  | ||||
| </launch> | ||||
| @@ -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', 'src/bellande_2d_computer_vision_instance_segmentation.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', 'src/bellande_2d_computer_vision_semantic_segmentation.py'], | ||||
|     packages=['ros_web_api_bellande_2d_computer_vision'], | ||||
|     package_dir={'': 'src'}, | ||||
| ) | ||||
|   | ||||
| @@ -0,0 +1,107 @@ | ||||
| # 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 <https://www.gnu.org/licenses/>. | ||||
|  | ||||
| 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 semantic_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() | ||||
|         segmentation_map = np.array(result['segmentation_map'], dtype=np.uint8) | ||||
|         colored_segmentation = cv2.applyColorMap(segmentation_map, cv2.COLORMAP_JET) | ||||
|         cv_image = cv2.addWeighted(cv_image, 0.7, colored_segmentation, 0.3, 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 = semantic_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']["vision_api"] | ||||
|         api_access_key = config["Bellande_Framework_Access_Key"] | ||||
|      | ||||
|     if ros_version == "1": | ||||
|         rospy.init_node('semantic_segmentation_node', anonymous=True) | ||||
|         pub = rospy.Publisher('semantic_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('semantic_segmentation_node') | ||||
|         pub = node.create_publisher(Image, 'semantic_segmentation_result', 10) | ||||
|         sub = node.create_subscription(Image, 'camera/image_raw', image_callback, 10) | ||||
|  | ||||
|     api_url = f"{url}{endpoint_path}" | ||||
|  | ||||
|     try: | ||||
|         print("Semantic 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 semantic 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() | ||||
		Reference in New Issue
	
	Block a user