Skip to content

Instantly share code, notes, and snippets.

@tusing
Created December 10, 2016 06:51
Show Gist options
  • Save tusing/bcf6d2cbcd486fd07bf6d21aa0edb54e to your computer and use it in GitHub Desktop.
Save tusing/bcf6d2cbcd486fd07bf6d21aa0edb54e to your computer and use it in GitHub Desktop.
import freenect
import numpy as np
from threading import Thread, RLock
class KinectParser:
def __init__(self, x, y, function=False):
if function is False:
self.function = self.decayingAverage
else:
self.function = function
self.subdivisions = np.zeros((y, x), dtype=tuple)
def getValue(self, x, y):
self.lock.acquire()
return self.subdivisions[y, x]
self.lock.release()
def getValues(self):
self.lock.acquire()
return self.subdivisions
self.lock.release()
def _setValues(self):
self.lock.acquire()
array, _ = freenect.sync_get_depth()
array = array.astype(np.uint8)
heightStrides = array.shape[0] // self.y
widthStrides = array.shape[1] // self.x
for yi in range(self.y):
yWindow = array[heightStrides * yi: heightStrides * (yi + 1)]
for xi in range(self.x):
xyWindow = yWindow[widthStrides * xi: widthStrides * (xi + 1)]
self.subdivisions[yi, xi] = self.function(xyWindow, xi, yi)
self.lock.release()
def decayingAverage(self, xyWindow, x, y, numSamples=4):
windowAverage = int(np.average(xyWindow))
oldDistance = self.getDistanceValue(x, y)
newDistChange = windowAverage - oldDistance
newDistValue = windowAverage * \
(1 / numSamples) + oldDistance * ((numSamples - 1) / numSamples)
return (newDistValue, newDistChange)
def run(self):
self.lock = RLock()
def startParsing():
while True:
self._setValues()
Thread(target=startParsing).start()
def main(argv):
parser = KinectParser(8, 4, KinectParser.decayingAverage)
print(parser.getValue(4, 4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment