extract_head_cloud.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import roslib; roslib.load_manifest('head_pose_estimation')
00004 import rospy
00005 from sensor_msgs.msg import RegionOfInterest, Image, CameraInfo
00006 import message_filters
00007 from cv_bridge import CvBridge, CvBridgeError
00008 from geometry_msgs.msg import PolygonStamped, Point32, PointStamped
00009 import numpy as np
00010 import dynamic_reconfigure.client
00011 from threading import RLock
00012 import contextlib # get rid of this once PR2 moves to python 2.7
00013 from rospy.service import ServiceException
00014 
00015 class Extract(object):
00016         bridge = CvBridge()
00017 
00018         xfilter = dynamic_reconfigure.client.Client('xfilter')
00019         yfilter = dynamic_reconfigure.client.Client('yfilter')
00020         zfilter = dynamic_reconfigure.client.Client('zfilter')
00021         
00022         depth_im = None
00023         roi      = None
00024 
00025         image_lock = RLock()
00026         roi_lock   = RLock()
00027         
00028         K = []
00029 
00030         def __init__(self):
00031                 roi_sub   = rospy.Subscriber('roi', RegionOfInterest, self.roi_cb)
00032                 depth_sub = message_filters.Subscriber('depth_image', Image)
00033                 info_sub  = message_filters.Subscriber('camera_info', CameraInfo)
00034                 
00035                 ts = message_filters.TimeSynchronizer([depth_sub, info_sub], 10)
00036                 ts.registerCallback(self.depth_cb);
00037                 
00038                 r = rospy.Rate(10)
00039                 while not rospy.is_shutdown() and not self.roi and not self.depth_im:
00040                         r.sleep()
00041                 while not rospy.is_shutdown():
00042                         self.update_params()
00043                         r.sleep()
00044 
00045         def roi_cb(self, msg):
00046                 with self.roi_lock:
00047                         self.roi = msg
00048 
00049         def depth_cb(self, depth_msg, info_msg):
00050                 with self.image_lock:
00051                         self.depth_im = self.bridge.imgmsg_to_cv(depth_msg, '32FC1')
00052                         self.K = info_msg.K
00053 
00054         def update_params(self):
00055                 with contextlib.nested(self.roi_lock, self.image_lock):
00056                         if self.depth_im and self.roi:
00057                                 x = self.roi.x_offset
00058                                 y = self.roi.y_offset
00059                                 w = self.roi.width
00060                                 h = self.roi.height
00061                                 
00062                                 depth = self.depth_im[y+h/2,x+w/2]
00063 
00064                                 face_center_img = np.array((x+w/2.0, y+h/2))
00065                                 face_center_pt  = (face_center_img - (self.K[2], self.K[5])) / np.array([self.K[0], self.K[4]])
00066                                 face_center_depth = self.depth_im[face_center_img]
00067                                 
00068                                 try:
00069                                         self.xfilter.update_configuration({
00070                                                 'filter_field_name': 'x',
00071                                                 'filter_limit_min': face_center_pt[0]-0.2,
00072                                                 'filter_limit_max': face_center_pt[0]+0.2
00073                                         })
00074 
00075                                         self.yfilter.update_configuration({
00076                                                 'filter_field_name': 'y',
00077                                                 'filter_limit_min': face_center_pt[1]-0.2,
00078                                                 'filter_limit_max': face_center_pt[1]+0.2
00079                                         })
00080 
00081                                         self.zfilter.update_configuration({
00082                                                 'filter_field_name': 'z',
00083                                                 'filter_limit_min': depth-0.2,
00084                                                 'filter_limit_max': depth+0.2
00085                                         })
00086                                 except ServiceException:
00087                                         rospy.logwarn("Service exception with dynamic reconfigure")
00088                         
00089 
00090 if __name__ == '__main__':
00091         rospy.init_node('extract_head_cloud')
00092         Extract()
00093         rospy.spin()


head_pose_estimation
Author(s): Daniel Lazewatsky
autogenerated on Mon Oct 6 2014 00:23:14