5 This module defines a UI for capturing images with a (web)camera and imediately
6 proccessing them with Imago.
13 from threading import Thread
14 from Queue import Queue, Empty
17 from pygame.locals import QUIT, KEYDOWN
20 from camera import Camera
23 """Basic PyGame setup."""
24 def __init__(self, res):
26 pygame.display.set_mode(res)
27 pygame.display.set_caption("Go image capture")
28 self._screen = pygame.display.get_surface()
30 def display_picture(self, im):
31 """Display image on PyGame screen."""
32 pg_img = pygame.image.frombuffer(im.tostring(), im.size, im.mode)
33 self._screen.blit(pg_img, (0,0))
37 """This object maintains communication between the camera, the PyGame screen
39 def __init__(self, device, res):
40 self.cam = Camera(vid=device, res=res)
41 self.screen = Screen(res)
45 self.saving_dir = "./captured/" + time.strftime("%Y-%m-%d %H:%M/")
47 if not os.path.isdir(self.saving_dir):
48 os.makedirs(self.saving_dir)
54 """Run live preview on the screen."""
55 clock = pygame.time.Clock()
60 line = q.get_nowait() # or q.get(timeout=.1)
66 elif line == "exit\n":
69 for event in pygame.event.get():
70 if event.type == pygame.QUIT:
72 elif event.type == pygame.KEYDOWN:
75 im = self.cam.get_image()
76 self.screen.display_picture(im)
79 def auto(self, interval):
80 """Take new image every *interval* seconds.""" #TODO or is it milisecs?
82 clock = pygame.time.Clock()
86 for event in pygame.event.get():
87 if event.type == pygame.QUIT:
89 if time.time() - last > interval:
95 """Take images manualy by pressing a key."""
97 event = pygame.event.wait()
98 if event.type == QUIT:
100 if event.type != KEYDOWN:
105 """Take a new image from the camera."""
107 im = self.cam.get_image()
108 self.screen.display_picture(im)
109 im.save(self.saving_dir + "{0:0>3}.jpg".format(self.im_number), 'JPEG')
114 """Parse the argument and setup the UI."""
115 parser = argparse.ArgumentParser(description=__doc__)
116 parser.add_argument('-c', '--cmd', dest='cmd', action='store_true',
117 help="take commands from stdin")
118 parser.add_argument('-d', type=int, default=0,
119 help="video device id")
120 parser.add_argument('-a', type=int, default=0,
121 help="take picture automaticaly every A seconds")
122 parser.add_argument('-r', type=int, nargs=2, default=[640, 480],
123 help="set camera resolution")
124 args = parser.parse_args()
126 res=(args.r[0], args.r[1])
127 capture = Capture(args.d, res)
131 def enqueue_input(queue):
132 for line in iter(sys.stdin.readline, b''):
136 t = Thread(target=enqueue_input, args=(q,))
142 clock = pygame.time.Clock()
146 line = q.get_nowait() # or q.get(timeout=.1)
152 elif line == "exit\n":
165 if __name__ == '__main__':