Python Pastebin - Nopaste - Paste - easy sharing of text and codes

Posted by SecretStoat on Fri 3rd Sep 00:04 (modification of post by view diff)
download | new post

  1. import sys
  2. import pygame
  3. import pygame.camera
  4. from pygame.locals import *
  5.  
  6. class Concept():
  7.         def __init__(self, pSize=(640, 480)):
  8.                 # Set up a pygame window display surface
  9.                 self.window = pygame.display.set_mode(pSize, 0)
  10.                 pygame.display.set_caption('Webcam Colour Tracking: ...Waiting for webcam...')
  11.  
  12.                 # Set up clock for FPS counting
  13.                 self.clock = pygame.time.Clock()
  14.                
  15.                 # Initialise camera
  16.                 pygame.camera.init()
  17.                 self.clist = pygame.camera.list_cameras()
  18.                 if not self.clist:
  19.                         raise ValueError("Sorry, no cameras detected.")
  20.                 self.webcam = pygame.camera.Camera(self.clist[0], pSize) # Currently just choose the first camera
  21.                 self.webcam.start()
  22.  
  23.                 # Create a surface to capture to, same bit depth as window display surface
  24.                 self.snapshot = pygame.surface.Surface(pSize, 0, self.window)
  25.  
  26.                 # Load identifiers
  27.                 self.icons = []
  28.                 self.icons.append(["red", pygame.image.load ("red.png").convert_alpha(), (200, 50, 20), (80, 20, 15), (None, None)])
  29.                 self.icons.append(["green", pygame.image.load ("green.png").convert_alpha(), (50, 150, 50), (20, 80, 20), (None, None)])
  30.                 self.icons.append(["blue", pygame.image.load ("blue.png").convert_alpha(), (50, 50, 180), (20, 20, 80), (None, None)])
  31.                 self.icons.append(["laser", pygame.image.load ("laser.png").convert_alpha(), (180, 200, 180), (70, 50, 70), (None, None)])
  32.  
  33.         def get_colour_location(self, pColour=(200, 50, 50), pColourThreshold=(80, 20, 20)):
  34.                 self.snapshot = self.webcam.get_image(self.snapshot)
  35.                 if self.flipX or self.flipY:
  36.                         self.snapshot = pygame.transform.flip(self.snapshot, flipX, flipY)
  37.  
  38.                 # Threshold against the colour we got before
  39.                 mask = pygame.mask.from_threshold(self.snapshot, pColour, pColourThreshold)
  40.  
  41.                 overlay = self.snapshot.copy()
  42.                 overlay.set_alpha(40)
  43.                 overlay.lock()
  44.                 self.snapshot.lock()
  45.                 for m in mask.connected_components():
  46.                         if m.count() > 50:
  47.                                 for r in m.get_bounding_rects():
  48.                                         pygame.draw.rect(self.snapshot, pColour, r, 1)
  49.                                 pygame.draw.polygon(overlay, (pColour[0], pColour[1], pColour[2], 160), m.outline(3), 0)
  50.                 self.snapshot.unlock()
  51.                 overlay.unlock()
  52.                 self.snapshot.blit(overlay, (0, 0))
  53.                
  54.  
  55.                 # Keep only the largest blob of that colour
  56.                 connected = mask.connected_component()
  57.                 # These numbers are purely experimental and specific to your room and object
  58.                 # print mask.count() # use this to estimate
  59.                 # make sure the blob is big enough that it isn't just noise
  60.                 if mask.count() > 50:
  61.                         return mask.centroid()
  62.                 return (None,None)
  63.  
  64.         def main(self, pFlipX=False, pFlipY=False):
  65.                 running = True
  66.                 self.flipX = pFlipX
  67.                 self.flipY = pFlipY
  68.  
  69.                 while running:
  70.                         self.clock.tick()
  71.                         pygame.display.set_caption('Webcam Colour Tracking: %d fps' % self.clock.get_fps())
  72.                        
  73.                         # Check for events
  74.                         for e in pygame.event.get():
  75.                                 if e.type == QUIT or (e.type == KEYDOWN and e.key == K_ESCAPE):
  76.                                         # Exit cleanly
  77.                                         self.webcam.stop()
  78.                                         running = False
  79.  
  80.                         # Find icon positions
  81.                         for icon in self.icons:
  82.                                 tmp_coord = self.get_colour_location(pColour=icon[2], pColourThreshold=icon[3])
  83.                                 if tmp_coord != (None,None):
  84.                                         if icon[4] != (None,None):
  85.                                                 delta = sum( [(x-y)**2 for (x,y) in zip(tmp_coord, icon[4])]) # Kalman might be better
  86.                                                 if delta > 20:
  87.                                                         icon[4] = tmp_coord
  88.                                         else:
  89.                                                 icon[4] = tmp_coord
  90.                                         self.snapshot.blit(icon[1], icon[4])
  91.  
  92.                         # Finally blit the snapshot to the window
  93.                         self.window.blit(self.snapshot,(0,0))
  94.                         #pygame.display.flip()
  95.                         pygame.display.update()
  96.  
  97.  
  98. # Run Proof of Concept
  99. if __name__=='__main__':
  100.         con = Concept()
  101.         con.main()

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


I'm Human
Remember me



Captcha required for posting