Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement