ardrone_tracker.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Copyright (c) 2012, Falkor Systems, Inc.  All rights reserved.
00003 
00004 # Redistribution and use in source and binary forms, with or without
00005 # modification, are permitted provided that the following conditions are
00006 # met:
00007 
00008 # Redistributions of source code must retain the above copyright notice,
00009 # this list of conditions and the following disclaimer.  Redistributions
00010 # in binary form must reproduce the above copyright notice, this list of
00011 # conditions and the following disclaimer in the documentation and/or
00012 # other materials provided with the distribution.  THIS SOFTWARE IS
00013 # PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
00014 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00015 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00016 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
00017 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00018 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00019 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00020 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00021 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00022 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00023 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00024 
00025 import roslib
00026 roslib.load_manifest('falkor_ardrone')
00027 
00028 import sys
00029 import rospy
00030 import cv
00031 import cv2
00032 import math
00033 
00034 from std_msgs.msg import String
00035 from sensor_msgs.msg import Image
00036 from geometry_msgs.msg import Point
00037 from cv_bridge import CvBridge, CvBridgeError
00038 
00039 import numpy as np
00040 import tracker
00041 
00042 class ardroneTracker:
00043 
00044   def __init__(self, tracker):
00045     self.point_pub = rospy.Publisher("/ardrone_tracker/found_point", Point )
00046 
00047     self.bridge = CvBridge()
00048     self.image_sub = rospy.Subscriber("/ardrone/front/image_raw",Image,self.callback)
00049     self.image_pub = rospy.Publisher( "/ardrone_tracker/image", Image )
00050 
00051     self.tracker = tracker
00052 
00053   def callback(self,data):
00054     try:
00055       cv_image = self.bridge.imgmsg_to_cv(data, "bgr8")
00056     except CvBridgeError, e:
00057       print e
00058   
00059     numpy_image = np.asarray( cv_image )
00060     trackData = self.tracker.track( numpy_image )
00061     if trackData:
00062       x,y,z = trackData[0],trackData[1],trackData[2]
00063 
00064       point = Point( x,y,z )
00065       self.point_pub.publish( point )
00066     else:
00067       self.point_pub.publish( Point( 0, 0, -1 ) )
00068 
00069     try:
00070       vis = self.tracker.get_vis()
00071       image_message = self.bridge.cv_to_imgmsg(cv2.cv.fromarray( vis ), encoding="passthrough")
00072       self.image_pub.publish( image_message )
00073     except CvBridgeError, e:
00074       print e
00075 
00076 def main():
00077   rospy.init_node( 'ardrone_tracker' )
00078 #  ardroneTracker(tracker.LkTracker() )
00079 #  ardroneTracker(tracker.DummyTracker() )
00080 #            '/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml'
00081 
00082   cascade_file = rospy.get_param( '~cascadefile', 'NULL' )
00083   if cascade_file == 'NULL':
00084     print "Must set cascadefile parameter!"
00085     exit
00086 
00087   ardroneTracker( tracker.CascadeTracker( cascade_file ) )
00088   try:
00089     rospy.spin()
00090   except KeyboardInterrupt:
00091     print "Shutting down"
00092   cv.DestroyAllWindows()
00093 
00094 if __name__ == '__main__':
00095 #  import cProfile
00096 #  cProfile.run('main()')
00097     main()


falkor_ardrone
Author(s): Sameer Parekh
autogenerated on Tue Jan 7 2014 11:12:13