Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. import freenect
  2. import numpy as np
  3. from threading import Thread, RLock
  4.  
  5.  
  6. class KinectParser:
  7. def __init__(self, x, y, function=False):
  8. if function is False:
  9. self.function = self.decayingAverage
  10. else:
  11. self.function = function
  12. self.subdivisions = np.zeros((y, x), dtype=tuple)
  13.  
  14. def getValue(self, x, y):
  15. self.lock.acquire()
  16. return self.subdivisions[y, x]
  17. self.lock.release()
  18.  
  19. def getValues(self):
  20. self.lock.acquire()
  21. return self.subdivisions
  22. self.lock.release()
  23.  
  24. def _setValues(self):
  25. self.lock.acquire()
  26. array, _ = freenect.sync_get_depth()
  27. array = array.astype(np.uint8)
  28. heightStrides = array.shape[0] // self.y
  29. widthStrides = array.shape[1] // self.x
  30. for yi in range(self.y):
  31. yWindow = array[heightStrides * yi: heightStrides * (yi + 1)]
  32. for xi in range(self.x):
  33. xyWindow = yWindow[widthStrides * xi: widthStrides * (xi + 1)]
  34. self.subdivisions[yi, xi] = self.function(xyWindow, xi, yi)
  35. self.lock.release()
  36.  
  37. def decayingAverage(self, xyWindow, x, y, numSamples=4):
  38. windowAverage = int(np.average(xyWindow))
  39. oldDistance = self.getDistanceValue(x, y)
  40. newDistChange = windowAverage - oldDistance
  41. newDistValue = windowAverage * \
  42. (1 / numSamples) + oldDistance * ((numSamples - 1) / numSamples)
  43. return (newDistValue, newDistChange)
  44.  
  45. def run(self):
  46. self.lock = RLock()
  47.  
  48. def startParsing():
  49. while True:
  50. self._setValues()
  51. Thread(target=startParsing).start()
  52.  
  53.  
  54. def main(argv):
  55. parser = KinectParser(8, 4, KinectParser.decayingAverage)
  56. print(parser.getValue(4, 4))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement