Ladies and Gentleman, turn your lights on
SourceCode
#!/usr/bin/python import pygame import pygame.camera class Capture(object): a = [] allines = [] found = 0 def __init__(self): self.size = (640,480) # create a display surface. standard pygame stuff self.display = pygame.display.set_mode(self.size, 0) # this is the same as what we saw before self.clist = pygame.camera.list_cameras() if not self.clist: raise ValueError("Sorry, no cameras detected.") self.cam = pygame.camera.Camera(self.clist[0], self.size) self.cam.start() # create a surface to capture to. for performance purposes # bit depth is the same as that of the display surface. self.snapshot = pygame.surface.Surface(self.size, 0, self.display) def get_and_flip(self): # if you don't want to tie the framerate to the camera, you can check # if the camera has an image ready. note that while this works # on most cameras, some will never return true. if self.cam.query_image(): self.snapshot = self.cam.get_image(self.snapshot) # blit it to the display surface. simple! self.display.blit(self.snapshot, (0,0)) pygame.display.flip() def main(self): going = True while going: for event in pygame.event.get() : if event.type == pygame.KEYDOWN : if event.key == pygame.K_SPACE : print "Space bar pressed down." elif event.key == pygame.K_ESCAPE : print "Escape key pressed down." elif event.type == pygame.KEYUP : if event.key == pygame.K_SPACE : del self.a[:] del self.allines[:] elif event.key == pygame.K_ESCAPE : pygame.quit() self.get_and_flip() def get_and_flip(self): self.snapshot = self.cam.get_image(self.snapshot) # threshold against the color we got before crect = pygame.draw.rect(self.display, (255,0,0), (145,105,30,30), 4) #self.ccolor = pygame.transform.average_color(self.snapshot, crect) mask = pygame.mask.from_threshold(self.snapshot, (240, 240, 255), (30, 30, 30)) self.display.blit(self.snapshot,(0,0)) # keep only the largest blob of that color connected = mask.connected_component() # make sure the blob is big enough that it isn't just noise if mask.count() > 100: # find the center of the blob coord = mask.centroid() self.a.append(coord) self.found = 0 else: self.found = self.found+1 #if we not found the threshold color more then 15 times #we create a new line if self.found > 15: self.allines.append(self.a[:]) del self.a[:] l = len(self.a) for i in range(len(self.allines)): if len(self.allines[i]) >1: pygame.draw.aalines(self.display, (255,255,255), 0, self.allines[i], 1) if l > 1: pygame.draw.aalines(self.display, (255,255,255), 0, self.a, 1) pygame.display.flip() pygame.init() pygame.camera.init() x = Capture() x.main()
Happy Hacking
Andreas