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 all 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
65 changes: 65 additions & 0 deletions scripts/image_capture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/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
from pathlib import Path
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 = (
Path(get_package_share_directory("mrover"))
/ ".."
/ ".."
/ ".."
/ ".."
/ "src"
/ "mrover"
/ "data"
/ "images"
)

if not path.exists():
path.mkdir(parents=True, exist_ok=True)

path = path / f"image_{unique_id}.jpg"

cv2.imwrite(str(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()