Guest User

Untitled

a guest
Jul 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. import sys
  2. sys.path += ['..']
  3. import cv
  4. import numpy as np
  5. from visuals.pykinectwindow import Window
  6. from OpenGL.GL import *
  7.  
  8. from OpenGL.GL.ARB.vertex_buffer_object import *
  9. import calibkinect
  10.  
  11. # I probably need more help with these! Which graphics cards
  12. # support OpenGL2.0 textures and which support the earlier ARB rect?
  13. try:
  14. TEXTURE_TARGET = GL_TEXTURE_RECTANGLE
  15. except:
  16. TEXTURE_TARGET = GL_TEXTURE_RECTANGLE_ARB
  17.  
  18. if not 'window' in globals():
  19. window = Window()
  20.  
  21. # Always create fresh textures when we run the script
  22. def create_textures():
  23. global textures
  24. if 'textures' in globals(): glDeleteTextures(textures)
  25. textures = glGenTextures(3)
  26.  
  27. # Read in the mask image, separate into channels
  28. png = np.asarray(cv.LoadImageM('mask.png'))
  29. h,w = png.shape[:2]
  30. global R,G,B
  31. R,G,B = np.rollaxis(png,2)>120
  32.  
  33. from scipy.ndimage import uniform_filter
  34. m1 = np.dstack(4*[~R|~B]).astype('f') # Start with back layer
  35. m1 = uniform_filter(m1, 45) # Blur it to get halo effect
  36. m1 *= [0.6,0.6,1.2,5.0] # Set the base color
  37. m1 = m1.clip(0,1) # Constrain
  38.  
  39. # Add in the solid button backgrounds
  40. m1[~G,:] = [0.1,0.1,0.5,1.0]
  41. m1[R&~G,:] = [0.5,0.5,0.8,1.0]
  42.  
  43. # Make the mask for the cursor strip
  44. m2 = np.dstack(4*[B&~R]).astype('f')
  45.  
  46. # Make the mask for the button icons
  47. m3 = np.dstack(4*[R&~G]).astype('f')
  48.  
  49. glBindTexture(TEXTURE_TARGET, textures[0])
  50. glTexImage2D(TEXTURE_TARGET,0,GL_RGBA,w,h,0,GL_RGBA,GL_FLOAT,m1)
  51. glBindTexture(TEXTURE_TARGET, textures[1])
  52. glTexImage2D(TEXTURE_TARGET,0,GL_RGBA,w,h,0,GL_RGBA,GL_FLOAT,m2)
  53. glBindTexture(TEXTURE_TARGET, textures[2])
  54. glTexImage2D(TEXTURE_TARGET,0,GL_RGBA,w,h,0,GL_RGBA,GL_FLOAT,m3)
  55.  
  56. create_textures()
  57.  
  58. cursorpos = 320
  59. cursorpick = None
  60.  
  61. def init():
  62. pass
  63. init()
  64.  
  65. @window.event
  66. def on_draw():
  67. glClearColor(0,0,0,0)
  68. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  69.  
  70.  
  71. glMatrixMode(GL_PROJECTION)
  72. glLoadIdentity()
  73. glOrtho(0,640,0,480,-1,1)
  74.  
  75. glMatrixMode(GL_MODELVIEW)
  76. glLoadIdentity()
  77.  
  78. # Draw the glowy background and buttons
  79. glEnable (GL_BLEND);
  80. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  81. glEnable(TEXTURE_TARGET)
  82. glBindTexture(TEXTURE_TARGET, textures[0])
  83. glColor(1,1,1)
  84. glBegin(GL_QUADS)
  85. glTexCoord(0,240); glVertex(0,0);
  86. glTexCoord(0,0); glVertex(0,240);
  87. glTexCoord(640,0); glVertex(640,240);
  88. glTexCoord(640,240); glVertex(640,0)
  89. glEnd()
  90.  
  91. # Draw the cursor only where the texture is
  92. x = cursorpos
  93. glBindTexture(TEXTURE_TARGET, textures[1])
  94. glBegin(GL_QUAD_STRIP)
  95. glColor(0,0,0.6);
  96. glTexCoord(x-50,240); glVertex(x-50,0); glTexCoord(x-50,0); glVertex(x-50,240)
  97. glColor(1,1,1);
  98. glTexCoord(x ,240); glVertex(x ,0); glTexCoord(x ,0); glVertex(x ,240)
  99. glColor(0,0,0.6);
  100. glTexCoord(x+50,240); glVertex(x+50,0); glTexCoord(x+50,0); glVertex(x+50,240)
  101. glEnd()
  102.  
  103. glDisable(GL_STENCIL_TEST)
  104.  
  105. if not cursorpick is None:
  106. glEnable(TEXTURE_TARGET)
  107. glBindTexture(TEXTURE_TARGET, textures[2])
  108. glColor(1,1,1)
  109. x = 100*(1+cursorpick)
  110. glBegin(GL_QUADS)
  111. glTexCoord(x-50,240); glVertex(x-50,0);
  112. glTexCoord(x-50,0); glVertex(x-50,240);
  113. glTexCoord(x+50,0); glVertex(x+50,240);
  114. glTexCoord(x+50,240); glVertex(x+50,0)
  115. glEnd()
  116.  
  117. @window.eventx
  118. def EVT_MOTION(event):
  119. # Control the sliding cursor using the mouse X coordinate
  120. x,_ = event.Position
  121. global cursorpos, cursorpick
  122. cursorpos = x
  123. pick = np.round(float(x)/100)
  124. dist = np.abs(x-pick*100)
  125. cursorpick = pick-1 if dist<30 else None
  126. window.Refresh()
  127.  
  128. import freenect
  129. import scipy.ndimage as nd
  130. @window.eventx
  131. def EVT_IDLE(event):
  132. global depth, cursorpos, cursorpick
  133. depth,_ = freenect.sync_get_depth()
  134. thresh = depth[::2,::2] < 600
  135. if thresh.sum() > 30:
  136. v,u = np.mgrid[:480/2,:640/2]
  137. x = cursorpos = cursorpos*0.8 + (640 - u[thresh].mean()*2)*0.2
  138. pick = np.round(float(x)/100)
  139. dist = np.abs(x-pick*100)
  140. cursorpick = pick-1 if dist<30 else None
  141. else:
  142. cursorpos = 0
  143. cursorpick = None
  144. event.RequestMore()
  145. window.Refresh()
  146.  
  147. window.Refresh()
Add Comment
Please, Sign In to add comment