Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port Over Image Capture Script #39

Merged
merged 5 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 55 additions & 0 deletions scripts/image_capture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/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")
jbrhm marked this conversation as resolved.
Show resolved Hide resolved

if not os.path.exists(path):
os.mkdir(path)

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