Package libavg :: Module camcalibrator

Source Code for Module libavg.camcalibrator

  1  #!/usr/bin/python 
  2  # -*- coding: utf-8 -*- 
  3   
  4   
  5  import sys, os, math, stat 
  6  from libavg import avg 
  7  from libavg import anim 
  8   
9 -class CamCalibrator:
10 - def __init__(self, Player, Tracker, CameraType, ParentNode):
11 self.__Player = Player 12 self.__Tracker = Tracker 13 self.__curParam = 0 14 self.__saveIndex = 0 15 self.__onFrameID = Player.setOnFrameHandler(self.onFrame) 16 self.__makeParamList(CameraType) 17 self.__parentNode = ParentNode 18 node = Player.createNode('<image href="black.png" opacity="1"/>') 19 node.width = ParentNode.width 20 node.height = ParentNode.height 21 ParentNode.appendChild(node) 22 node = Player.createNode('<image id="cc_distorted"/>') 23 ParentNode.appendChild(node) 24 node = Player.createNode('<image id="cc_fingers"/>') 25 ParentNode.appendChild(node) 26 node = Player.createNode(''' 27 <div id="cc_gui" x="30" y="30" > 28 <image x="0" y="13" width="500" height="215" 29 href="black.png" opacity="0.6"/> 30 <words x="2" y="13" text="camera" size="16" font="Eurostile" color="00FF00"/> 31 <image x="2" y="32" href="CamImgBorder.png"/> 32 <image id="cc_camera" x="4" y="34" width="160" height="120"/> 33 34 <words x="168" y="13" text="nohistory" size="16" font="Eurostile" color="00FF00"/> 35 <image x="168" y="32" href="CamImgBorder.png"/> 36 <image id="cc_nohistory" x="170" y="34" width="160" height="120"/> 37 38 <words x="334" y="13" text="histogram" size="16" font="Eurostile" color="00FF00"/> 39 <image x="334" y="32" href="CamImgBorder.png"/> 40 <image id="cc_histogram" x="336" y="34" width="160" height="120"/> 41 42 <words id="cc_param0" x="2" y="162" size="13" font="Eurostile"/> 43 <words id="cc_param1" x="2" y="178" size="13" font="Eurostile"/> 44 <words id="cc_param2" x="2" y="194" size="13" font="Eurostile"/> 45 <words id="cc_param3" x="2" y="210" size="13" font="Eurostile"/> 46 <words id="cc_param4" x="168" y="162" size="13" font="Eurostile"/> 47 <words id="cc_param5" x="168" y="178" size="13" font="Eurostile"/> 48 <words id="cc_param6" x="168" y="194" size="13" font="Eurostile"/> 49 <words id="cc_param7" x="168" y="210" size="13" font="Eurostile"/> 50 <words id="cc_param8" x="304" y="162" size="13" font="Eurostile"/> 51 <words id="cc_param9" x="428" y="162" size="13" font="Eurostile"/> 52 <words id="cc_param10" x="304" y="178" size="13" font="Eurostile"/> 53 <words id="cc_param11" x="428" y="178" size="13" font="Eurostile"/> 54 <words id="cc_param12" x="304" y="194" size="13" font="Eurostile"/> 55 <words id="cc_param13" x="428" y="194" size="13" font="Eurostile"/> 56 <words id="cc_param14" x="304" y="210" size="13" font="Eurostile"/> 57 <words id="cc_param15" x="428" y="210" size="13" font="Eurostile"/> 58 </div> 59 ''') 60 node.width = ParentNode.width 61 node.height = ParentNode.height 62 ParentNode.appendChild(node) 63 self.__isActive = True 64 self.__switchActive()
65
66 - def __flipBitmap(self, ImgName):
67 Node = self.__player.getElementByID(ImgName) 68 Grid = Node.getOrigVertexCoords() 69 Grid = [ [ (pos[0], 1-pos[1]) for pos in line ] for line in Grid] 70 Node.setWarpedVertexCoords(Grid)
71
72 - def __switchActive(self):
73 self.__isActive = not(self.__isActive) 74 if self.__isActive: 75 self.__parentNode.active = 1 76 self.__parentNode.opacity = 1 77 self.__displayParams() 78 self.__onFrameID = self.__Player.setOnFrameHandler(self.onFrame) 79 else: 80 self.__parentNode.active = 0 81 self.__parentNode.opacity = 0 82 self.__Player.clearInterval(self.__onFrameID) 83 self.__Tracker.setDebugImages(self.__isActive, self.__isActive)
84
85 - def __makeParamList(self, CameraType):
86 if CameraType == "Fire-i": 87 self.__paramList = [ 88 # Camera 89 {'Name':"Brightness", 90 'path':"/camera/brightness/@value", 91 'min':128, 'max':383, 'increment':1, 'precision':0}, 92 {'Name':"Exposure", 93 'path':"/camera/exposure/@value", 94 'min':0, 'max':511, 'increment':1, 'precision':0}, 95 {'Name':"Shutter", 96 'path':"/camera/shutter/@value", 97 'min':0, 'max':7, 'increment':1, 'precision':0}, 98 {'Name':"Gain", 99 'path':"/camera/gain/@value", 100 'min':0, 'max':255, 'increment':1, 'precision':0} 101 ] 102 elif CameraType == "FireFly": 103 self.__paramList = [ 104 # Camera 105 {'Name':"Brightness", 106 'path':"/camera/brightness/@value", 107 'min':1, 'max':255, 'increment':1, 'precision':0}, 108 {'Name':"Exposure", 109 'path':"/camera/exposure/@value", 110 'min':7, 'max':62, 'increment':1, 'precision':0}, 111 {'Name':"Shutter", 112 'path':"/camera/shutter/@value", 113 'min':1, 'max':533, 'increment':1, 'precision':0}, 114 {'Name':"Gain", 115 'path':"/camera/gain/@value", 116 'min':16, 'max':64, 'increment':1, 'precision':0} 117 ] 118 else: 119 print("CamCalibrator: unknown CameraType") 120 sys.exit() 121 122 self.__paramList.extend([ 123 # Tracker 124 {'Name':"Threshold", 125 'path':"/tracker/track/threshold/@value", 126 'min':1, 'max':255, 'increment':1, 'precision':0}, 127 {'Name':"Min Area", 128 'path':"/tracker/track/areabounds/@min", 129 'min':1, 'max':1000000, 'increment':3, 'precision':0}, 130 {'Name':"Max Area", 131 'path':"/tracker/track/areabounds/@max", 132 'min':20, 'max':1000000, 'increment':10, 'precision':0}, 133 {'Name':"Contour Precision", 134 'path':"/tracker/contourprecision/@value", 135 'min':0, 'max':1000, 'increment':1, 'precision':0}, 136 137 # Transform 138 {'Name':"Displacement x", 139 'path':"/transform/displaydisplacement/@x", 140 'min':-5000, 'max':0, 'increment':1, 'precision':0}, 141 {'Name':"y", 142 'path':"/transform/displaydisplacement/@y", 143 'min':-5000, 'max':0, 'increment':1, 'precision':0}, 144 {'Name':"Scale x", 145 'path':"/transform/displayscale/@x", 146 'min':-3, 'max':8, 'increment':0.01, 'precision':2}, 147 {'Name':"y", 148 'path':"/transform/displayscale/@y", 149 'min':-3, 'max':8, 'increment':0.01, 'precision':2}, 150 {'Name':"Distortion p2", 151 'path':"/transform/distortionparams/@p2", 152 'min':-3, 'max':3, 'increment':0.001, 'precision':3}, 153 {'Name':"p3", 154 'path':"/transform/distortionparams/@p3", 155 'min':-3, 'max':3, 'increment':0.001, 'precision':3}, 156 {'Name':"Trapezoid", 157 'path':"/transform/trapezoid/@value", 158 'min':-3, 'max':3, 'increment':0.00001, 'precision':5}, 159 {'Name':"Angle", 160 'path':"/transform/angle/@value", 161 'min':-3.15, 'max':3.15, 'increment':0.01, 'precision':2}, 162 ])
163
164 - def __changeParam(self, Change):
165 param = self.__paramList[self.__curParam] 166 if param['increment'] >= 1: 167 Val = int(self.__Tracker.getParam(param['path'])) 168 else: 169 Val = float(self.__Tracker.getParam(param['path'])) 170 Val += Change*param['increment'] 171 if Val < param['min']: 172 Val = param['min'] 173 if Val > param['max']: 174 Val = param['max'] 175 self.__Tracker.setParam(param['path'], str(Val))
176
177 - def __displayParams(self):
178 i = 0 179 for Param in self.__paramList: 180 Node = self.__Player.getElementByID("cc_param"+str(i)) 181 Path = Param['path'] 182 Val = float(self.__Tracker.getParam(Path)) 183 Node.text = Param['Name']+": "+('%(val).'+str(Param['precision'])+'f') % {'val': Val} 184 if self.__curParam == i: 185 Node.color = "FFFFFF" 186 else: 187 Node.color = "A0A0FF" 188 i += 1
189
190 - def __saveTrackerImage(self, ImageID, ImageName):
191 self.__Tracker.getImage(ImageID).save( 192 "img"+str(self.__saveIndex)+"_"+ImageName+".png")
193
194 - def onFrame(self):
195 def showTrackerImage(TrackerImageID, NodeID, w=None, h=None): 196 Bitmap = self.__Tracker.getImage(TrackerImageID) 197 Node = self.__Player.getElementByID(NodeID) 198 Node.setBitmap(Bitmap) 199 if w != None: 200 Node.width=w 201 Node.height=h
202 showTrackerImage(avg.IMG_DISTORTED, "cc_distorted", 203 self.__parentNode.width, self.__parentNode.height) 204 showTrackerImage(avg.IMG_FINGERS, "cc_fingers", 205 self.__parentNode.width, self.__parentNode.height) 206 showTrackerImage(avg.IMG_CAMERA, "cc_camera", 160, 120) 207 showTrackerImage(avg.IMG_NOHISTORY, "cc_nohistory", 160, 120) 208 showTrackerImage(avg.IMG_HISTOGRAM, "cc_histogram", 160, 120)
209
210 - def onKeyUp(self, Event):
211 if Event.keystring == "t": 212 self.__switchActive() 213 return True 214 else: 215 if self.__isActive: 216 if Event.keystring == "up": 217 if self.__curParam > 0: 218 self.__curParam -= 1 219 elif Event.keystring == "down": 220 if self.__curParam < len(self.__paramList)-1: 221 self.__curParam += 1 222 elif Event.keystring == "left": 223 self.__changeParam(-1) 224 elif Event.keystring == "right": 225 self.__changeParam(1) 226 elif Event.keystring == "page up": 227 self.__changeParam(-10) 228 elif Event.keystring == "page down": 229 self.__changeParam(10) 230 elif Event.keystring == "h": 231 self.__Tracker.resetHistory() 232 print "History reset" 233 elif Event.keystring == "s": 234 self.__Tracker.saveConfig("") 235 print ("Tracker configuration saved.") 236 elif Event.keystring == "w": 237 self.__saveIndex += 1 238 self.__saveTrackerImage(avg.IMG_CAMERA, "camera") 239 self.__saveTrackerImage(avg.IMG_DISTORTED, "distorted") 240 self.__saveTrackerImage(avg.IMG_NOHISTORY, "nohistory") 241 self.__saveTrackerImage(avg.IMG_HIGHPASS, "highpass") 242 self.__saveTrackerImage(avg.IMG_FINGERS, "fingers") 243 print ("Images saved.") 244 elif Event.keystring == "i": 245 self.__showImages() 246 else: 247 return False 248 self.__displayParams() 249 return True 250 else: 251 return False
252