Difference between revisions of "PlayChessWithAWebCam/DetectState"

From BITPlan Wiki
Jump to navigation Jump to search
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Second attempt =
+
{{:PlayChessWithAWebCam/Links}}
 +
= State transitions =
 
<graphviz>
 
<graphviz>
 
   digraph detection2 {
 
   digraph detection2 {
     calibrating -> calibrating [ label="gradient < gradientDelta" ]
+
     calibrating -> calibrating [ label="not stable or less than calibrationWindow times calibrating" ]
     calibrating -> preMove [ label="" ]
+
     calibrating -> preMove [ label="stable and more than calibrationWindows times calibrating" ]
     preMove -> preMove
+
     preMove -> preMove [ label="stable" ]
     preMove -> inMove [ label="" ]
+
     preMove -> inMove [ label="not stable" ]
     inMove->inMove
+
     inMove->inMove [ label="not stable or difference of pixelChanges>10% of peak inMove value" ]
     inMove -> potentialMove [ label="" ]
+
     inMove -> potentialMove [ label="stable and difference of pixelChanges<10% of peak inMove value" ]
 
   }
 
   }
 
</graphviz>
 
</graphviz>
  
= First attempt =
+
= Relevant Source parts =
<graphviz>
+
<source lang='python'>
  digraph detection1 {
+
class ImageChange:
    calibrating -> valid [ label="more than calibrationWindow value" ]
+
...
    valid -> validStable [ label="more than validWindow valid values" ]
+
def isStable(self):
    validStable -> invalid [ label="value out of valid range" ]
+
        delta=abs(self.movingAverage.gradient())
    invalid -> invalidStable  [ label="value out of valid range for more than invalidWindow values" ]
+
        stable=delta<ImageChange.gradientDelta
    invalidStable -> potentialMove [ label="value in valid range" ]
+
        if stable:
    potentialMove -> invalidStable [ label="value out of valid range" ]
+
            self.stableCounter+=1
    potentialMove -> valid [ label="value in valid range - move detected?" ]
+
        else:
   
+
            self.stableCounter=0   
  }
+
        return stable 
</graphviz>
+
...
 +
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)
 +
</source>

Latest revision as of 12:10, 18 December 2019

Click here to comment see PlayChessWithAWebCam

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)