summaryrefslogtreecommitdiff
path: root/src/python/camcalibrator.py
blob: 08bd308579487dab194a435804671ed11a12a123 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# libavg - Media Playback Engine.
# Copyright (C) 2003-2014 Ulrich von Zadow
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Current versions can be found at www.libavg.de
#
# Original author of this file is igor <igor (at) c-base (dot) org>
#

import sys, os
from libavg import avg, AVGApp, player

import coordcalibrator
import apphelpers

mediadir = os.path.join(os.path.dirname(__file__), 'data')
g_KbManager = apphelpers.KeyboardManager.get()


def camera_setup(CameraType):
    if CameraType == "Fire-i":
        paramList = [
            {'Name':"Brightness",
             'path':"/camera/brightness/@value",
             'min':128, 'max':383, 'increment':1, 'precision':0},
            {'Name':"Exposure",
             'path':"/camera/exposure/@value",
             'min':-1, 'max':511, 'increment':1, 'precision':0},
            {'Name':"Shutter",
             'path':"/camera/shutter/@value",
             'min':0, 'max':7, 'increment':1, 'precision':0},
            {'Name':"Gain",
             'path':"/camera/gain/@value",
             'min':0, 'max':255, 'increment':1, 'precision':0},
        ]
    elif CameraType == "FireFly":
        paramList = [
            {'Name':"Brightness",
             'path':"/camera/brightness/@value",
             'min':1, 'max':255, 'increment':1, 'precision':0},
            {'Name':"Shutter",
             'path':"/camera/shutter/@value",
             'min':1, 'max':533, 'increment':1, 'precision':0},
            {'Name':"Gain",
             'path':"/camera/gain/@value",
             'min':16, 'max':64, 'increment':1, 'precision':0},
            {'Name':"Gamma",
             'path':"/camera/gamma/@value",
             'min':0, 'max':1, 'increment':1, 'precision':0},
        ]
    elif CameraType == "DragonFly":
        paramList = [
            {'Name':"Brightness",
             'path':"/camera/brightness/@value",
             'min':1, 'max':255, 'increment':1, 'precision':0},
            {'Name':"Gamma",
             'path':"/camera/gamma/@value",
             'min':512, 'max':4095, 'increment':5, 'precision':0},
            {'Name':"Shutter",
             'path':"/camera/shutter/@value",
             'min':0, 'max':709, 'increment':2, 'precision':0},
            {'Name':"Gain",
             'path':"/camera/gain/@value",
             'min':16, 'max':683, 'increment':2, 'precision':0},
        ]
    else:
        avg.logger.error("Unknown CameraType %s" % CameraType)
        sys.exit()

    paramList.extend([
        # Touch
        {'Name':"Threshold",
         'path':"/tracker/touch/threshold/@value",
         'min':1, 'max':255, 'increment':1, 'precision':0},
        {'Name':"Similarity",
         'path':"/tracker/touch/similarity/@value",
         'min':1, 'max':300, 'increment':1, 'precision':1},

        {'Name':"Min Area",
         'path':"/tracker/touch/areabounds/@min",
         'min':1, 'max':1000000, 'increment':3, 'precision':0},
        {'Name':"Max Area",
         'path':"/tracker/touch/areabounds/@max",
         'min':20, 'max':1000000, 'increment':10, 'precision':0},

        {'Name':"Ecc. Min",
         'path':"/tracker/touch/eccentricitybounds/@min",
         'min':1, 'max':30, 'increment':1, 'precision':1},
        {'Name':"Ecc. Max",
         'path':"/tracker/touch/eccentricitybounds/@max",
         'min':1, 'max':2000, 'increment':1, 'precision':1},

        {'Name':"Bandpass Min",
         'path':"/tracker/touch/bandpass/@min",
         'min':0, 'max':15, 'increment':.1, 'precision':1},
        {'Name':"Bandpass Max",
         'path':"/tracker/touch/bandpass/@max",
         'min':0, 'max':15, 'increment':.1, 'precision':1},
        {'Name':"Bandpass Postmult",
         'path':"/tracker/touch/bandpasspostmult/@value",
         'min':0, 'max':30, 'increment':.1, 'precision':1},

        # Track
        {'Name':"Threshold",
         'path':"/tracker/track/threshold/@value",
         'min':1, 'max':255, 'increment':1, 'precision':0},

        {'Name':"Min Area",
         'path':"/tracker/track/areabounds/@min",
         'min':1, 'max':1000000, 'increment':3, 'precision':0},
        {'Name':"Max Area",
         'path':"/tracker/track/areabounds/@max",
         'min':20, 'max':1000000, 'increment':10, 'precision':0},

        {'Name':"Ecc. Min",
         'path':"/tracker/track/eccentricitybounds/@min",
         'min':1, 'max':30, 'increment':1, 'precision':1},
        {'Name':"Ecc. Max",
         'path':"/tracker/track/eccentricitybounds/@max",
         'min':1, 'max':2000, 'increment':1, 'precision':1},

        # Transform
        {'Name':"p2",
         'path':"/transform/distortionparams/@p2",
         'min':-3, 'max':3, 'increment':0.001, 'precision':3},
        {'Name':"Trapezoid",
         'path':"/transform/trapezoid/@value",
         'min':-3, 'max':3, 'increment':0.0001, 'precision':4},
        {'Name':"Angle",
         'path':"/transform/angle/@value",
         'min':-3.15, 'max':3.15, 'increment':0.01, 'precision':2},
        {'Name':"Displ. x",
         'path':"/transform/displaydisplacement/@x",
         'min':-5000, 'max':0, 'increment':1, 'precision':0},
        {'Name':"Displ. y",
         'path':"/transform/displaydisplacement/@y",
         'min':-5000, 'max':0, 'increment':1, 'precision':0},
        {'Name':"Scale x",
         'path':"/transform/displayscale/@x",
         'min':-3, 'max':8, 'increment':0.01, 'precision':2},
        {'Name':"Scale y",
         'path':"/transform/displayscale/@y",
         'min':-3, 'max':8, 'increment':0.01, 'precision':2},
    ])
    return paramList

class Calibrator(AVGApp):
    def __init__(self, parentNode, CameraType = "FireFly", appStarter = None):
        super(Calibrator, self).__init__(parentNode)
        self.paramList = camera_setup(CameraType)
        self.parentNode=parentNode
        self.appStarter = appStarter
        self.mainNode = player.createNode(
        """
        <div active="False" opacity="0">
            <image width="1280" height="800" href="black.png"/>
            <image id="cal_distorted" x="0" y="0" width="1280" height="800"
                    sensitive="false" opacity="1"/>
            <words id="cal_fps" x="30" y="30" color="00FF00" text=""/>
            <words id="cal_notification" x="390" y="390" width="500" fontsize="18"
                    color="ff3333" alignment="center" />
            <div id="cal_gui" x="30" y="540">
                <image id="cal_shadow" x="0" y="13" width="500" height="150" 
                        href="black.png" opacity="0.6"/>
                
                <words x="2" y="13" text="camera" fontsize="16" color="00FF00"/>
                <image x="2" y="32" href="CamImgBorder.png"/>
                <image id="cal_camera" x="4" y="34" width="160" height="120"/>

                <words x="168" y="13" text="nohistory" fontsize="16" color="00FF00"/>
                <image x="168" y="32" href="CamImgBorder.png"/>
                <image id="cal_nohistory" x="170" y="34" width="160" height="120"/>
                
                <words x="334" y="13" text="histogram" fontsize="16" color="00FF00"/>
                <image x="334" y="32" href="CamImgBorder.png"/>
                <image id="cal_histogram" x="336" y="34" width="160" height="120"/>

                <div id="cal_params" y="170" opacity="0.9">
                <image id="cal_shadow2" width="750" height="65" href="black.png" opacity="0.6"/>
                    <div id="cal_paramdiv0" x="2">
                        <words text="camera" y="0" fontsize="10" color="00ff00" /> 
                        <words id="cal_param0" y="12" fontsize="10"/> 
                        <words id="cal_param1" y="24" fontsize="10"/> 
                        <words id="cal_param2" y="36" fontsize="10"/> 
                        <words id="cal_param3" y="48" fontsize="10"/> 
                    </div>
                    <div id="cal_paramdiv1" x="80">
                        <words text="touch" y="0" fontsize="10" color="00ff00" /> 
                        <words id="cal_param4" y="12" fontsize="10"/> 
                        <words id="cal_param5" y="24" fontsize="10"/> 
                        <words id="cal_param6" y="36" fontsize="10"/> 
                        <words id="cal_param7" y="48" fontsize="10"/> 
                    </div>
                    <div id="cal_paramdiv2" x="200">
                        <words id="cal_param8" y="0" fontsize="10"/> 
                        <words id="cal_param9" y="12" fontsize="10"/> 
                        <words id="cal_param10" y="24" fontsize="10"/> 
                        <words id="cal_param11" y="36" fontsize="10"/> 
                        <words id="cal_param12" y="48" fontsize="10"/> 
                    </div>
                    <div id="cal_paramdiv3" x="350">
                        <words text="track" y="0" fontsize="10" color="00ff00" /> 
                        <words id="cal_param13" y="12" fontsize="10"/> 
                        <words id="cal_param14" y="24" fontsize="10"/> 
                        <words id="cal_param15" y="36" fontsize="10"/> 
                        <words id="cal_param16" y="48" fontsize="10"/> 
                    </div>
                    <div id="cal_paramdiv4" x="500">
                        <words id="cal_param17" y="0" fontsize="10"/> 
                        <words text="distort" y="12" fontsize="10" color="00ff00"/> 
                        <words id="cal_param18" y="24" fontsize="10"/> 
                        <words id="cal_param19" y="36" fontsize="10"/> 
                        <words id="cal_param20" y="48" fontsize="10"/> 
                    </div>
                    <div id="cal_paramdiv5" x="650">
                        <words id="cal_param21" y="0" fontsize="10"/> 
                        <words id="cal_param22" y="12" fontsize="10"/> 
                        <words id="cal_param23" y="24" fontsize="10"/> 
                        <words id="cal_param24" y="36" fontsize="10"/> 
                        <words id="cal_param25" y="48" fontsize="10"/> 
                    </div>
                </div>
            </div>
            <div id="cal_coordcalibrator" opacity="0" active="false">
              <image x="0" y="0" width="1280" height="800" href="border.png"/>
              <div id="cal_messages" x="100" y="100"/>
              <image id="cal_crosshair" href="crosshair.png"/>
              <image id="cal_feedback" href="Feedback.png"/>
            </div>
        </div>
        """)
        self.mainNode.mediadir=mediadir
        parentNode.insertChild(self.mainNode, 0)

        self.coordCal = None
        self.tracker = player.getTracker()
        self.curParam = 0
        self.saveIndex = 0
        self.hideMainNodeTimeout = None
        self.video = []
        self.__guiOpacity = 1
        self.__showBigCamImage = False
        self.__notificationTimer = None
        self.__onCalibrationSuccess = None

        
        
    def _enter(self):
        
        g_KbManager.push()
        
        g_KbManager.bindKey('d', self.__trackerSetDebugImages, 'tracker set debug images')
        g_KbManager.bindKey('b', self.__bigCamImage, 'big cam image')
        g_KbManager.bindKey('up', self.__keyFuncUP, 'select parameter up')
        g_KbManager.bindKey('down', self.__keyFuncDOWN, 'select parameter down')
        g_KbManager.bindKey('left', self.__keyFuncLEFT, 'value up')
        g_KbManager.bindKey('right', self.__keyFuncRIGHT, 'value down')
        g_KbManager.bindKey('page up', self.__keyFuncPAGEUp, 'value up * 10')
        g_KbManager.bindKey('page down', self.__keyFuncPAGEDown, 'value down * 10')
        g_KbManager.bindKey('s', self.__trackerSaveConfig, 'save configuration')
        g_KbManager.bindKey('g', self.__toggleGUI, 'toggle GUI')
        g_KbManager.bindKey('c', self.__startCoordCalibration,
                'start geometry calibration')
        g_KbManager.bindKey('w', self.__saveTrackerIMG, 'SAVE trager image')
        g_KbManager.bindKey('h', self.appStarter.tracker.resetHistory, 'RESET history')
        
        self.appStarter.showTrackerImage()
        self.mainNode.active=True
        self.tracker.setDebugImages(True, True)
        avg.fadeIn(self.mainNode, 400, 1)
        Bitmap = self.tracker.getImage(avg.IMG_DISTORTED)  # Why is this needed?
        self.__onFrameID = player.subscribe(player.ON_FRAME, self.__onFrame)
        #grandparent = self.parentNode.getParent()
        #if grandparent:
        #    grandparent.reorderChild(grandparent.indexOf(self.parentNode), grandparent.getNumChildren()-1)
        self.displayParams()
        if self.hideMainNodeTimeout:
            player.clearInterval(self.hideMainNodeTimeout)

    def _leave(self):
        #unbind all calibrator keys - bind old keys
        g_KbManager.pop()
        
        def hideMainNode():
            self.mainNode.opacity=0
            self.mainNode.active = False
        self.appStarter.hideTrackerImage()
        #grandparent = self.parentNode.getParent()
        #if grandparent:
        #    grandparent.reorderChild(grandparent.indexOf(self.parentNode), 0)
        self.hideMainNodeTimeout = player.setTimeout(400, hideMainNode)
        player.clearInterval(self.__onFrameID)

    def reparent(self, newParent):
        """reparents the calibrator node; returns the old(!) parent node"""
        oldParent = self.mainNode.getParent()
        self.mainNode.unlink()
        newParent.appendChild(self.mainNode)
        return oldParent

    def __deferredRefreshCB(self):
        self.displayParams()
        self.tracker.resetHistory()
        self.setNotification('')
        g_KbManager.pop()
        player.getElementByID('cal_params').opacity = 0.9

    def __clearNotification(self):
        self.__notificationTimer = None
        self.setNotification('')

    def __toggleGUI(self):
        self.__guiOpacity = 1 - self.__guiOpacity
        player.getElementByID('cal_gui').opacity = self.__guiOpacity

    def __onFrame(self):
        def showTrackerImage(trackerImageID, nodeID, size, pos=(0,0)):
            bitmap = self.tracker.getImage(trackerImageID)
            node = player.getElementByID(nodeID)
            node.setBitmap(bitmap)
            node.size = size
            if pos != (0,0):
                node.pos = pos

            # flip:
            grid = node.getOrigVertexCoords()
            grid = [ [ (1-pos[0], pos[1]) for pos in line ] for line in grid]
            node.setWarpedVertexCoords(grid)

        if self.__showBigCamImage:
            showTrackerImage(avg.IMG_CAMERA, "cal_distorted", (1280, 960))
        else:
            pos = self.tracker.getDisplayROIPos()
            size = self.tracker.getDisplayROISize()
            showTrackerImage(avg.IMG_DISTORTED, "cal_distorted", pos = pos, size = size)
        showTrackerImage(avg.IMG_CAMERA, "cal_camera", (160, 120))
        showTrackerImage(avg.IMG_NOHISTORY, "cal_nohistory", (160, 120))
        showTrackerImage(avg.IMG_HISTOGRAM, "cal_histogram", (160, 120))
        fps = player.getEffectiveFramerate()
        player.getElementByID("cal_fps").text = '%(val).2f' % {'val': fps} 
        
    def __trackerSetDebugImages(self):
        self.appStarter.toggleTrackerImage()
        # toggleTrackerImage() will influence setDebugImages status, so we have to reset it:
        self.tracker.setDebugImages(True, True)
                     
    def __bigCamImage(self):
        self.__showBigCamImage = not(self.__showBigCamImage)
        
    def __keyFuncUP(self):
        if self.curParam > 0:
            self.curParam -= 1
        self.displayParams()    
                
    def __keyFuncDOWN(self):
        if self.curParam < len(self.paramList)-1:
            self.curParam += 1
        self.displayParams()  
                
    def __keyFuncLEFT(self):
        self.changeParam(-1)
        self.displayParams()  
        
    def __keyFuncRIGHT(self):
        self.changeParam(1)  
        self.displayParams()  
        
    def __keyFuncPAGEUp(self):
        self.changeParam(10)
        self.displayParams()  
        
    def __keyFuncPAGEDown(self):
        self.changeParam(-10)
        self.displayParams()  
                
    def __trackerSaveConfig(self):
        self.tracker.saveConfig()
        self.setNotification('Tracker configuration saved', 2000)
        avg.logger.info("Tracker configuration saved.")
              
    def __saveTrackerIMG(self):
        def saveTrackerImage(id, name):
            self.tracker.getImage(id).save("img"+str(self.saveIndex)+"_"+name+".png")

        self.saveIndex += 1
        saveTrackerImage(avg.IMG_CAMERA, "camera")
        saveTrackerImage(avg.IMG_DISTORTED, "distorted")
        saveTrackerImage(avg.IMG_NOHISTORY, "nohistory")
        saveTrackerImage(avg.IMG_HIGHPASS, "highpass")
        saveTrackerImage(avg.IMG_FINGERS, "fingers")
        saveTrackerImage(avg.IMG_HISTOGRAM, "histogram")
        self.setNotification('Tracker images dumped', 2000)
        avg.logger.info("Tracker images saved.")
    
    def __startCoordCalibration(self):
        assert(not self.coordCal)

        self.__savedShutter = self.tracker.getParam("/camera/shutter/@value")
        self.tracker.setParam("/camera/shutter/@value", "8")
        self.__savedGain = self.tracker.getParam("/camera/gain/@value")
        self.tracker.setParam("/camera/gain/@value", "16")
        self.__savedStrobe = self.tracker.getParam("/camera/strobeduration/@value")
        self.tracker.setParam("/camera/strobeduration/@value", "-1")
        self.coordCal = coordcalibrator.CoordCalibrator(self.__onCalibrationTerminated)
    
    def __onCalibrationTerminated(self, isSuccessful):
        self.coordCal = None
        self.tracker.setParam("/camera/shutter/@value", self.__savedShutter)
        self.tracker.setParam("/camera/gain/@value", self.__savedGain)
        self.tracker.setParam("/camera/strobeduration/@value", self.__savedStrobe)
        self.deferredRefresh()
        
    def setOnCalibrationSuccess(self, callback):
        self.__onCalibrationSuccess = callback

    def deferredRefresh(self):
        player.setTimeout(1500, self.__deferredRefreshCB)
        self.setNotification('Please wait for settlement')
        g_KbManager.push()
        player.getElementByID('cal_params').opacity = 0.3

    def setNotification(self, text, timeout=0):
        player.getElementByID('cal_notification').text = text
        if timeout:
            if self.__notificationTimer is not None:
                player.clearInterval(self.__notificationTimer)
            
            self.__notificationTimer = player.setTimeout(timeout,
                    self.__clearNotification)

    def displayParams(self):
        i = 0
        for Param in self.paramList:
            Node = player.getElementByID("cal_param"+str(i))
            Path = Param['path']
            Val = float(self.tracker.getParam(Path))
            Node.text = (Param['Name']+": "
                    +('%(val).'+str(Param['precision'])+'f') % {'val': Val})
            if self.curParam == i:
                Node.color = "FFFFFF"
            else:
                Node.color = "A0A0FF"
            i += 1 

    def changeParam(self, Change):
        param = self.paramList[self.curParam]
        if param['increment'] >= 1:
            Val = int(float(self.tracker.getParam(param['path'])))
        else:
            Val = float(self.tracker.getParam(param['path']))
        Val += Change*param['increment']
        if Val < param['min']:
            Val = param['min']
        if Val > param['max']:
            Val = param['max']
        self.tracker.setParam(param['path'], str(Val))