-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandstats.py
65 lines (54 loc) · 3.18 KB
/
handstats.py
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
import math, time, cv2
import numpy as np
from depthmap import *
from hand import *
from coords import *
def performFuncOverInterval(func, *args, **kwargs):
interval = kwargs.pop('interval', 0.01) # only kwarg = interval, default val = 0.01
intervalStartTime = time.time()
result = func(*args)
timeTakenToSample = time.time() - intervalStartTime
sleepTime = interval-timeTakenToSample if timeTakenToSample < interval else 0
time.sleep(sleepTime)
return result
class HandStats(Hand):
"""child class of Hand with sampling capabilities"""
def sampleOpenFingersForMsec(self, msec=50, intervalMsec=10):
"""Return fraction of time, 0<=t<=1, that each finger is open during sampling time."""
openFingerSamples={fing: [] for fing in getFingList()}
startTime = time.time()
while (time.time() - startTime) < (msec/1000.0):
openFingerSamples = performFuncOverInterval(self.addNewFingPosSample, openFingerSamples, interval=intervalMsec/1000.0)
if any([len(fingSampLs) == 0 for fingSampLs in openFingerSamples.values()]): return None
openFingerAverages = {fing: average(openFingerSamples[fing]) for fing in getFingList()}
return openFingerAverages
def addNewFingPosSample(self, prevSamps):
fingersSample = self.getOpenFingers(getMask())
if fingersSample != None:
for fing in getFingList(): prevSamps[fing].append(fingersSample[fing])
return prevSamps
def sampleHandPosForMsec(self, msec=50, intervalMsec=10):
handPosList = []
startTime = time.time()
while (time.time() - startTime) < (msec/1000.0):
handPosList = performFuncOverInterval(self.addNewHandPosSample, handPosList, interval=intervalMsec/1000.0)
return handPosList
def addNewHandPosSample(self, prevSamps):
handPos = self.getHandPos(getMask())
if handPos != None: prevSamps.append(handPos)
return prevSamps
def getHandVelocityVec(self, sampleTimeMsec=50, sampIntervalMsec=10):
handPosList = self.sampleHandPosForMsec(msec=sampleTimeMsec, intervalMsec=sampIntervalMsec)
if len(handPosList)<2: return None # if less than two positions, cannot calculate speed
getSpeedVec = lambda p1, p2, time: Vector((p2.getX()-p1.getX())/time, (p2.getY()-p1.getY())/time)
speedsList = [getSpeedVec(handPosList[i], handPosList[i+1], sampIntervalMsec/1000.0) for i in range(len(handPosList)-1)]
averageSpeedVec = Vector(average([p.getX() for p in speedsList]), average([p.getY() for p in speedsList]))
return averageSpeedVec
def getHandAccelVec(self, sampleTimeMsec=50, sampIntervalMsec=10):
"""assume acceleration is constant during sampling"""
speedSampleTime = sampleTimeMsec/2.0
veloc1 = self.getHandVelocityVec(sampleTimeMsec=speedSampleTime, sampIntervalMsec=sampIntervalMsec)
veloc2 = self.getHandVelocityVec(sampleTimeMsec=speedSampleTime, sampIntervalMsec=sampIntervalMsec)
if veloc1==None or veloc2==None: return None
accelerationVec = Vector((veloc2.getX()-veloc1.getX())/(sampleTimeMsec/1000.0), (veloc2.getY()-veloc1.getY())/(sampleTimeMsec/1000.0))
return accelerationVec