Go to the documentation of this file.00001
00002 import roslib; roslib.load_manifest('projector_interface')
00003 import rospy
00004 from sensor_msgs.msg import CameraInfo, Image
00005 from geometry_msgs.msg import Point
00006 import image_geometry
00007 from pr2_python import transform_listener
00008
00009 import cv2
00010 import numpy as np
00011 import sys
00012 import tf
00013 from cv_bridge import CvBridge, CvBridgeError
00014 from pr2_python.head import Head
00015
00016 import PySide
00017 from PySide import QtGui, QtCore
00018 from PySide.QtGui import QPalette
00019 from PySide import QtOpenGL
00020
00021
00022 class Gradient(QtGui.QWidget):
00023 image = None
00024 model = None
00025
00026 def keyPressEvent(self, e):
00027 if 16777216 == e.key():
00028 sys.exit(0)
00029
00030 def initUI(self):
00031 self.showFullScreen()
00032
00033 def paintEvent(self, e):
00034 p = QPalette()
00035 print self.width(), self.height()
00036 gradient = QtGui.QRadialGradient(self.width()/2, self.height()/2, min(self.width(), self.height()))
00037 gradient.setColorAt(0.7, QtGui.QColor(0,0,0))
00038 gradient.setColorAt(0.0, QtGui.QColor(0,255,0))
00039 p.setBrush(QPalette.Window, QtGui.QBrush(gradient))
00040 self.setPalette(p)
00041
00042 def info_cb(self, info):
00043 tmp_model = image_geometry.PinholeCameraModel()
00044 tmp_model.fromCameraInfo(info)
00045 self.model = tmp_model
00046
00047 def image_cb(self, msg):
00048 self.image = msg
00049
00050 def center(self, *args):
00051 while (self.image is None or self.model is None) and not rospy.is_shutdown():
00052 rospy.sleep(0.1)
00053
00054 if rospy.is_shutdown(): sys.exit(0)
00055
00056 im = np.asarray(self.bridge.imgmsg_to_cv(self.image))
00057 dists = np.sum((im - [0,255,0])**2, axis=2)
00058 px = np.unravel_index(np.argmin(dists.flatten()), dists.shape)
00059 target_pt = self.model.projectPixelTo3dRay((px[1],px[0]))
00060 print 'pointing at %s' % str(target_pt)
00061 pt = transform_listener.transform_point('base_footprint', self.model.tf_frame, Point(*target_pt))
00062 self.head.look_at_relative_point(pt.x,pt.y,pt.z)
00063 self.center()
00064
00065 def __init__(self):
00066 super(Gradient, self).__init__()
00067 self.head = Head()
00068 self.head.pointing_frame = '/narrow_stereo_optical_frame'
00069 self.bridge = CvBridge()
00070 self.initUI()
00071 rospy.Subscriber('camera_info', CameraInfo, self.info_cb)
00072 rospy.Subscriber('image', Image, self.image_cb)
00073
00074 PySide.QtCore.QTimer.singleShot(1000, self.center)
00075
00076 sys.exit(app.exec_())
00077 rospy.spin()
00078
00079 if __name__ == '__main__':
00080 rospy.init_node('gradient')
00081 app = PySide.QtGui.QApplication(sys.argv)
00082 g = Gradient()