Advertisement
Guest User

kinect

a guest
Jan 7th, 2012
1,286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.38 KB | None | 0 0
  1. from OpenGL.GL import *
  2. from pygame import *
  3. from opencv import highgui
  4. import Image, pygame, opencv, math, sys
  5.  
  6. class Camera(object):
  7.     def __init__(self, draw_rectangle=False):
  8.         self.camera = highgui.cvCreateCameraCapture(0)
  9.         self.template = highgui.cvLoadImage("template.png", 0)
  10.         self.image = None; self.location = None
  11.         self.rectangle = draw_rectangle
  12.        
  13.     def update_image(self):
  14.         im = highgui.cvQueryFrame(self.camera)
  15.         self.image = opencv.cvGetMat(im)
  16.  
  17.     def get_image(self): return opencv.adaptors.Ipl2PIL(self.image)
  18.     def get_position(self):
  19.         if not self.location: return 0,0
  20.         return self.location.x, self.location.y
  21.  
  22.     def track(self):
  23.         result = opencv.cvCreateImage(opencv.cvSize(self.image.width-self.template.width+1,
  24.                                                     self.image.height-self.template.height+1),
  25.                                       opencv.IPL_DEPTH_32F, 1)
  26.         image = opencv.cvCreateImage(opencv.cvSize(self.image.width,self.image.height), opencv.IPL_DEPTH_8U, 1)
  27.         opencv.cvCvtColor(self.image,image,opencv.CV_RGB2GRAY)
  28.         opencv.cvZero(result)
  29.         opencv.cvMatchTemplate(image, self.template, result, opencv.CV_TM_CCOEFF_NORMED)
  30.         min_val, max_val, min_loc, max_loc = opencv.cvMinMaxLoc(result)
  31.         if max_val < .25: return
  32.         else: self.location = max_loc
  33.  
  34.     def draw_rectangle(self):
  35.         if not self.location: return
  36.         opencv.cvRectangle(self.image, self.location,
  37.                            opencv.cvPoint(self.location.x+self.template.width,
  38.                                           self.location.y+self.template.height),
  39.                            opencv.cvScalar(0), 1)
  40.  
  41.     def update(self):
  42.         self.update_image()
  43.         self.track()
  44.         if self.rectangle: self.draw_rectangle()
  45.  
  46. class Tracking(object):
  47.     def __init__(self, position, motions, callback=None):
  48.         self.position = position
  49.         self.change = [0,0]
  50.         self.path = []
  51.         self.threshold = 32
  52.         self.diff_threshold = 20
  53.         self.angle_step = 45
  54.         self.motions = motions
  55.         self.callback = callback
  56.         self.motion = None
  57.  
  58.     def update_position(self, position):
  59.         self.change = [self.position[x]-position[x] for x in range(len(position))]
  60.         self.position = position
  61.  
  62.     def motion_diff(self, path, motion):
  63.         diffs = [abs(motion[x]-path[x]) for x in range(min([len(path),len(motion)]))]
  64.         diff = sum(diffs)/float(len(diffs))
  65.         return diff
  66.  
  67.     def get_motion(self):
  68.         return self.motion
  69.  
  70.     def update(self):
  71.         self.motion = None
  72.         if math.sqrt(self.change[0]**2+self.change[1]**2) > self.threshold:
  73.             self.path.append(self.change)
  74.         else:
  75.             if len(self.path) > 1:
  76.                 angles = []
  77.                 for angle in [math.degrees(math.atan2(y,x)) for x,y in self.path]:
  78.                     if not angles: angles.append(angle)
  79.                     else:
  80.                         if abs(abs(angles[-1])-abs(angle)) > self.angle_step:
  81.                             angles.append(angle)
  82.                 diffs = {}
  83.                 for motion in self.motions:
  84.                     diff = self.motion_diff(angles,motion)
  85.                     if diff < self.diff_threshold: diffs[diff] = motion
  86.                 if len(diffs.keys()): self.motion = diffs[min(diffs.keys())]
  87.                 if self.callback and self.motion: self.callback(self.motion)
  88.             self.path = []
  89.                
  90.  
  91. class Application(object):
  92.     def __init__(self):
  93.         pygame.init()
  94.         self.screen_size = (640,480)
  95.         self.motions = [[0],[90],[-180],[180],[-90]]
  96.         self.window = pygame.display.set_mode(self.screen_size, OPENGL|DOUBLEBUF)
  97.         self.initgl()
  98.         self.camera = Camera(True)
  99.         self.camera.update()
  100.         self.tracking = Tracking(self.camera.get_position(), self.motions, self.motion_callback)
  101.         self.display_list = glGenLists(1)
  102.         self.clock = pygame.time.Clock()
  103.         self.background = None
  104.  
  105.     def start(self):
  106.         self.update()
  107.         self.background = opencv.cvCloneImage(self.camera.image)
  108.         self.render()
  109.         self.loop()
  110.  
  111.     def initgl(self):
  112.         glViewport(0,0,self.screen_size[0],self.screen_size[1])
  113.         glMatrixMode(GL_PROJECTION)
  114.         glLoadIdentity()
  115.         glOrtho(0,self.screen_size[0],self.screen_size[1],0,0,10)
  116.         glMatrixMode(GL_MODELVIEW)
  117.         glLoadIdentity()
  118.         glClearColor(1,1,1,1)
  119.         glEnable(GL_TEXTURE_2D)
  120.         glGenTextures(1)
  121.  
  122.     def set_texture(self):
  123.         glBindTexture(GL_TEXTURE_2D, 1)
  124.         image = self.camera.get_image()
  125.         data = image.tostring()
  126.         glTexImage2D(GL_TEXTURE_2D, 0, 3, image.size[0], image.size[1], 0,
  127.                      GL_RGB, GL_UNSIGNED_BYTE, data)
  128.         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR)
  129.         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR)
  130.  
  131.     def render(self):
  132.         glNewList(self.display_list, GL_COMPILE)
  133.         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
  134.         glPushMatrix()
  135.         glBegin(GL_QUADS)
  136.         glTexCoord2i(0,0); glVertex2i(0,0)
  137.         glTexCoord2i(1,0); glVertex2i(self.screen_size[0],0)
  138.         glTexCoord2i(1,1); glVertex2i(self.screen_size[0],self.screen_size[1])
  139.         glTexCoord2i(0,1); glVertex2i(0,self.screen_size[1])
  140.         glEnd()
  141.         glPopMatrix()
  142.         glEndList()
  143.  
  144.     def motion_callback(self, motion):
  145.         print motion
  146.  
  147.     def end(self):
  148.         print self.clock.get_fps()
  149.         pygame.quit()
  150.         sys.exit(0)
  151.  
  152.     def handle_events(self, events):
  153.         for event in events:
  154.             if event.type == QUIT:
  155.                 self.end()
  156.  
  157.     def paint(self):
  158.         self.set_texture()
  159.         glCallList(self.display_list)
  160.         pygame.display.flip()
  161.  
  162.     def sync(self):
  163.         glFinish()
  164.  
  165.     def update(self):
  166.         self.camera.update()
  167.         self.tracking.update_position(self.camera.get_position())
  168.         self.tracking.update()
  169.  
  170.     def loop(self):
  171.         while True:
  172.             self.clock.tick()
  173.             self.handle_events(pygame.event.get())
  174.             self.update()
  175.             self.paint()
  176.             self.sync()
  177.  
  178. if __name__ == '__main__':
  179.     app = Application()
  180.     app.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement