From 5d63cfe11493c6717452f2044f3abef79e871539 Mon Sep 17 00:00:00 2001 From: jbrhm Date: Mon, 4 Nov 2024 18:46:21 -0500 Subject: [PATCH 1/5] Working Image Capturer --- CMakeLists.txt | 1 + scripts/image_capture.py | 50 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100755 scripts/image_capture.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 8c9e02fe..5ecc2c6a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -320,6 +320,7 @@ install(PROGRAMS superstructure/superstructure.py scripts/debug_course_publisher.py scripts/visualizer.py + scripts/image_capture.py # starter project sources starter_project/autonomy/src/localization.py diff --git a/scripts/image_capture.py b/scripts/image_capture.py new file mode 100755 index 00000000..e61455a2 --- /dev/null +++ b/scripts/image_capture.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 + +import rclpy +from rclpy.node import Node +from sensor_msgs.msg._image import Image +from ament_index_python import get_package_share_directory + +import numpy as np +import sys +import os +import datetime + +import cv2 + +from rclpy.executors import ExternalShutdownException + +class ImageCapture(Node): + def __init__(self) -> None: + super().__init__("image_capture") + + self.image_sub = self.create_subscription(Image, "/zed/left/image", self.save_image, 1) + + def save_image(self, msg: Image): + img = np.frombuffer(msg.data, dtype=np.uint8).reshape(msg.height, msg.width, -1) + unique_id = "{date:%Y-%m-%d_%H:%M:%S}".format(date=datetime.datetime.now()) + + path = os.path.join(get_package_share_directory("mrover"), "../../../../src/mrover/data/images") + + if not os.path.exists(path): + os.mkdir(path) + + path = os.path.join(path, f'image_{unique_id}.jpg') + + cv2.imwrite(path, img) + pass + +def main() -> None: + try: + rclpy.init(args=sys.argv) + while(True): + input() + rclpy.spin_once(ImageCapture()) + rclpy.shutdown() + except KeyboardInterrupt: + pass + except ExternalShutdownException: + sys.exit(1) + +if __name__ == '__main__': + main() From c2218496b3cc819e38a975cf97fd2206708ad42f Mon Sep 17 00:00:00 2001 From: jbrhm Date: Wed, 6 Nov 2024 22:59:51 -0500 Subject: [PATCH 2/5] Better Logging --- scripts/image_capture.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/image_capture.py b/scripts/image_capture.py index e61455a2..3cf7ff19 100755 --- a/scripts/image_capture.py +++ b/scripts/image_capture.py @@ -32,6 +32,8 @@ def save_image(self, msg: Image): path = os.path.join(path, f'image_{unique_id}.jpg') cv2.imwrite(path, img) + + self.get_logger().info(f'Saved image_{unique_id}.jpg') pass def main() -> None: From ad8d0647a486b67cb1d4e451a3da8061b8f45b62 Mon Sep 17 00:00:00 2001 From: jbrhm Date: Wed, 6 Nov 2024 23:11:50 -0500 Subject: [PATCH 3/5] style --- scripts/image_capture.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/scripts/image_capture.py b/scripts/image_capture.py index 3cf7ff19..424f88ca 100755 --- a/scripts/image_capture.py +++ b/scripts/image_capture.py @@ -14,6 +14,7 @@ from rclpy.executors import ExternalShutdownException + class ImageCapture(Node): def __init__(self) -> None: super().__init__("image_capture") @@ -29,17 +30,18 @@ def save_image(self, msg: Image): if not os.path.exists(path): os.mkdir(path) - path = os.path.join(path, f'image_{unique_id}.jpg') + path = os.path.join(path, f"image_{unique_id}.jpg") cv2.imwrite(path, img) - self.get_logger().info(f'Saved image_{unique_id}.jpg') + self.get_logger().info(f"Saved image_{unique_id}.jpg") pass + def main() -> None: try: rclpy.init(args=sys.argv) - while(True): + while True: input() rclpy.spin_once(ImageCapture()) rclpy.shutdown() @@ -48,5 +50,6 @@ def main() -> None: except ExternalShutdownException: sys.exit(1) -if __name__ == '__main__': + +if __name__ == "__main__": main() From 1db5656cf10cac107df8c04bfe9a3767e7440c23 Mon Sep 17 00:00:00 2001 From: jbrhm Date: Sun, 10 Nov 2024 07:23:52 -0500 Subject: [PATCH 4/5] pathlib --- scripts/image_capture.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/scripts/image_capture.py b/scripts/image_capture.py index 424f88ca..046482f9 100755 --- a/scripts/image_capture.py +++ b/scripts/image_capture.py @@ -7,7 +7,7 @@ import numpy as np import sys -import os +from pathlib import Path import datetime import cv2 @@ -25,14 +25,14 @@ def save_image(self, msg: Image): img = np.frombuffer(msg.data, dtype=np.uint8).reshape(msg.height, msg.width, -1) unique_id = "{date:%Y-%m-%d_%H:%M:%S}".format(date=datetime.datetime.now()) - path = os.path.join(get_package_share_directory("mrover"), "../../../../src/mrover/data/images") + path = Path(get_package_share_directory("mrover")) / ".."/ ".." / ".." / ".." / "src" / "mrover" / "data" / "images" - if not os.path.exists(path): - os.mkdir(path) + if not path.exists(): + path.mkdir(parents=True, exist_ok=True) - path = os.path.join(path, f"image_{unique_id}.jpg") + path = path / f"image_{unique_id}.jpg" - cv2.imwrite(path, img) + cv2.imwrite(str(path), img) self.get_logger().info(f"Saved image_{unique_id}.jpg") pass From 5af901869a3eae7511bc083cfa34e998407254b1 Mon Sep 17 00:00:00 2001 From: jbrhm Date: Sun, 10 Nov 2024 07:28:23 -0500 Subject: [PATCH 5/5] style --- scripts/image_capture.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/scripts/image_capture.py b/scripts/image_capture.py index 046482f9..97fec048 100755 --- a/scripts/image_capture.py +++ b/scripts/image_capture.py @@ -25,7 +25,17 @@ def save_image(self, msg: Image): img = np.frombuffer(msg.data, dtype=np.uint8).reshape(msg.height, msg.width, -1) unique_id = "{date:%Y-%m-%d_%H:%M:%S}".format(date=datetime.datetime.now()) - path = Path(get_package_share_directory("mrover")) / ".."/ ".." / ".." / ".." / "src" / "mrover" / "data" / "images" + path = ( + Path(get_package_share_directory("mrover")) + / ".." + / ".." + / ".." + / ".." + / "src" + / "mrover" + / "data" + / "images" + ) if not path.exists(): path.mkdir(parents=True, exist_ok=True)