Difference between revisions of "PlayChessWithAWebCam/DetectState"

From BITPlan Wiki
Jump to navigation Jump to search
Line 1: Line 1:
= Second attempt =
+
= State transitions =
 
<graphviz>
 
<graphviz>
 
   digraph detection2 {
 
   digraph detection2 {
Line 10: Line 10:
 
   }
 
   }
 
</graphviz>
 
</graphviz>
 +
= Relevant Source parts =
 
<source lang='python'>
 
<source lang='python'>
 
class ImageChange:
 
class ImageChange:
Line 47: Line 48:
 
                         self.onMoveDetected(cbImageSet)
 
                         self.onMoveDetected(cbImageSet)
 
</source>
 
</source>
 
= First attempt =
 
<graphviz>
 
  digraph detection1 {
 
    calibrating -> valid [ label="more than calibrationWindow value" ]
 
    valid -> validStable [ label="more than validWindow valid values" ]
 
    validStable -> invalid [ label="value out of valid range" ]
 
    invalid -> invalidStable  [ label="value out of valid range for more than invalidWindow values" ]
 
    invalidStable -> potentialMove [ label="value in valid range" ]
 
    potentialMove -> invalidStable [ label="value out of valid range" ]
 
    potentialMove -> valid [ label="value in valid range - move detected?" ]
 
   
 
  }
 
</graphviz>
 

Revision as of 12:25, 17 December 2019

State transitions

Relevant Source parts

class ImageChange:
...
def isStable(self):
        delta=abs(self.movingAverage.gradient())
        stable=delta<ImageChange.gradientDelta
        if stable:
            self.stableCounter+=1
        else:
            self.stableCounter=0    
        return stable  
...
class SimpleDetector:
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)