scanner.py
Go to the documentation of this file.
00001 #
00002 # Copyright (c) 2010, Georgia Tech Research Corporation
00003 # All rights reserved.
00004 #
00005 # Redistribution and use in source and binary forms, with or without
00006 # modification, are permitted provided that the following conditions are met:
00007 #     * Redistributions of source code must retain the above copyright
00008 #       notice, this list of conditions and the following disclaimer.
00009 #     * Redistributions in binary form must reproduce the above copyright
00010 #       notice, this list of conditions and the following disclaimer in the
00011 #       documentation and/or other materials provided with the distribution.
00012 #     * Neither the name of the Georgia Tech Research Corporation nor the
00013 #       names of its contributors may be used to endorse or promote products
00014 #       derived from this software without specific prior written permission.
00015 #
00016 # THIS SOFTWARE IS PROVIDED BY GEORGIA TECH RESEARCH CORPORATION ''AS IS'' AND
00017 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 # DISCLAIMED. IN NO EVENT SHALL GEORGIA TECH BE LIABLE FOR ANY DIRECT, INDIRECT,
00020 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00021 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00022 # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00023 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
00024 # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00025 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 #
00027 
00028 #  \author Martin Schuster (Healthcare Robotics Lab, Georgia Tech.)
00029 
00030 import roslib; roslib.load_manifest('laser_camera_segmentation')
00031 import hrl_tilting_hokuyo.tilt_hokuyo_servo as ths
00032 #import hokuyo.hokuyo_processing as hp
00033 import hrl_hokuyo.hokuyo_scan as hs
00034 
00035 
00036 import sys
00037 
00038 from labeling import label_object, scan_dataset, scans_database
00039 
00040 from opencv import highgui
00041 import hrl_lib.util as ut
00042 
00043 class scanner:
00044     def __init__(self, configuration):
00045         
00046         self.config = configuration
00047         
00048         
00049         self.webcam = False
00050         self.thok = False
00051         
00052         self.img = False
00053         
00054     
00055     #use the actual hardware 
00056     def init_webcam(self):
00057         if self.webcam:
00058             return
00059         
00060         if self.config.device == 'codyRobot':
00061             import camera
00062             self.webcam = camera.Camera(self.config.cam_name)
00063         else:
00064             import webcamera
00065             self.webcam = webcamera.Webcamera(self.config.cam_name, self.config.webcam_id)
00066            
00067         
00068 
00069 #        img = False
00070 #        cam_index = -1
00071 #        while not(img) and cam_index < 20:
00072 #           cam_index = cam_index + 1
00073 #            try:
00074 #               del self.webcam
00075 #               del webcamera
00076 #               import webcamera
00077  #               self.webcam = webcamera.Webcamera('DesktopWebcam', cam_index)
00078   #              img = self.webcam.get_frame()
00079   #          except:
00080    #             print "Unexpected error:", sys.exc_info()[0]
00081     #            print "try again...with next webcam" + str(cam_index)
00082      #           pass
00083             
00084 #        if not(img):
00085  #           print 'ERROR: Webcam init FAILED'
00086   #          return
00087    
00088 
00089  
00090     #use the actual hardware 
00091     def init_thok(self):
00092         if self.thok:
00093             return
00094         print "Init THOK"
00095         self.hok = hs.Hokuyo('utm',self.config.thok_hoknum,flip=False)
00096         self.thok = ths.tilt_hokuyo(self.config.thok_devname,self.config.thok_servonum,self.hok,l1=self.config.thok_l1,l2=self.config.thok_l2) 
00097         print "Init THOK done"
00098         
00099     def capture_image(self):
00100         self.init_webcam()
00101         del self.img
00102         self.img = False
00103         count = 0
00104         print 'capture image...'
00105         
00106         while not(self.img) and count < 20:
00107             count = count + 1
00108             try:
00109                 #call get_frame several times to really get a new picture(!)
00110                 for i in xrange(10):
00111                     self.img = self.webcam.get_frame()
00112             except:
00113                 print "Unexpected error:", sys.exc_info()[0]
00114                 print "try again..."
00115                 pass
00116         print 'count:'+str(count)
00117         return self.img
00118     
00119     def capture_laserscan(self, number_of_scans = 1, angle = None):
00120         self.init_thok()
00121         self.laserscans = []
00122         
00123         if angle != None:
00124             tilt_angles = (self.config.thok_tilt_angles[0] + angle, self.config.thok_tilt_angles[1] + angle)
00125         else:
00126             tilt_angles = self.config.thok_tilt_angles
00127         
00128         for i in range(number_of_scans):
00129             pos_list,scan_list = self.thok.scan(tilt_angles,speed=self.config.thok_scan_speed,save_scan=False)
00130             self.laserscans.append((pos_list,scan_list))
00131             #print scan_list[0]
00132     
00133             
00134         return self.laserscans
00135     
00136     
00137     def save_data(self,name, metadata=True, angle = None):
00138         dict = {'laserscans' : self.laserscans,
00139             'l1': self.config.thok_l1, 'l2': self.config.thok_l2,
00140             'image_angle' : angle} 
00141         
00142         prefix = self.config.path+'/data/'+name
00143         print "Saving: "+prefix+'_laserscans.pkl'
00144         ut.save_pickle(dict,prefix+'_laserscans.pkl')
00145         print "Saving: "+prefix+'_image.png'
00146         highgui.cvSaveImage(prefix+'_image.png',self.img)
00147         
00148         if metadata:
00149             # save metadata to database:
00150             database = scans_database.scans_database()
00151             database.load(self.config.path,'database.pkl')
00152             dataset = scan_dataset.scan_dataset()
00153             dataset.id = name
00154             dataset.scan_filename = 'data/'+name+'_laserscans.pkl'
00155             dataset.image_filename = 'data/'+name+'_image.png'
00156             database.add_dataset(dataset)
00157             database.save()
00158         
00159         return name
00160         
00161     def take_artag_image(self):    
00162         img = self.capture_image()
00163         return img
00164     
00165     def save_artag_image(self,name):    
00166         
00167         filename = self.config.path+'/data/'+name+'_artag_image.png'
00168         print "Saving: "+filename
00169         highgui.cvSaveImage(filename,self.img)
00170         
00171         return '/data/'+name+'_artag_image.png'
00172         
00173     #capture image and laserscans, save image, scan
00174     def capture_and_save(self,name, metadata=True, angle = None):
00175         self.init_webcam()
00176         self.init_thok()
00177         if None != angle:
00178             self.thok.servo.move_angle(angle)
00179             angle = self.thok.servo.read_angle()        
00180         
00181         self.capture_image()
00182         self.capture_laserscan(1, angle)
00183         return self.save_data(name, metadata, angle)


laser_camera_segmentation
Author(s): Martin Schuster, Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab at Georgia Tech
autogenerated on Wed Nov 27 2013 11:56:44