color_controller.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import rospy
00004 import tf
00005 
00006 from sensor_msgs.msg import Image, CameraInfo 
00007 from image_geometry import PinholeCameraModel
00008 from cmvision.msg import Blobs
00009 from cmvision_3d.msg import Blobs3d, Blob3d
00010 
00011 from cv_bridge import CvBridge
00012 
00013 import cv, cv2
00014 import numpy as np
00015 from color_model import color_model
00016 
00017 #This package integrates cmvision with tf and localization; now we can track color in 3D.
00018 class color_controller():
00019         def __init__(self, publish_tf):
00020                 #To take our Ros Image into a cv message and subsequently a numpy array.
00021                 self.bridge = CvBridge()        
00022 
00023                 # To make the pixel to vector projection
00024                 self.cam_model = PinholeCameraModel()
00025 
00026                 #We need CameraInfo in order to use PinholeCameraModel below.
00027                 rospy.Subscriber("camera_topic", CameraInfo, self.camera_callback)
00028                 self.hasCameraInfo = False
00029 
00030                 while not self.hasCameraInfo:
00031                         print "waiting on camera info."
00032                         rospy.sleep(0.5)
00033 
00034                 #We are using a depth image to get depth information of what we're tracking.
00035                 rospy.Subscriber("depth_image", Image, self.depth_callback)
00036 
00037                 #This package is just an extension of cmvision to provide tf tracking of the blobs provided by cmvision. 
00038                 rospy.Subscriber('blobs', Blobs, self.blob_callback)
00039 
00040                 #Subscribe to image for debugging.
00041                 # rospy.Subscriber('thing', Image, self.image_callback)
00042                 self.listener = tf.TransformListener()
00043                 self.broadcaster = tf.TransformBroadcaster()
00044 
00045                 #Republish each blob as part of a blob.
00046                 self.blob_pub = rospy.Publisher('/blobs_3d', Blobs3d)
00047 
00048                 self.publish_tf = publish_tf
00049 
00050 
00051 
00052                 #blobs is received from running cmvision. It's color blobs as defined by our color file.
00053         def blob_callback(self, blobs):
00054                 #array of our color_models.
00055                 self.colors = {}
00056                 blobs3d = Blobs3d()
00057 
00058                 for blob in blobs.blobs:
00059                         #If we have multiple catches of a single color, we only want to publish the one with the maximum area. Fortunately they are sorted as such.
00060                         already_published = False
00061                         if blob.name in self.colors:
00062                                 already_published = True
00063 
00064                         self.colors[blob.name] = color_model(blob, self.camera_info, self.parent_frame, self.depth_image, self.cam_model, self.listener, self.broadcaster)
00065                         
00066                         #Make sure this blob isn't shitty.
00067                         if self.colors[blob.name].validate():
00068 
00069                                 #Publish to tf if master wishes it upon Dobby the programming elf.
00070                                 if self.publish_tf and not already_published:
00071                                         self.colors[blob.name].publish()
00072 
00073                                 #3d blobs that are in this blobs3d list are published to the /blobs_3d topic. 
00074                                 blobs3d.blobs.append(self.colors[blob.name].toBlob3d())
00075 
00076                 blobs3d.header.frame_id = self.parent_frame
00077                 blobs3d.header.stamp = rospy.Time.now() 
00078                 blobs3d.blob_count = len(blobs3d.blobs) 
00079                 self.blob_pub.publish(blobs3d) 
00080 
00081 
00082         def depth_callback(self, image):
00083                 image_cv = self.bridge.imgmsg_to_cv(image, image.encoding)
00084                 image_cv2 = np.array(image_cv, dtype=np.float32)
00085                 self.depth_image = image_cv2
00086 
00087 
00088         def camera_callback(self, camera_info):
00089                 if not self.hasCameraInfo:
00090                         self.cam_model.fromCameraInfo(camera_info)
00091                         self.camera_info = camera_info
00092                         self.parent_frame = self.camera_info.header.frame_id
00093                 self.hasCameraInfo = True
00094                 
00095 if __name__ == '__main__':
00096         rospy.init_node('color_controller')
00097         
00098         publish_tf = rospy.get_param('color_controller/publish_tf', True)
00099         my_controller = color_controller(publish_tf)
00100 
00101         rospy.spin()


cmvision_3d
Author(s): Alex Hubers
autogenerated on Fri Aug 28 2015 10:20:34