Browse Source

Created the capture_image file for the example 7 tutorial for ROS2

humble
Jesus Eduardo Rodriguez 1 year ago
committed by GitHub
parent
commit
2a96244ac0
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 52 additions and 0 deletions
  1. +52
    -0
      stretch_ros_tutorials/capture_image.py

+ 52
- 0
stretch_ros_tutorials/capture_image.py View File

@ -0,0 +1,52 @@
#!/usr/bin/env python3
import rclpy
import sys
import os
import cv2
from rclpy.node import Node
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
class CaptureImage(Node):
"""
A class that converts a subscribed ROS image to a OpenCV image and saves
the captured image to a predefined directory.
"""
def __init__(self):
"""
A function that initializes a CvBridge class, subscriber, and save path.
:param self: The self reference.
"""
super().__init__('stretch_capture_image')
self.bridge = CvBridge()
self.sub = self.create_subscription(Image, '/camera/color/image_raw', self.image_callback, 10)
self.save_path = '/home/hello-robot/ament_ws/src/stretch_tutorials/stored_data'
self.br = CvBridge()
def image_callback(self, msg, data):
"""
A callback function that converts the ROS image to a CV2 image and stores the
image.
:param self: The self reference.
:param msg: The ROS image message type.
"""
try:
image = self.bridge.imgmsg_to_cv2(msg, 'bgr8')
except CvBridgeError as e:
self.get_logger().warn('CV Bridge error: {0}'.format(e))
file_name = 'camera_image.jpeg'
completeName = os.path.join(self.save_path, file_name)
cv2.imwrite(completeName, image)
rclpy.shutdown()
sys.exit(0)
def main(args=None):
rclpy.init(args=args)
capture_image = CaptureImage()
rclpy.spin(capture_image)
if __name__ == '__main__':
main()

Loading…
Cancel
Save