Difference between revisions of "PlayChessWithAWebCam/DetectState"

From BITPlan Wiki
Jump to navigation Jump to search
Line 10: Line 10:
 
   }
 
   }
 
</graphviz>
 
</graphviz>
 +
<source lang='python'>
 +
def updateState(self,cbImageSet):
 +
        calibrationWindow=5
 +
       
 +
        ic=self.imageChange
 +
        ics=ic.changeState
 +
        if ics==ChangeState.CALIBRATING:
 +
            # leave calibrating when enough stable values are available
 +
            if ic.isStable() and ic.stableCounter>=calibrationWindow:
 +
                ic.changeState=ChangeState.PRE_MOVE
 +
        elif ics==ChangeState.PRE_MOVE:
 +
            if not ic.isStable():
 +
                ic.changeState=ChangeState.IN_MOVE
 +
                ic.minInMove=ic.pixelChanges
 +
                ic.maxInMove=ic.pixelChanges
 +
        elif ics==ChangeState.IN_MOVE:
 +
            ic.maxInMove=max(ic.maxInMove,ic.pixelChanges)
 +
            peak=ic.maxInMove-ic.minInMove
 +
            dist=ic.pixelChanges-ic.minInMove
 +
            if peak>0:
 +
                relativePeak=dist/peak
 +
                if ic.isStable():
 +
                    if relativePeak<0.1:
 +
                        self.onMoveDetected(cbImageSet)
 +
</source>
  
 
= First attempt =
 
= First attempt =

Revision as of 13:18, 17 December 2019

Second attempt

def updateState(self,cbImageSet):
        calibrationWindow=5
        
        ic=self.imageChange
        ics=ic.changeState
        if ics==ChangeState.CALIBRATING:
            # leave calibrating when enough stable values are available
            if ic.isStable() and ic.stableCounter>=calibrationWindow:
                ic.changeState=ChangeState.PRE_MOVE
        elif ics==ChangeState.PRE_MOVE:
            if not ic.isStable():
                ic.changeState=ChangeState.IN_MOVE
                ic.minInMove=ic.pixelChanges
                ic.maxInMove=ic.pixelChanges
        elif ics==ChangeState.IN_MOVE:
            ic.maxInMove=max(ic.maxInMove,ic.pixelChanges)
            peak=ic.maxInMove-ic.minInMove
            dist=ic.pixelChanges-ic.minInMove
            if peak>0:
                relativePeak=dist/peak
                if ic.isStable():
                    if relativePeak<0.1:
                        self.onMoveDetected(cbImageSet)

First attempt