5 Written by: Zahi Kakish (zmk5) 13 from sensor_msgs.msg
import Image
14 from cv_bridge
import CvBridge
19 Sample Pi Camera ROS Node 21 This simple ROS node script takes images from a Pi Camera attached to a 22 Pheeno's Raspberry Pi (or other Robot that uses the Pi Camera) and 23 Publishes the images as a ROS topic. 25 NOTE: This method of publishing images is very computationally intensive, 26 especially on a Raspberry Pi. Use when needed or try to do image processing 27 within a node and only publish the results. For example, checking if a 28 color is located within an image and then publishing True or False instead 33 with picamera.PiCamera()
as camera:
34 with picamera.array.PiRGBArray(camera)
as stream:
36 sys.stdout.write(
"Initializing 'pi_cam' node...")
37 rospy.init_node(
"pi_cam")
38 sys.stdout.write(
"done!\n")
41 sys.stdout.write(
"Creating Publisher...")
42 pub = rospy.Publisher(
"/pi_cam/image_raw",
45 sys.stdout.write(
"done!\n")
48 camera.resolution = (640, 480)
53 sys.stdout.write(
"Starting node!\n")
54 while not rospy.is_shutdown():
57 camera.capture(stream,
'bgr', use_video_port=
True)
58 image_message = bridge.cv2_to_imgmsg(
59 stream.array, encoding=
"bgr8")
60 pub.publish(image_message)
66 except KeyboardInterrupt:
67 sys.exit(
"Closing node!\n")
71 if __name__ ==
"__main__":
75 except rospy.ROSInterruptException:
79 rospy.loginfo(
"Exiting 'pi_cam' node.")