Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 import roslib; roslib.load_manifest('laser_camera_segmentation')
00031 import hrl_tilting_hokuyo.tilt_hokuyo_servo as ths
00032
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
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
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
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
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
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
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
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)