Created
December 30, 2018 14:52
-
-
Save lamhoangtung/ba2620a3def328cdb6c576d613a42066 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import rospy | |
# ROS Image message | |
from sensor_msgs.msg import CompressedImage | |
# ROS Image message -> OpenCV2 image converter | |
from cv_bridge import CvBridge, CvBridgeError | |
# OpenCV2 for saving an image | |
import cv2 | |
# Instantiate CvBridge | |
bridge = CvBridge() | |
def image_callback(unity_image): | |
print("Received an image!") | |
try: | |
# Convert your ROS Image message to OpenCV2 | |
cv2_img = bridge.imgmsg_to_cv2(unity_image) | |
except CvBridgeError as e: | |
print(e) | |
else: | |
# Save your OpenCV2 image as a jpeg | |
# cv2.imwrite('camera_image.jpeg', cv2_img) | |
cv2.imshow('Camera_feed', cv2_img) | |
def main(): | |
rospy.init_node('python_image_subscriber', anonymous=True) | |
# Set up your subscriber and define its callback | |
rospy.Subscriber("/Team1_image/compressed", CompressedImage, image_callback) | |
# Spin until ctrl + c | |
rospy.spin() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment