This commit is contained in:
Ronaldson Bellande 2024-06-29 11:46:08 -04:00
parent 5daf137e8c
commit ddb67692c7
6 changed files with 257 additions and 4 deletions

View File

@ -45,6 +45,7 @@ if($ENV{ROS_VERSION} EQUAL 1)
PROGRAMS PROGRAMS
src/bellande_2d_computer_vision_prediction.py src/bellande_2d_computer_vision_prediction.py
src/bellande_2d_computer_vision_face_detection.py src/bellande_2d_computer_vision_face_detection.py
src/bellande_2d_computer_vision_object_detection.py
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
) )
endif() endif()
@ -53,11 +54,13 @@ endif()
if($ENV{ROS_VERSION} EQUAL "1") 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_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_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 config/ DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/config)
install(DIRECTORY launch/ DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/launch) install(DIRECTORY launch/ DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/launch)
else() else()
install(PROGRAMS src/bellande_2d_computer_vision_prediction.py DESTINATION lib/${PROJECT_NAME}) 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_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 config/ DESTINATION share/${PROJECT_NAME}/config)
install(DIRECTORY launch/ DESTINATION share/${PROJECT_NAME}/launch) install(DIRECTORY launch/ DESTINATION share/${PROJECT_NAME}/launch)
ament_package() ament_package()

View File

@ -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_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)

View File

@ -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="object_detection_node" pkg="ros_web_api_bellande_2d_computer_vision"
type="bellande_2d_computer_vision_object_detection.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>

View File

@ -18,7 +18,7 @@ from catkin_pkg.python_setup import generate_distutils_setup
# fetch values from package.xml # fetch values from package.xml
setup_args = generate_distutils_setup( 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'], packages=['ros_web_api_bellande_2d_computer_vision'],
package_dir={'': 'src'}, package_dir={'': 'src'},
) )

View File

@ -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 <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 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()

View File

@ -59,21 +59,19 @@ def image_callback(msg):
def main(): 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') config_file_path = os.path.join(os.path.dirname(__file__), '../config/configs.json')
if not os.path.exists(config_file_path): if not os.path.exists(config_file_path):
print("Config file not found:", config_file_path) print("Config file not found:", config_file_path)
return return
with open(config_file_path, 'r') as config_file: with open(config_file_path, 'r') as config_file:
config = json.load(config_file) config = json.load(config_file)
url = config['url'] url = config['url']
endpoint_path = config['endpoint_path']["prediction"] endpoint_path = config['endpoint_path']["prediction"]
api_access_key = config["Bellande_Framework_Access_Key"] api_access_key = config["Bellande_Framework_Access_Key"]
# Initialize ROS node
if ros_version == "1": if ros_version == "1":
rospy.init_node('prediction_node', anonymous=True) rospy.init_node('prediction_node', anonymous=True)
pub = rospy.Publisher('prediction_result', String, queue_size=10) pub = rospy.Publisher('prediction_result', String, queue_size=10)