pi_cam_node.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import rospy
00004 import picamera
00005 import picamera.array
00006 import cv2
00007 import sys
00008 from sensor_msgs.msg import Image
00009 from cv_bridge import CvBridge
00010 from cv_bridge import CvBridgeError
00011 
00012 
00013 def main():
00014     # Select camera for use.
00015     with picamera.PiCamera() as camera:
00016         with picamera.array.PiRGBArray(camera) as stream:
00017             # Initialize Node
00018             sys.stdout.write("Initializing 'pi_cam' node...")
00019             rospy.init_node("pi_cam")
00020             sys.stdout.write("done!\n")
00021 
00022             # Create Publisher to publish pi_cam images.
00023             sys.stdout.write("Creating Publisher...")
00024             pub = rospy.Publisher("/pi_cam/image_raw",
00025                                 Image,
00026                                 queue_size=307200)
00027             sys.stdout.write("done!\n")
00028 
00029             # Other important variables
00030             camera.resolution = (640, 480)
00031             bridge = CvBridge()
00032             rate = rospy.Rate(2)
00033 
00034             # Starting node
00035             sys.stdout.write("Starting node!\n")
00036             while not rospy.is_shutdown():
00037                 try:
00038                     # Capture and publish
00039                     camera.capture(stream, 'bgr', use_video_port=True)
00040                     image_message = bridge.cv2_to_imgmsg(
00041                         stream.array, encoding="bgr8")
00042                     pub.publish(image_message)
00043 
00044                     # Reset stream for next capture
00045                     stream.truncate(0)
00046                     rate.sleep()
00047 
00048                 except KeyboardInterrupt:
00049                     sys.exit("Closing node!\n")
00050 
00051 
00052 if __name__ == "__main__":
00053     try:
00054         main()
00055 
00056     except rospy.ROSInterruptException:
00057         pass
00058 
00059     finally:
00060         rospy.loginfo("Exiting 'pi_cam' node.")


pheeno_ros
Author(s): Zahi Kakish, Sean Wilson
autogenerated on Thu Jun 6 2019 21:26:16